java多线程 TCP服务器接收消息并回执

控制台可以看出,每个用户分配一个线程,客户端发送消息,服务器收到消息并且如果成功则回执“接收成功”。

否则回执失败。

Client.java

package thread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.*;
import java.net.Socket;

public class Client extends JFrame
{
    static Client ui;
    private JButton st, ex; //按钮
    private static JTextArea text, mes;
    public Client()
    {
        super("Client");
        setSize(670, 300);
        setLocationRelativeTo(null);//居中
        draw();
    }
    public void draw()
    {
        setLayout(null);
        JLabel name = new JLabel("主机:127.0.0.1", JLabel.CENTER);
        name.setFont(new Font("隶书", Font.PLAIN, 20));
        name.setBounds(0, 0, 200, 50);
        JLabel port = new JLabel("端口号:9999", JLabel.CENTER);
        port.setFont(new Font("隶书", Font.PLAIN, 20));
        port.setBounds(200, 0, 120, 50);
        JLabel send = new JLabel("消息", JLabel.CENTER);
        send.setFont(new Font("隶书", Font.PLAIN, 20));
        send.setBounds(150, 50, 120, 50);
        JLabel rec = new JLabel("服务器回执", JLabel.CENTER);
        rec.setFont(new Font("隶书", Font.PLAIN, 20));
        rec.setBounds(480, 50, 120, 50);
        st = new JButton("发送");
        st.setFont(new Font("隶书", Font.PLAIN, 20));
        st.setForeground(Color.BLACK);
        st.setBackground(Color.YELLOW);
        st.setBorderPainted(false);
        ex = new JButton("退出");
        ex.setFont(new Font("隶书", Font.PLAIN, 20));
        ex.setForeground(Color.BLACK);
        ex.setBackground(Color.GREEN);
        ex.setBorderPainted(false);
        st.setBounds(450, 10, 80, 40);
        ex.setBounds(550,10,80, 40);
        text = new JTextArea();
        text.setFont(new Font("隶书", Font.PLAIN, 30));
        text.setBounds(0, 100, 400, 300);
        mes = new JTextArea();
        mes.setFont(new Font("隶书", Font.PLAIN, 30));
        mes.setBounds(440, 100, 220, 300);
        st.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent a) {
                if (a.getButton() == a.BUTTON1) {
                    try
                    {
                        Thread w = new Thread();
                        w.start();
                        send();
                    } catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
            public void mousePressed(MouseEvent a) {}
            public void mouseReleased(MouseEvent a) {}
            public void mouseEntered(MouseEvent a) {}
            public void mouseExited(MouseEvent a) {}
        });
        ex.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent a) {
                if (a.getButton() == a.BUTTON1) {
                    System.exit(0);
                }
            }
            public void mousePressed(MouseEvent a) {}
            public void mouseReleased(MouseEvent a) {}
            public void mouseEntered(MouseEvent a) {}
            public void mouseExited(MouseEvent a) {}
        });
        add(name);
        add(port);
        add(st);
        add(ex);
        add(send);
        add(text);
        add(rec);
        add(mes);
        setVisible(true);
    }

    public static void send() throws IOException
    {
        String show = ui.text.getText();
        Socket s = new Socket("127.0.0.1", 9999);
        DataOutputStream out = new DataOutputStream(s.getOutputStream());
        DataInputStream in = new DataInputStream(s.getInputStream());
        out.writeUTF(show);
        mes.setText(in.readUTF());
        out.close();
        s.close();
    }
    public static void main(String[] args) throws IOException, IOException
    {
        ui = new Client();
    }
}

 ServerThread.java

package thread;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

class ServerThread extends Thread
{
    private Socket s = null;
    public String res;
    private int cnt;
    public ServerThread(Socket s, int cnt)
    {
        this.s = s;
        this.cnt = cnt;
        start();
    }
    public void run()
    {
        System.out.println("当前线程: " + Thread.currentThread().getId() + " 当前用户: " + cnt);
        try
        {
            DataInputStream in = new DataInputStream (s.getInputStream());
            DataOutputStream out = new DataOutputStream(s.getOutputStream());
            this.res = in.readUTF() + "\n";
            if (this.res.length() == 1)
                out.writeUTF("发送失败");
            else
                out.writeUTF("发送成功");
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

TCPServer.java

package thread;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

class TcpServer implements Runnable
{
    private static int cnt;
    public String res;
    public ServerSocket ss;
    public void run()
    {
        try
        {
            ++cnt;
            Socket s = ss.accept();
            ServerThread st = new ServerThread(s, cnt);
            st.join();
            this.res = st.res;
        } catch (IOException | InterruptedException e)
        {
            e.printStackTrace();
        };

    }
}

 UI.java

package thread;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.net.ServerSocket;

public class UI extends JFrame
{
    public String res;
    private static ServerSocket ss;
    public static JTextArea text;
    public UI() throws IOException, InterruptedException
    {
        super("TcpServe");
        setSize(970, 600);
        setLocationRelativeTo(null);//居中
        draw();
    }
    public void draw() throws IOException, InterruptedException
    {
        setLayout(null);
        JLabel name = new JLabel("服务器:admin", JLabel.CENTER);
        name.setFont(new Font("隶书", Font.PLAIN, 20));
        name.setBounds(0, 0, 120, 50);
        JLabel port = new JLabel("端口号:9999", JLabel.CENTER);
        port.setFont(new Font("隶书", Font.PLAIN, 20));
        port.setBounds(160, 0, 120, 50);
        JButton st, ex; //按钮
        st = new JButton("启动");
        st.setFont(new Font("隶书", Font.PLAIN, 20));
        st.setForeground(Color.BLACK);
        st.setBackground(Color.YELLOW);
        st.setBorderPainted(false);
        ex = new JButton("退出");
        ex.setFont(new Font("隶书", Font.PLAIN, 20));
        ex.setForeground(Color.BLACK);
        ex.setBackground(Color.GREEN);
        ex.setBorderPainted(false);
        st.setBounds(750, 10, 80, 40);
        ex.setBounds(850,10,80, 40);
        text = new JTextArea();
        text.setFont(new Font("隶书", Font.PLAIN, 30));
        text.setBounds(0, 80, 970, 500);
        add(name);
        add(port);
        add(st);
        add(ex);
        add(text);
        st.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent a) {
                if (a.getButton() == a.BUTTON1) {
                        System.out.println("已启动");
                }
            }
            public void mousePressed(MouseEvent a) {}
            public void mouseReleased(MouseEvent a) {}
            public void mouseEntered(MouseEvent a) {}
            public void mouseExited(MouseEvent a) {}
        });
        ex.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent a) {
                if (a.getButton() == a.BUTTON1) {
                    System.exit(0);
                }
            }
            public void mousePressed(MouseEvent a) {}
            public void mouseReleased(MouseEvent a) {}
            public void mouseEntered(MouseEvent a) {}
            public void mouseExited(MouseEvent a) {}
        });
        setVisible(true);
        execute();
    }
    public void execute() throws IOException, InterruptedException
    {
        TcpServer now = new TcpServer();
        ss = new ServerSocket(9999);
        StringBuffer pr = new StringBuffer();
        int cnt = 1;
        while (true)
        {
            now.ss = ss;
            now.run();
            if (now.res.length() == 1)
                continue;
            pr.append("用户" + String.valueOf(cnt) + ":" + now.res);
            ++cnt;
            text.setText(String.valueOf(pr));
        }
    }
    public static void main(String[] args) throws IOException, InterruptedException
    {
        UI now = new UI();
    }
}

 

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值