欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现简易TCP客户端、服务器端通信程序

程序员文章站 2022-06-19 15:49:57
本学期计算机网络课程要求完成一个TCP和一个UDP的通信程序,我完成了功能的简单实现,下面讲讲我的TCP程序的实现。目录效果展示一、项目结构二、完整代码1.TCPClient1.TCPClient总结效果展示以下是具体实现过程:一、项目结构下面说说每个类的具体作用:TCPClient:TCP客户端的核心代码部分;TCPClientFrame:TCP客户端的界面搭建;TCPServer:TCP服务器端的核心代码部分;TCPServerFrame:TCP服务器端的界面搭建;TCP...

本学期计算机网络课程要求完成一个TCP和一个UDP的通信程序,我完成了功能的简单实现,下面讲讲我的TCP程序的实现。(UDP的见另一篇博客)



效果展示

Java实现简易TCP客户端、服务器端通信程序

Java实现简易TCP客户端、服务器端通信程序

以下是具体实现过程:

一、项目结构

Java实现简易TCP客户端、服务器端通信程序

下面说说每个类的具体作用:
TCPClient:TCP客户端的核心代码部分;
TCPClientFrame:TCP客户端的界面搭建;
TCPServer:TCP服务器端的核心代码部分;
TCPServerFrame:TCP服务器端的界面搭建;
TCPSend:客户端和服务器端发送信息的功能实现;
ReminderTextAdapter:网上查阅的代码,非核心内容实现部分,主要用于灰色提示字hint功能的实现(swing中的实现方法不太熟悉);

二、完整代码

1.TCPClient

代码如下:

package tcpCommunication;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;

public class TCPClient implements ActionListener, Runnable {
    private TCPClientFrame clientFrame;
    private Socket socket;
    private Thread thread = null;
    private DataInputStream inFromServer;
    private DataOutputStream outToServer;
    
    public TCPClient() {
        JPanel panel = new JPanel();

        clientFrame = new TCPClientFrame("TCPClient");
        clientFrame.add(panel);

        clientFrame.placeComponents(panel);
        clientFrame.setVisible(true);

        clientFrame.getConnect().addActionListener(e -> {
            try {
                //host:"127.0.0.1"
                String host = clientFrame.getIpField().getText();
                //port:6666
                int port = Integer.parseInt(clientFrame.getPortField().getText());
                socket = new Socket(host, port);
                clientFrame.getTextArea().append("[Connect] : Connect…\n");

                inFromServer = new DataInputStream(socket.getInputStream());
                outToServer = new DataOutputStream(socket.getOutputStream());
                //new TCPSend(socket, "客户端").sendMessage();

                thread = new Thread(this);
                thread.setPriority(Thread.MIN_PRIORITY);
                thread.start();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });

        clientFrame.getTransmit().addActionListener(this);
        System.out.println("TCP客户端准备就绪\n");
    }

    public static void main(String[] args) {
        new TCPClient();
    }

    //向服务器端发送数据
    @Override
    public void actionPerformed(ActionEvent e) {
        String send = clientFrame.getSendField().getText();
        new TCPSend(send, outToServer, clientFrame.getTextArea(),
                "Client").sendMessages();
    }

    //使用线程处理客户端接收服务器端的数据
    @Override
    public void run() {
        while (true) {
            try {
                clientFrame.getTextArea().append("[Server send] : " + inFromServer.readUTF() + "\n");
                Thread.sleep(100);
            } catch (IOException | InterruptedException e) {
                //e.printStackTrace();
                System.out.println("客户端与服务器连接中断\n");
                break;
            }
        }
    }
}

2.TCPClientFrame

代码如下:

package tcpCommunication;

import udpCommunication.ReminderTextAdapter;

import javax.swing.*;
import java.awt.*;

public class TCPClientFrame extends JFrame {
    private static JTextField ipField;
    private static JTextField portField;
    private static JTextField sendField;
    private static JTextArea textArea;
    private static JButton connect;
    private static JButton transmit;

    public TCPClientFrame(String title) {
        setSize(700, 480);
        setLocationRelativeTo(null);//居中显示
        setResizable(false);//设置不允许改变窗体大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
    }

    public JTextField getIpField() {
        return ipField;
    }

    public JTextField getPortField() {
        return portField;
    }

    public JTextField getSendField() {
        return sendField;
    }

    public JTextArea getTextArea() {
        return textArea;
    }

    public JButton getConnect() {
        return connect;
    }

    public JButton getTransmit() {
        return transmit;
    }

