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

