简体   繁体   English

同时关闭服务器和客户端套接字连接

[英]Close server and client socket connection at the same time

I have a created a client and server using sockets, Hangman game where the client sends the letter input to the server and so on. 我已经创建了一个使用套接字的客户端和服务器,Hangman游戏,其中客户端将字母输入发送到服务器等等。 When the player wins or loses, the server terminates. 当玩家赢或输时,服务器终止。 I want the client to terminate at the same time as well before displaying "Enter letter: ". 我希望客户端在显示“输入字母:”之前也要同时终止。 Is there a way of doing this? 有办法吗?

Here is a part of my client code: 这是我的客户代码的一部分:

do {
        System.out.println();
        System.out.print("Enter letter: "); 
        BufferedReader UserIn = new BufferedReader(new InputStreamReader(System.in));
        input = UserIn.readLine();
        pw.println(input);
        System.out.println(SocketIn.readLine());
        System.out.println();

        while(SocketIn.ready()) {
            System.out.println(SocketIn.readLine());
        }   

    } while (socket.isConnected());

Get rid of the ready() test and recode your loop like this: 摆脱ready()测试并像这样重新编码循环:

String line;
while ((line = SocketIn.readLine()) != null)
{
    System.out.println(line);
}

ready() can't tell you about end of stream. ready()无法告诉您流结束。 Neither does isConnected() . isConnected()也没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM