简体   繁体   English

BufferedReader从套接字读取

[英]BufferedReader reading from socket

I have a bufferedreader and for some reason it wont read the text from the print stream I am sending from my client. 我有一个缓冲读取器,由于某种原因,它不会从我从客户端发送的打印流中读取文本。 This is the point at which it fails every time the line = in.readline 这是每次line = in.readline时失败的点

Also I have checked and the server is connected. 我也检查过,服务器已连接。

This is the error 这是错误

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:168)
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
    at java.io.InputStreamReader.read(InputStreamReader.java:167)
    at java.io.BufferedReader.fill(BufferedReader.java:136)
    at java.io.BufferedReader.readLine(BufferedReader.java:299)
    at java.io.BufferedReader.readLine(BufferedReader.java:362)
    at Server.ServerListener$getXML.run(ServerListener.java:82)
    at java.lang.Thread.run(Thread.java:662)

Thanks in advance 提前致谢

       BufferedReader in = new BufferedReader(new       InputStreamReader(server.getInputStream()));
            //PrintStream out = new PrintStream(server.getOutputStream());
            System.out.println("Start");
            //read the xml
           boolean connected = server.isConnected();
            System.out.println("xml: "+ connected);
            line = in.readLine();
            System.out.println("Postread");
            while ((line = in.readLine()) != null) {               
                System.out.println("while1");
                xml = xml + line;
                System.out.println("while2");
            }`

isConnected() tells you whether your socket is connected to the connection, not whether the connection is still connected to the peer. isConnected()告诉您套接字是否已连接到连接,而不是连接是否仍连接到对等方。 Obviously you aren't still connected at all. 显然你根本没有联系。 'Connection reset' has several possible causes: you wrote to a connection that had already been closed by the other end (application protocol error); “连接重置”有几个可能的原因:您写入已被另一端关闭的连接(应用程序协议错误); the other end aborted the connection; 另一端中断了连接; the local TCP stack has encountered network errors sending and has given up. 本地TCP堆栈遇到网络错误发送并已放弃。 First of those is then most likely suspect. 其中第一个很可能是怀疑。 And don't use PrintStreams/Writers across the network, as they swallow exceptions you need to know about. 并且不要在网络中使用PrintStreams / Writers,因为它们会吞下您需要了解的异常。 And you are throwing away a line of data with the first readLine() call: remove it and just leave the one in the loop. 并且您正在使用第一个readLine()调用丢弃一行数据:将其删除并将其保留在循环中。

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

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