hi, i am making a program with sockets but i cant know when the client disconnects from the server, im trying this:
public bool IsAlive()
{
return clientSocket.Poll(1000, SelectMode.SelectRead);
}
//This is on a thread so it dont freeze my app
//if IsAlive returns false then i fire an event so i can notify the client went offline
public void PollConnection()
{
while (true)
{
if (!IsAlive())
{
if (ClientOffline != null)
ClientOffline(this, new ConnectionEventArgs(this));
break;
}
}
}
my problem is that :
return clientSocket.Poll(1000, SelectMode.SelectRead);
never returns false , even if i close the client .
mig16

Socket.Poll
Priyananth
mig16
Der Sven
Sorry for diverting your mind! but why dont you use NetworkStream Why do you want to do all work With Socket
Try this:
NetworkStream networkStream = new NetworkStream(clientSocket);
Now do whatever you want to do with networkStream! Much Much Much easiear and simple to handle, detect disconnection, send and receive data.
What do you say
One other thing you better post this question in Networking and Communication Forum to get better answers to your problem.
Best Regards,
dhika
According to the documentation, Socket.Poll (SelectMode.SelectRead) will return true if the connection has been closed, reset or terminated. It returns false when the client is connected, but there are no data to read. Sounds strange at first, but I guess this is made so that you are not kept waiting for a condition that will never be verified.
Anyway, this can be used in the following way:
bool isDisconnected = (socket.Poll (100, SelectMode.SelectRead) && (socket.Available == 0));
HTH
--mc
jamba8
mig16
MSP.Saami
a Similar question was answered here:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=761863&SiteID=1
Best Regards,
saiet
thx
mig16
chaza
while (true)
{
if (!IsAlive())
{
if (ClientOffline != null)
ClientOffline(this, new ConnectionEventArgs(this));
break;
}
}
public bool IsAlive()
{
return clientSocket.Poll(100,SelectMode.SelectRead) && (clientSocket.Available == 0);
}
i allready made sure that the while loop is running, and it allways reutrn true
i open my server, and await connection , when connection is made, i close the client and the server never detect it, i use the debugger and i allways get true, meaning cliet still connected,
this is very strange :(
mig16
Zulbaric
Sorry, but the logic in the code you posted is reversed.
As it stands, IsAlive will return true when the client disconnects. Try:
public bool IsAlive()
{
return ! (clientSocket.Poll(100,SelectMode.SelectRead) && (clientSocket.Available == 0));
}
Keep in mind that you will not be able to detect if the client disconnects until you have emptied the receive buffer.
This said, you may want to follow the advice of RizwanSharp and move your question to the Networking and Communication forum, as the thread is getting a little off-topic and you will get better assistance there.
HTH
--mc
SanjaDj
Yes, Thats What I and Amit Paka Explained in the following post:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=761863&SiteID=1
Cheers ;-)