Java - 使用ObjectInputStream监听套接字

时间:2011-08-11 07:04:44

标签: java sockets objectinputstream

好的,我有一个名为'Client'的线程类,每次服务器接受一个连接时它会创建一个新的Client .... run方法监听来自客户端的消息,我正在使用ObjectInputStream ..

   do {            
         ObjectInputStream in = null;

        try {
            in = new ObjectInputStream(socket.getInputStream());
            String message = (String) in.readObject();
            System.out.println(message);
            }
            catch (ClassNotFoundException ex) {
                isConnected = false;
                System.out.println("Progoramming Error");
            }
            catch (IOException ex) {
                isConnected = false;
                System.out.println("Server ShutDown");
                System.exit(0);
            }
    } while(isConnected);

我遇到的问题是,为什么每次循环时都必须创建一个新的ObjectInputStream ...如果我在循环结束时关闭输入流并再次循环另一条消息我将收到错误...请一些帮助

1 个答案:

答案 0 :(得分:7)

仅为客户端连接创建一次ObjectInputStream(在循环外部),然后将readObject方法放入循环中。

这是一个有效的测试课程:

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();
                }
            }
        }
    }

}

在此示例中,字符串通过ObjectStream发送。如果您获得ClassNotFoundException(http://download.oracle.com/javase/6/docs/api/java/io/ObjectInputStream.html#readObject())并使用独立的客户端和服务器程序,那么您可能会检查是否客户端和服务器都有要在其类路径中发送的对象的类。