Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i written code to find client socket is connected or not.
if(socket.isConnected())
{
//if disconnected also it returing true;
}
else
{
}
But client disconnected also it returing true;Please tell me How to find whether client socket is connected to server or not.
Posted
Comments
pasztorpisti 18-Sep-13 6:22am    
In a correctly written networking application you never need this check. An existing socket should always be connected and closed/deleted in case of both graceful/ungraceful termination of the communication. What kind of problem do you want to solve with isConnected(). I'm afraid you are trying to solve a problem with a wrong approach.
kalaivanan from Bangalore, India 18-Sep-13 6:37am    
if server socket is terminated connection incorrectly,isConnected() returing true.

1 solution

Android's Socket.isConnected() will return true instead of false if the Socket was disconnected incorrectly (i.e. not calling Socket.close).

I think the only guaranteed way of knowing if a Socket is still alive is to try to write to it and handle the IOException that it will throw if it is not connected.

Hope this helps,
Fredrik
 
Share this answer
 
Comments
kalaivanan from Bangalore, India 18-Sep-13 6:21am    
Thank you for reply, this is good idea ,helped for me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900