服务器/客户端迭代服务器

时间:2019-06-27 15:17:24

标签: java sockets server

如何使服务器迭代?在我的书中,我建议在一会儿(true)内插入accept()和对客户端的响应。我该如何解决?我的书对我没有帮助。

这是服务器类:

public class ServerHelloWorld { 
    static Logger logger = Logger.getLogger("global");

    public static void main(String[] args) {
         try {
             ServerSocket serverSocket = new ServerSocket (9000); 
             logger.info("Socket istanziato, accetto connessioni,..");
             Socket socket = serverSocket.accept();
             logger.info("Accettata una connessione... attendo comandi.");
             ObjectOutputStream outStream = new ObjectOutputStream (socket.getOutputStream());

             ObjectInputStream inStream = new ObjectInputStream (socket.getInputStream());
             String nome = (String) inStream.readObject();

             logger.info("Ricevuto:" + nome);

             outStream.writeObject("Hello "+ nome);

             socket .close();

         } catch (EOFException e) { 
             logger.severe("Problemi con la connessione:" + e.getMessage());
             e.printStackTrace();
         } catch (Throwable t) { 
             logger.severe("“Lanciata Throwable:" + t.getMessage());
             t.printStackTrace();
         }
    }
}

这是客户端类:

public class ClientHelloWorld {
    static Logger logger = Logger.getLogger("global");

    public static void main(String args[]) {
        try {
            Socket socket = new Socket ("localhost", 9000);
            ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
            ObjectInputStream in = new ObjectInputStream(socket.getInputStream());

            out.writeObject("Giovanni"); 
            System.out.println(in.readObject());
            socket .close();

        } catch (EOFException e) { 
            logger.severe("Problemi con la connessione:" + e.getMessage());
            e.printStackTrace();
        } catch (Throwable t) {
            logger.severe("Lanciata Throwable:" + t.getMessage());
            t.printStackTrace();
        }
    }
}

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您只需要在接受客户端并读/写消息的位置循环代码的一部分。 while(true)允许您永远循环,或者直到使用诸如breakreturn之类的控制语句,或者抛出异常为止

try {
    ServerSocket serverSocket = new ServerSocket (9000); 
    logger.info("Socket istanziato, accetto connessioni,..");
    while(true) {
        Socket socket = serverSocket.accept();
        logger.info("Accettata una connessione... attendo comandi.");
        ObjectOutputStream outStream = new ObjectOutputStream (socket.getOutputStream());

        ObjectInputStream inStream = new ObjectInputStream (socket.getInputStream());
        String nome = (String) inStream.readObject();

        logger.info("Ricevuto:" + nome);

        outStream.writeObject("Hello "+ nome);

        socket .close();
    }
} catch (EOFException e) { 
    logger.severe("Problemi con la connessione:" + e.getMessage());
    e.printStackTrace();
} catch (Throwable t) { 
    logger.severe("“Lanciata Throwable:" + t.getMessage());
    t.printStackTrace();
}