TCP通信实现:多发多收消息、实现可以同时接收多个客户端

客户端

package com.csdn.d5_socket2;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 目标:完成 Socket  客户端开发,实现 多发 和 多收
 */
public class ClientDemo1 {
    public static void main(String[] args) {

        try{
            System.out.println("===客户端启动===");
            //1、创建Socket通信管道请求有服务端的连接
            Socket socket = new Socket("127.0.0.1", 7777);
            //2、从socket通信管道中得到一个字节输出流 负责发送数据
            OutputStream os = socket.getOutputStream();
            //3、把低级的字节流包装成打印流
            PrintStream ps = new PrintStream(os);

            Scanner sc = new Scanner(System.in);
            while (true) {
                System.out.println("请说:");
                String msg = sc.nextLine();
                //4、发送消息
                ps.println(msg);
                ps.flush();
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

 服务端

package com.csdn.d5_socket2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 目标:实现 服务端 可以同时处理 多个客户端 的消息
 */
public class ServerDemo2 {
    public static void main(String[] args) {

        try {
            System.out.println("===服务端启动成功===");
            //1、注册端口
            ServerSocket serverSocket = new ServerSocket(7777);
            //a.定义一个死循环由主线程负责不断的接收客户端的Socket管道连接
            while (true) {
                //2、每接收到一个客户端的Socket管道,交给一个独立的子线程负责读取消息
                Socket socket = serverSocket.accept();
                //上线逻辑
                System.out.println(socket.getRemoteSocketAddress() + "它来了,上线了!");
                //3、开始创建独立线程处理socket
                new ServerReaderThread(socket).start();

            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

class ServerReaderThread extends Thread {
    private Socket socket;

    public ServerReaderThread(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        try {
            //3、从socket通信管道中得到一个字节输入流
            InputStream is = socket.getInputStream();
            //4、把字节输入流包装成缓冲字符输入流进行消息的接收
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            //5、按照行读取消息
            String msg;
            while ((msg = br.readLine()) != null) {
                System.out.println(socket.getRemoteSocketAddress() + "说了:" + msg);
            }
        } catch (IOException e) {
            System.out.println(socket.getRemoteSocketAddress() + "下线了!!!");
        }
    }
}

你可能感兴趣的:(#,网络编程,tcp/ip,网络协议)