多线程server与多client通信

鉴于ServerSocket的accept方法是阻塞的,那么只能通过多线程的方式实现多客户端连接与服务器连接

基本步骤:

1,服务端创建ServerSocket绑定端口号,循环调用accept()方法

2,客户端创建一个socket并请求和服务器端连接

3,服务器端接受客户端请求,创建socket与该客户建立连接

4,两个socket在一个单独的线程上通话

5,服务器端继续等待新的连接

//服务端代码  重复调用ServerThread
public class MultithreadingServer {
    public static void main(String[] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(6666);
            System.out.println("服务端启动中....");
            Socket socket = null;
            int i = 0;
            while(true){
                socket = serverSocket.accept();//再来一个客户端就新建一个socket
                ServerThread ts = new ServerThread(socket);
                ts.run();
                i++;
                System.out.println("已有"+ i +"台客户端连接");
                InetAddress address = socket.getInetAddress();//获取客户端的inetaddress对象
                System.out.println("当前主机ip:" + address.getHostAddress());//获取客户端的ip
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
public class ServerThread extends Thread{//服务器线程处理类
    Socket socket = null;
    InputStream is = null;
    BufferedReader br = null;
    OutputStream os = null;
    PrintWriter pw = null;
    
    public ServerThread(Socket socket) {
        this.socket = socket;
    }

    public void run(){
        try {
            is = socket.getInputStream();//获取字节流
            br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while((info = br.readLine()) != null){
                System.out.println("客户端传递的信息" + info);
            }
            socket.shutdownInput();//必须要及时关闭,因为readline这个方法是以\r\n作为界定符的,由于发送消息的那一端用的是
                                   //PrintWriter的write()方法,这个方法并没加上\r\n,所以会一直等待
            //回复客户端
            os = socket.getOutputStream();
            pw = new PrintWriter(os);
            pw.write("你好!");
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(pw != null){
                    pw.close();
                }
                if(br != null){
                    br.close();
                }
                if(os != null){
                    os.close();
                }
                is.close();//关闭返回的 InputStream 将关闭关联套接字。 
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
//客户端代码  举个例子  大致相同
public class Client1 {
    
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("localhost", 6666);
            System.out.println("客户端启动中....");
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.write("admin");
            pw.flush();
            socket.shutdownOutput();
            
            InputStream is = socket.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String info = null;
            while((info = br.readLine()) != null){
                System.out.println("服务端返回的信息" + info);
            }
            
            os.close();
            pw.close();
            is.close();
            br.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

详细可参考:http://www.cnblogs.com/yjbjingcha/p/8268317.html

posted @ 2018-12-21 15:44  陳丶  阅读(693)  评论(0编辑  收藏  举报