Problem when client closes

Here is my server side:

import java.net.*;

import java.io.*;

publicclass server{

privatestatic ServerSocket serversocket =null;

publicstaticvoid main(String[] args){

//ServerSocket serversocket = null;

try{

serversocket =new ServerSocket(11116);

}catch (IOException e){

System.out.println(e);

}

while(true){

ClientWorker clientworker;

try{

clientworker =new ClientWorker(serversocket.accept());

Thread thread =new Thread(clientworker);

thread.start();

}catch(IOException e){

System.out.println(e);

System.exit(-1);

}

}

}

protectedvoid finalize(){

try{

serversocket.close();

}catch (IOException e){

System.out.println("Could not close socket");

System.exit(-1);

}

}

}

class ClientWorkerimplements Runnable{

private Socket socket;

public ClientWorker(Socket socket){

this.socket = socket;

}

publicvoid run(){

String strLine;

BufferedReader input =null;

PrintWriter output =null;

try{

input =new BufferedReader(new InputStreamReader(socket.getInputStream()));

output =new PrintWriter(socket.getOutputStream(),true);

}catch (IOException e){

System.out.println(e);

}

while(true){

try{

strLine = input.readLine();

output.println(strLine);

System.out.println(strLine);

}catch (IOException e){

System.out.println(e);

System.exit(-1);

}

}

}

}

and my client side

import java.net.*;

import java.io.*;

publicclass client{

publicstaticvoid main(String[] args){

Socket socket =null;

BufferedReader input =null;

PrintWriter output =null;

String strServer ="192.168.0.3";

String strLine ="";

try{

socket =new Socket(strServer, 11116);

input =new BufferedReader(new InputStreamReader(socket.getInputStream()));

output =new PrintWriter(socket.getOutputStream(),true);

}catch(UnknownHostException e){

System.out.println("Couldn't get I/O for the connection to: " + strServer);

}catch(IOException e){

System.out.println(e);

}

BufferedReader stdIn =new BufferedReader(new InputStreamReader(System.in));

if(socket!=null && input!=null && output!=null){

while(!strLine.equals("quit")){

try{

System.out.print("Output: ");

strLine = stdIn.readLine();

output.println(strLine);

System.out.println("Server: " + input.readLine());

}catch(UnknownHostException e){

System.out.println("Couldn't get I/O for the connection to: " + strServer);

}catch(IOException e){

System.out.println(e);

}

}

}

try{

output.close();

input.close();

socket.close();

}catch(UnknownHostException e){

System.out.println("Couldn't get I/O for the connection to: " + strServer);

}catch(IOException e){

System.out.println(e);

}

}//end main method

}

Seems like everytime I close my client, the server crashes which is "java.net.SocketException: Connection reset"

A little info about the program, it's just a multi-threaded server where clients can connect to it.

How do i fix it?

Thanks

[7744 byte] By [Kero_1116a] at [2007-11-26 13:14:08]
# 1
This happens because you are writing to the client after the client has closed its socket.This in turn happens because you aren't recognizing when the client has done that.Check the return values of BufferedReader.readLine().
ejpa at 2007-7-7 17:33:09 > top of Java-index,Archived Forums,Socket Programming...
# 2
How can i fix this?
Kero_1116a at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...
# 3
For example you can check string against "quit" on server side as well as on clients side.Or you can catch SocketException (Or just remove System.exit() call) and make simple closing connection and thread passing down instead.Many ways actually.
Michael.Nazarov@sun.coma at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...
# 4

> This happens because you are writing to the client after the client has closed its socket.

Really? :)

strLine = stdIn.readLine(); // user

output.println(strLine); // send to server

System.out.println("Server: " + input.readLine()); // read from server

...now back to loop, check and exit

So this happens because you are reading from the client after the client has closed its socket.

Michael.Nazarov@sun.coma at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...
# 5
I fixed it thanks to you guys.All I did was remove the system exit.I was wondering how to detect clients that d/c
Kero_1116a at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...
# 6

> Really? :)

> strLine = stdIn.readLine(); // user

> output.println(strLine); // send to server

Yes, really. That 'strLine' is null if the client has closed. The OP is not checking for this condition so he is writing the null after the client has already closed, and this happens in a hard loop from which there is no escape until the exception.

> So this happens because you are reading from the client after the client has closed its socket.

That would cause readLine() to return a null. It wouldn't cause an IOException.

Message was edited by:

ejp

ejpa at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...
# 7
You should review code again. I wrote about client side and strLine can not be null - if client closed connection then stdIn.readLine() will not call again.
Michael.Nazarov@sun.coma at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...
# 8

OK, so I quoted the wrong code, but the problem is still at the server side. The server is not recognizing when the client has closed the socket and is continuing to write "null" in a hard loop to the client. Eventually this will cause a 'connection reset' exception. Reading from a closed socket does not produce a connection reset exception, it produces -1, an EOFException, or in the case of readLine() a null.

ejpa at 2007-7-7 17:33:10 > top of Java-index,Archived Forums,Socket Programming...