Java - Listening to a socket with ObjectInputStream

前端 未结 1 1899
迷失自我
迷失自我 2020-12-18 01:31

Ok so , i have a thread class called \'Client\' every time the server accepts a connection it creates a new Client....The run method listens for messages from the client and

1条回答
  •  自闭症患者
    2020-12-18 02:07

    Only create the ObjectInputStream once (outside the loop) for a client connection, then put the readObject method into the loop.

    Here's a working test class:

    public class TestPrg {
    
        public static void main(String... args) throws IOException {
            ServerListener server = new ServerListener();
            server.start();
    
            Socket socketToServer = new Socket("localhost", 15000);
            ObjectOutputStream outStream = new ObjectOutputStream(socketToServer.getOutputStream());
    
            for (int i=1; i<10; i++) {
                try {
                    Thread.sleep((long) (Math.random()*3000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Sending object to server ...");
                outStream.writeObject("test message #"+i);
            }
            System.exit(0);
    
        }
    
        static class ServerListener extends Thread {
    
            private ServerSocket serverSocket;
    
            ServerListener() throws IOException {
                serverSocket = ServerSocketFactory.getDefault().createServerSocket(15000);
            }
    
            @Override
            public void run() {
                while (true) {
                    try {
                        final Socket socketToClient = serverSocket.accept();
                        ClientHandler clientHandler = new ClientHandler(socketToClient);
                        clientHandler.start();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        static class ClientHandler extends Thread{
            private Socket socket;
            ObjectInputStream inputStream;
    
            ClientHandler(Socket socket) throws IOException {
                this.socket = socket;
                inputStream = new ObjectInputStream(socket.getInputStream());
            }
    
            @Override
            public void run() {
                while (true) {
                    try {
                        Object o = inputStream.readObject();
                        System.out.println("Read object: "+o);
                    } catch (IOException e) {
                        e.printStackTrace();
    
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    }
    

    In this example Strings are sent trough the ObjectStream. If you get the ClassNotFoundException (http://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html#readObject()) and are using an independent client and server program than you might check if both the client and the server have the class of the object to send in their class paths.

    0 讨论(0)
提交回复
热议问题