简体   繁体   中英

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

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. 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. 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. And you are throwing away a line of data with the first readLine() call: remove it and just leave the one in the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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