-1

I'm creating a NetUtils class which extends thread to handle socket communications in a GUI without blocking the main thread. My code looks like the following (assume all import are accounted for):

Main method

public static void main(String[] args) {
    EventQueue.invokeLater( () -> {
         new Window().setVisible(true);
    });
}

Window class

public class Window { // normally would extend JFrame bc this is a gui
    // ...
    NetUtils comms;

    public Window() {
        // ...
        comms = new NetUtils("192.168.1.1", 288); // this ip/port info works fine
        comms.start();
        // ...
    }

    // other methods....
}

NetUtils class

public class NetUtils extends Thread {
    private String ip;
    private int port;

    public NetUtils(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    @Override
    public void run() {
        try (Socket socket = new Socket()) {
            socket.connect(new InetSocketAddress(ip, port), 10000); // timeout 10s
            System.out.println("Socket started: " + socket); // correctly prints

            while (true) { // during the life of the thread
                String line = readLine(socket); // throws SocketException here (socket closed error)
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String readLine(Socket socket) {
        // uses inputstream to read bytes and such
        String line;
        boolean isDone = false;
        while (!isDone) {
            try (InputStreamReader isr = new InputStreamReader(socket.getInputStream))) {
                if (isr.ready()) {
                    line += (char) isr.read();
                }
                if (!isr.ready() && line != "") {
                    isDone = true;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return line;
    }
}

What am I doing that would cause the socket to close? I ran the NetUtils code directly in the main method (I didnt separate out the readLine method) and it ran as I expected it to which lead me to believe the problem has to do with the socket being in a thread. Thanks for the help.

Mike
  • 175
  • 1
  • 13
  • Most probably remote server closes connection – hoaz Apr 17 '18 at 20:28
  • Define 'this part works'. NB You aren't checking for end of stream. Do that. @hoaz A remote close would cause `readLine()` to return null, not a 'socket closed' exception. – user207421 Apr 17 '18 at 23:08

1 Answers1

0

Clearly 'this part works' is closing the socket or its input or output stream.

NB You aren't checking for end of stream in the code you posted. I don't see the need for the readLine() method. Just replace your loop with this:

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
    System.out.println(line);
}
reader.close();

[exception handling omitted.]

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Yes it does. I ran `readLine()` earlier directly in a main method like i mentioned earlier, but i didn't exactly copy it to my thread code. And unfortunately when I tried to use a BufferedReader, I wasn't getting back the string I was expecting so I made my own solution. I'll update my question to reflect the problem. I apologize for being incomplete. – Mike Apr 18 '18 at 00:49
  • Re your updated `readLine()` method, there are few correct uses of `ready()`, and this isn't one of them. And this method does not read a *line* at all. Also you're continually recreating `BufferedReaders`, which will cause you to lose data. Don't write code like this. – user207421 Apr 18 '18 at 01:08