How to find out one side has closed socket?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   southnorth_pole
Posted On:   Wednesday, September 26, 2001 09:22 AM

I want the server writes a lot of data to client. While client receiving the data, it closes its socket. At the server side, there is on exception occurs. So how can the server find out that the client has closed the socket? Here is what I tested: Server side: (PrintWriter pw: getting from socket output steam) try{ ... int k=1; while(k <5000){ pw.println(k); pw.flush(); } ... } catch (Exception e){ ... } client side: BufferedReader br (getting from socket input stream) int k=1; while((input=br.read   More>>

I want the server writes a lot of data to client. While client receiving the data, it closes its socket. At the server side, there is on exception occurs. So how can the server find out that the client has closed the socket?




Here is what I tested:



Server side: (PrintWriter pw: getting from socket output steam)



try{

... int k=1;

while(k <5000){

pw.println(k);

pw.flush();

}

...

}

catch (Exception e){

...

}



client side:

BufferedReader br (getting from socket input stream)

int k=1;

while((input=br.readLine())!=null){

k++;

if(k==5){

socket.close();

break;

}

...

}





when client closes the socket, there is no exception at the server side.



So, how can the server detect this? (I don't want to set time out.)



Thanks.

   <<Less

Re: How to find out one side has closed socket?

Posted By:   zhang_binbin  
Posted On:   Thursday, September 27, 2001 06:25 PM

A new method isClose() is added to jdk1.4 beta2.

Wish this will help you!


Zhang binbin
About | Sitemap | Contact