Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

How to detect in the server code that client socket is closed?

Hi,

I am using TCP socket to communicate between from a client and server.

The client emulates disconnections by aborting the TCP connection after each 10 packets received. The server needs to detect the fact that the clienthas dropped the connection and should go back to accepting new connections. Client will enentually connect back .

I am essentially trying for an explicit way catching some sort of sinal as in C to detect this. In C language, when the server tries to write on the closed socket, the system will send a SIGPIPE to the server process and return an error from write(). One can get a hold of SIGPIPE singal in C, and do the needful.

When the client closes the connection in java, I am having hardtime in detecting it on the server side even when the sever is keep writing to the Outstream of the socket.

I've tried the following.

1. socket.setKeepAlive(true) both on the client and server. It did not help. I think, this is not needed in my case as my server is always keep writing and the connect is not idle any time.

2. socket.setShutDownOutput() and socket.setShutDownIntput() on the client side before closing the socket. This did not help. On the server side, I also checked socket.isOutputShutDown() before writing. This did not help either.

3. I've also checked socket.isBound() , socket.isConnected() and socket.isClosed() on the server side even though they are not meant for this purpose.

4. I've also tried getting the OutputStream from the socket every time I am writing to the Socket to see if it gives an exception.

I essentially need some way (either a method or an exception) on the server side to know that the client has closed the socket.

I've a simple testcase for this.

Server Code:

import java.io.*;
import java.net.*;

public class TestServer {

public static void main(String[] args)
{
try
{
ServerSocket ss = new ServerSocket(0);

System.out.println("Server started on port number : " + ss.getLocalPort());

while(true)
{

Socket s = ss.accept();

for(int i=0; i<25; i++)
{

PrintWriter pw = new PrintWriter(s.getOutputStream());
System.out.println(i + " Hello ");
pw.println(i + " Hello ");
pw.flush();
try{ Thread.sleep(1000); } catch(Exception e){}

}//end of for loop

}//end of while loop

}
catch(Exception e)
{
System.err.println("Server error: " + e);
}
}
}

Client Code:

import java.io.*;
import java.net.*;

public class TestClient
{
public static void main(String[] args) throws IOException
{
try
{
if(args[0] == null)
{
System.out.println("Server port number is not passed");
System.exit(1);
}

Socket socket = null;

InetAddress local = InetAddress.getLocalHost();

while(true)
{

System.out.println("Client initiating connection");

socket = new Socket(local, Integer.parseInt(args[0]));

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

int count = 0;
String temp = null;

while((temp = in.readLine()) != null)
{
count++;
System.out.println("Received : " + temp + " " + count);

if(count == 5)
{
socket.close();
break;
}
}//end of inner while loop

}//end of external while loop


}
catch(Exception e)
{
System.err.println("Server error: " + e);
}
}
}
[4434 byte] By [Varsha1717] at [2007-11-11 8:46:41]