在服务器-客户端套接字通信中为多个客户端创建ClientHandler

时间:2019-06-21 08:48:13

标签: android sockets server client

我正在使用Android Studio,并且能够成功在服务器和1个客户端之间进行通信。我知道我需要创建一个ClientHandler类,并在服务器类中为每个连接的新客户端创建一个新线程,但是当我尝试执行此操作时,我无法正确实现它。我希望服务器与多台设备通信,大约为4。

服务器

public class ServerClass extends Thread {
        Socket clientSocket;
        ServerSocket serverSocket;


        @Override
        public void run() {
            while (true) {
                try {
                    //server is listening on port 8888
                    serverSocket = new ServerSocket(8888);

                        //getting a socket for the client connected
                        clientSocket = serverSocket.accept();
                        Log.d(TAG, "Connected Clients in for loop: "+clientSocket.getPort());
                        //need to start a new thread to handle this client


                        //add client to array list

                    Log.d(TAG, "clients array list size: "+clientList.size());

                        //thread that handles sending and receving data
                       sendReceiveClass = new SendReceive(clientSocket);
                        sendReceiveClass.start();


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

客户

 public class ClientClass extends Thread {
        Socket socket;
        String hostAddress;

        public ClientClass (InetAddress hostAddress) {
            this.hostAddress = hostAddress.getHostAddress();
            socket = new Socket();
        }



        @Override
        public void run() {
            while (true) {
                try {
                    socket.connect(new InetSocketAddress(hostAddress, 8888), 500);
                    sendReceiveClass = new SendReceive(socket);
                    sendReceiveClass.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        }

发送/接收线程

 public class SendReceive extends Thread {
        private Socket socket;
        private InputStream inputStream;
        private OutputStream outputStream;


            public SendReceive (Socket socket) {
            this.socket = socket;
            try {
                inputStream = socket.getInputStream();
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

            @Override
            public void run() {
                byte[] buffer = new byte[1024];
                int bytes;

                while (socket !=null) {
                    try {
                        bytes = inputStream.read(buffer);
                        if (bytes > 0) { //means we have a message
                            handler.obtainMessage(MESSAGE_READ,bytes,-1,buffer).sendToTarget();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

            public void write(byte[] bytes) {
                try {
                    outputStream.write(bytes);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

0 个答案:

没有答案