Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么套接字不';读取用户输入的内容一次后是否关闭?_Java_Multithreading_Sockets - Fatal编程技术网

Java 为什么套接字不';读取用户输入的内容一次后是否关闭?

Java 为什么套接字不';读取用户输入的内容一次后是否关闭?,java,multithreading,sockets,Java,Multithreading,Sockets,我在聊天应用程序的服务器中使用以下代码,其中我为每个客户端使用一个seprate线程。 代码: public void run() { String msg; InputStreamReader isReader = new InputStreamReader(sock.getInputStream()) BufferReader r= new BufferedReader(isReader); int i=0,j=0; System.out.print("entered run again"+

我在聊天应用程序的服务器中使用以下代码,其中我为每个客户端使用一个seprate线程。 代码:

public void run()
{
String msg;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream())
BufferReader r= new BufferedReader(isReader);
int i=0,j=0;
System.out.print("entered run again"+i++);
try{
             while((msg=r.readLine())!=null)
    {
        System.out.println("readed"+msg );

            spreadMsg(msg);

    }


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


 }


}   

为什么while循环“while((msg=r.readLine())!=null)”是无限循环,为什么它在读取了我输入的唯一一行之后不返回。

Socket.getInputStream()返回的流在客户端关闭套接字之前不会返回null

为什么while循环“while((msg=r.readLine())!=null)”是无限循环,为什么它在读取了我输入的唯一一行之后不返回

另一端没有关闭连接,因此读者不知道您只发送一条线路


如果您想假设只发送一条线路,您可以自己关闭连接,而不是进行循环。

我知道我缺少一些概念,请回答为什么应该关闭?无限循环是代码中的正常行为。我认为它应该在一次读取了所有输入的内容之后,就像正常读取字符串一样。但是现在它得到了一个点,即只有在关闭sockett时才会返回null。由
Socket.getInputStream()
”返回的“流”根本不会“返回null”。它要么返回非null值,要么抛出异常。如果您所指的是
readLine()
方法,则需要明确说明。