Socket BufferedReader IOException only thrown in Windows and not Linux?


Socket BufferedReader IOException only thrown in Windows and not Linux?



The project is a chat program with a multi-threaded server to handle multiple clients. The code below is server-side for the thread that begins once a client connects. The ConnectionHandler takes a connected clients socket and 'ID'.



The static "broadcast" method called in run() is not relevant to the question as well as both functions in the finally clause.



I have been working on the project on both a Windows 10 desktop, and an XPS laptop running Ubuntu 18.04.



Client disconnection is handled once the IOException is caught in the run() method. This works as designed on Windows, however for Ubuntu that is not the case. When exiting the GUI client it seems like only the exception is caught on Windows and doesn't happen on Ubuntu. Thus rendering the server non-working for Linux and working perfectly on Windows.



Is this not a well practiced method to handle a client disconnection in general? Or is it something else that I need to change in order for this to work on Linux? I have no idea what to do because I'm not sure if it's an error in my code or it's just the way Linux handles Java versus Windows?



Any help or tips is appreciated. Thanks in advance.


class ConnectionHandler implements Runnable
{

//Begin declarations for new client thread
int clientId;
Socket connection;
BufferedReader reader;
BufferedWriter writer;
String message;
//end declarations for new client thread

public ConnectionHandler(int clientId, Socket connection)
{
this.clientId = clientId;
this.connection = connection;
}

@Override
public void run()
{

try
{
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
}
catch (IOException e)
{
e.printStackTrace();
}


try
{
while (true)
{
message = reader.readLine();

if (message != null)
{

System.out.println("Message from " + message);

CrosstalkServer.broadcast(message);

}
}
}
catch (IOException e)
{
System.out.println("Client has disconnected. Recycling client ID: " + clientId);
}
finally
{
//Close the reader, writer, and socket.
terminateClient();

//Client has disconnected, handle client count and recycle client ID
CrosstalkServer.recycleClientId(this);

}

}



Edit: I tested the program on Windows 10, Windows 7, and Ubuntu 18.04. After some testing, it is the client side causing the problem. I ran the server on my Ubuntu laptop, and tested a Windows 7 client along with a Windows 10 clients. The Windows 7 client did not cause an IOException to be thrown, while Windows 10 did. Ubuntu client-side does not cause the IOException either.. What is a better way to determine a client disconnection? Start a thread to monitor by attempting to constantly write to each client in my client arraylist?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV