Socket多线程通信

服务端:

package org.example;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class ServerThread implements Runnable {

    public Socket socket;

    public ServerThread (Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true) {
                String str = br.readLine();
                System.out.println(str);
                PrintWriter pw = new PrintWriter(socket.getOutputStream());
                BufferedReader out=new BufferedReader(new InputStreamReader(System.in));
                str = out.readLine();
                pw.println(str);
                pw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

package org.example;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SocketService {
    public static void main(String args[])throws Exception {
        ServerSocket serverSocket = new ServerSocket(5208);
        System.out.println("服务器启动成功");
        Map stringMap=new HashMap<>();
        int i=1;
        while (true) {
            Socket socket= serverSocket.accept();
            System.out.println("上线通知: " + socket.getInetAddress() + ":" +socket.getPort());
            stringMap.put(i,socket);
            System.out.println(socket);
            new Thread(new ServerThread(socket)).start();
            i=i+1;
        }
    }
}

客户端1:

package org.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient{
    public static void main(String args[])throws Exception{
        Socket socket = new Socket("10.231.102.138", 5208);
        System.out.println("客户端1连接成功");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        while(true){
            pw.println("客户端1说:"+br.readLine());
            pw.flush();
            System.out.println("服务器说:"+in.readLine());
        }
    }
}

 客户端2:

package org.example;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient2 {
    public static void main(String args[])throws Exception{
        Socket socket = new Socket("10.231.102.138", 5208);
        System.out.println("客户端2连接成功");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw = new PrintWriter(socket.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        while(true){
            pw.println("客户端2说:"+br.readLine());
            pw.flush();
            System.out.println("服务器说:"+in.readLine());
        }
    }
}

你可能感兴趣的:(网络,开发语言,java)