C ++ winsock2 how to tell when a connection is closed

I have a C ++ program using winsock2. I would like to know how to tell when someone will connect to my program.

+2


source to share


2 answers


Use select to wait for a read on a socket; when the socket is closed, winsock should report it as readable. After receiving from the socket, you will receive 0 bytes, informing you that the socket was closed.



+5


source


int received_bytes = recv(_socket, buffer, sizeof(buffer)-1,0);
if(received_bytes > 0)
{
   //data received
}
else if (received_bytes == 0)
{
  //connection closed
}

else
{
  //wait for more data
}

      



0


source







All Articles