zoukankan      html  css  js  c++  java
  • 多线程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这个方法是以
    作为界定符的,由于发送消息的那一端用的是
                                       //PrintWriter的write()方法,这个方法并没加上
    ,所以会一直等待
                //回复客户端
                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

  • 相关阅读:
    jmeter参数化关联
    电商
    mysql联查
    购物车
    冒泡排序、二分查找、选择排序、斐波那契
    python数据转换/9*9表/for循环
    python三角形
    mysql语句
    Selenium 8
    Selenium 7
  • 原文地址:https://www.cnblogs.com/ch94/p/10156143.html
Copyright © 2011-2022 走看看