dcsimg
Identifying socket disconnection.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Rajakumar_Makapur
Posted On:   Thursday, June 28, 2001 01:15 AM

Hello all, In my server socket program there is while loop to read the data from client. If client socket is closed before sending any data because of n/w problem. How to come out of that loop. Is there any way to check that other end socket is closed. code is as follows. try { while(true) { soc = sersoc.accept(); InputStream is = soc.getInputStream(); bis = new BufferedInputStream(is); while(len == 0) { len = bis.available(); /*how to come out from if client socket closed before sending any data */ } // End of while(len == 0) byte b[] = new byte[len]; int l = bis.read(b, 0, b.length); String s = new Strin   More>>

Hello all,
In my server socket program there is while loop to read the data from client. If client socket is closed before sending any data because of n/w problem. How to come out of that loop.
Is there any way to check that other end socket is closed.

code is as follows.

try
{
while(true)
{
soc = sersoc.accept();
InputStream is = soc.getInputStream();
bis = new BufferedInputStream(is);
while(len == 0)
{
len = bis.available(); /*how to come out from
if client socket closed before
sending any data */
} // End of while(len == 0)
byte b[] = new byte[len];
int l = bis.read(b, 0, b.length);
String s = new String(b);
System.out.println("Message : " + s);
}
}catch(Exception e){
e.printStackTrace();
System.out.println("Server is not connected");
}

   <<Less

Re: Identifying socket disconnection.

Posted By:   Tim_Rohaly  
Posted On:   Thursday, June 28, 2001 10:38 AM

Reading from the socket will throw an Exception if the
client end is closed. Catch the exception and deal
with it.
About | Sitemap | Contact