    public void placeComponents(JPanel panel) {
        panel.setLayout(null);
        Font font1 = new Font("宋体", Font.BOLD, 18);
        Font font2 = new Font("宋体", Font.PLAIN, 18);

        //创建JLabel
        JLabel ip = new JLabel("IP");
        ip.setFont(font1);//设置JLabel的字体
        ip.setBounds(105, 35, 120, 38);
        panel.add(ip);

        JLabel port = new JLabel("Port");
        port.setFont(font1);
        port.setBounds(105, 80, 120, 38);
        panel.add(port);

        JLabel send = new JLabel("Send");
        send.setFont(font1);
        send.setBounds(105, 125, 120, 38);
        panel.add(send);

        JLabel text = new JLabel("Text");
        text.setFont(font1);
        text.setBounds(105, 170, 120, 38);
        panel.add(text);

        //创建文本域
        ipField = new JTextField(30);
        ipField.setFont(font2);
        ipField.addFocusListener(new tcpCommunication.ReminderTextAdapter(ipField, "默认:127.0.0.1"));
        ipField.setBounds(180, 35, 380, 38);
        panel.add(ipField);

        portField = new JTextField(30);
        portField.setFont(font2);
        portField.addFocusListener(new ReminderTextAdapter(portField, "port:"));
        portField.setBounds(180, 80, 380, 38);
        panel.add(portField);

        sendField = new JTextField(30);
        sendField.setFont(font2);
        sendField.addFocusListener(new ReminderTextAdapter(sendField, "请输入要发送的信息:"));
        sendField.setBounds(180, 125, 380, 38);
        panel.add(sendField);

        textArea = new JTextArea();
        textArea.setFont(font2);
        textArea.setBounds(180, 170, 380, 170);
        textArea.setEditable(false);
        textArea.setLineWrap(false);
        panel.add(textArea);

        //给textArea添加滚动条
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(180, 170, 380, 170);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(scrollPane);
        scrollPane.setViewportView(textArea);

        //添加两个按钮
        connect = new JButton("连接");
        connect.setFont(font1);
        connect.setBounds(140, 360, 115, 40);
        panel.add(connect);

        transmit = new JButton("发送");
        transmit.setFont(font1);
        transmit.setBounds(440, 360, 115, 40);
        panel.add(transmit);
    }

}

3.TCPServer

代码如下:

package tcpCommunication;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPServer implements ActionListener, Runnable {
    private TCPServerFrame serverFrame;
    private ServerSocket server;
    private Socket socket;
    private Thread thread = null;
    private DataInputStream inFromClient;
    private DataOutputStream outToClient;

    public TCPServer() {
        serverFrame = new TCPServerFrame("TCPServer");
        JPanel panel = new JPanel();
        serverFrame.add(panel);

        serverFrame.placeComponents(panel);
        serverFrame.setVisible(true);

        //port:6666
        serverFrame.getMonitor().addActionListener(e -> {
            try {
                int port = Integer.parseInt(serverFrame.getPortField().getText());
                server = new ServerSocket(port);
                serverFrame.getTextArea().append("[Listen] : Listen…\n");
                //获取客户端的socket
                socket = server.accept();
                serverFrame.getTextArea().append("[Accept] : Accept OK.\n");
                inFromClient = new DataInputStream(socket.getInputStream());
                outToClient = new DataOutputStream(socket.getOutputStream());

                thread = new Thread(this);
                //创建子线程处理客户端输入信息
                //new TCPServerThread(socket).start();
                thread.setPriority(Thread.MIN_PRIORITY);
                thread.start();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });

        serverFrame.getTransmit().addActionListener(this);
        System.out.println("TCP服务器准备就绪\n");
    }

    public static void main(String[] args) {
        new TCPServer();
    }

    //向客户端发送数据
    @Override
    public void actionPerformed(ActionEvent e) {
        String send = serverFrame.getSendField().getText();
        new TCPSend(send, outToClient, serverFrame.getTextArea(),
                "Server").sendMessages();
    }

    //使用线程处理服务器端接收客户端的数据
    @Override
    public void run() {
        while (true) {
            try {
                serverFrame.getTextArea().append("[Client send] : " + inFromClient.readUTF() + "\n");
                Thread.sleep(100);
            } catch (IOException | InterruptedException e) {
                //e.printStackTrace();
                System.out.println("服务器与客户端连接中断\n");
                break;
            }
        }

    }
}

4.TCPServerFrame

代码如下:

package tcpCommunication;

import udpCommunication.ReminderTextAdapter;

import javax.swing.*;
import java.awt.*;

public class TCPServerFrame extends JFrame {
    private static JTextField portField;
    private static JTextField sendField;
    private static JTextArea textArea;
    private static JButton monitor;
    private static JButton transmit;

    public TCPServerFrame(String title) {
        setSize(700, 480);
        setLocationRelativeTo(null);//居中显示
        setResizable(false);//设置不允许改变窗体大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle(title);
    }

    public JTextField getPortField() {
        return portField;
    }

    public JTextField getSendField() {
        return sendField;
    }

    public JTextArea getTextArea() {
        return textArea;
    }

    public JButton getMonitor() {
        return monitor;
    }

    public JButton getTransmit() {
        return transmit;
    }

    public void placeComponents(JPanel panel) {
        panel.setLayout(null);

        Font font1 = new Font("宋体", Font.BOLD, 18);
        Font font2 = new Font("宋体", Font.PLAIN, 18);

        //创建JLabel
        JLabel port = new JLabel("Port");
        port.setFont(font1);
        port.setBounds(105, 45, 120, 38);
        panel.add(port);

        JLabel send = new JLabel("Send");
        send.setFont(font1);
        send.setBounds(105, 100, 120, 38);
        panel.add(send);

        JLabel text = new JLabel("Text");
        text.setFont(font1);
        text.setBounds(105, 165, 120, 38);
        panel.add(text);

        //创建文本域
        portField = new JTextField(30);
        portField.setFont(font2);
        portField.addFocusListener(new ReminderTextAdapter(portField, "port:"));
        portField.setBounds(180, 45, 380, 38);
        panel.add(portField);

        sendField = new JTextField(30);
        sendField.setFont(font2);
        sendField.addFocusListener(new ReminderTextAdapter(sendField, "请输入要发送的信息:"));
        sendField.setBounds(180, 100, 380, 38);
        panel.add(sendField);

        textArea = new JTextArea();
        textArea.setFont(font2);
        textArea.setBounds(180, 160, 380, 170);
        textArea.setEditable(false);
        textArea.setLineWrap(false);
        panel.add(textArea);

        //给textArea添加滚动条
        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setBounds(180, 160, 380, 170);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        panel.add(scrollPane);
        scrollPane.setViewportView(textArea);

        //添加两个按钮
        monitor = new JButton("监听");
        monitor.setFont(font1);
        monitor.setBounds(140, 360, 115, 40);
        panel.add(monitor);

        transmit = new JButton("发送");
        transmit.setFont(font1);
        transmit.setBounds(440, 360, 115, 40);
        panel.add(transmit);
    }
}

5.TCPSend

代码如下:

package tcpCommunication;

import javax.swing.*;
import java.io.DataOutputStream;
import java.io.IOException;

public class TCPSend {
    private String send;
    private DataOutputStream dataOutputStream;
    private JTextArea textArea;
    private String sendObject;

    public TCPSend(String send, DataOutputStream dataOutputStream, JTextArea textArea, String sendObject) {
        this.send = send;
        this.dataOutputStream = dataOutputStream;
        this.textArea = textArea;
        this.sendObject = sendObject;
    }

    public void sendMessages() {
        if (send.length() > 0) {
            try {
                dataOutputStream.writeUTF(send);
                dataOutputStream.flush();
                textArea.append("\t\t     [" + sendObject + " send] : " + send + "\n");
            } catch (IOException ex) {
                textArea.append("Your message : “" + send + "”failed to send!\n");
            }
        }
    }

}

6.ReminderTextAdapter(参考别人的,非原创)

代码如下:

package tcpCommunication;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class ReminderTextAdapter extends FocusAdapter {
    //JTextField提示文字通用方法
    JTextField textField;
    String reminderString;

    //初始化
    public ReminderTextAdapter(JTextField textField, String reminderString) {
        this.textField = textField;
        this.reminderString = reminderString;
    }

    //焦点获得
    @Override
    public void focusGained(FocusEvent e) {
        String tempString = textField.getText();
        //String tempString = this.getText();
        if (tempString.equals(reminderString)) {
            textField.setText("");
            textField.setForeground(Color.BLACK);
        }
    }

    //焦点失去
    @Override
    public void focusLost(FocusEvent e) {
        String tempString = textField.getText();
        if (tempString.equals("")) {
            textField.setForeground(Color.GRAY);
            textField.setText(reminderString);
        }
    }

}

总结

该项目简单的实现了TCP客户端和服务器端的通信,但是功能比较简单,页面也比较简洁,没有实现多个客户端互聊的功能等,还有待完善。


刚开始写博客,大家觉得不错的点个赞吧!如果大家有什么建议或者问题的,欢迎评论,我会尽量与大家探讨问题的。

本文地址:https://blog.csdn.net/twj1248445531/article/details/110947510