I have a server which accepts input from client; it reads text input from client and processes it in some way(each new connection is assigned a thread also). However, whenever a client suddenly disconnects from the server I receive a socketException(connection reset) from the server where BufferedReader.readLine()
is invoked. So basically this exception is an indicator that a client was suddenly terminated.
So in this case could I just ignore the exception occurring at the server and simply just close that socket? I'm just wondering because it's not very usual in java to just "ignore" exceptions, however I haven't found any other solutions other than just to take no notice of the exception.
error:
java.net.SocketException: Connection reset
Error occurs whenever we read data from inputstream:
@Override
public void run() {
try (
Socket sock = socket;
PrintWriter out = new PrintWriter(sock.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
){
String receivedData;
while((receivedData = in.readLine()) != null){
out.println(receivedData);
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Client disconnected: " + socket);
}
}