-
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:
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:
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);
}
}
}
-
hello
Socket s=socket.accept();
try{
Thread input = new InputThread(s.getInputStream());
input.start();
Thread output
= new OutputThread(s.getOutputStream());
output.start();
// wait for output and input to finish
try {
input.join();
output.join();
}catch(Exception e){}
}catch(Exception e){}
/*
class InputThread extends Thread {
InputStream in;
public InputThread(InputStream in) {
this.in = in;
}
public void run() {
try {
while (true) {
int i = in.read();
if (i == -1) break;
System.out.write(i);
}
}
catch (SocketException ex) {
// output thread closed the socket
}
catch (IOException ex) {
System.err.println(ex);
}
try {
in.close();
}
catch (IOException ex) {
}
}
}
*/
/*
class OutputThread extends Thread {
private Writer out;
public OutputThread(OutputStream out) {
this.out = new OutputStreamWriter(out);
}
public void run() {
String line;
BufferedReader in
= new SafeBufferedReader(new InputStreamReader(System.in));
try {
while (true) {
line = in.readLine();
if (line.equals(".")) break;
out.write(line +"\r\n");
out.flush();
}
}
catch (IOException ex) {
}
try {
out.close();
}
catch (IOException ex) {
}
}
}
*/
/*
import java.io.*;
public class SafeBufferedReader extends BufferedReader {
public SafeBufferedReader(Reader in) {
this(in, 1024);
}
public SafeBufferedReader(Reader in, int bufferSize) {
super(in, bufferSize);
}
private boolean lookingForLineFeed = false;
public String readLine() throws IOException {
StringBuffer sb = new StringBuffer("");
while (true) {
int c = this.read();
if (c == -1) { // end of stream
if (sb.length() == 0) return null;
return sb.toString();
}
else if (c == '\n') {
if (lookingForLineFeed) {
lookingForLineFeed = false;
continue;
}
else {
return sb.toString();
}
}
else if (c == '\r') {
lookingForLineFeed = true;
return sb.toString();
}
else {
lookingForLineFeed = false;
sb.append((char) c);
}
}
}
}
*/
please insert this code,I think ,it gives answer ur question
Last edited by desilva; 06-03-2008 at 07:06 AM.
Similar Threads
-
By Phil Weber in forum .NET
Replies: 632
Last Post: 10-01-2003, 12:00 AM
-
Replies: 4
Last Post: 04-03-2003, 10:54 AM
-
By Noordeen in forum ASP.NET
Replies: 0
Last Post: 08-12-2002, 02:10 AM
-
By Chris in forum Enterprise
Replies: 0
Last Post: 02-05-2002, 01:00 PM
-
By John Greenwood in forum VB Classic
Replies: 0
Last Post: 08-23-2000, 04:49 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks