🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

socket state

Started by
3 comments, last by Xelot 22 years, 8 months ago
Which Winsock function is for checking the state of a socket like wether its connected or not? I looked for it in MSDN but no luck. BTW im using C++.
Advertisement
connect(). Look up the return values; one of them specifies the socket is already connected.
If you _know_ that you called connect() on a socket, and you want to check for disconnection, you can rely on the behaviour of recv().

When data is waiting, recv() will read the data and return.
When no data is waiting, recv() will either block (for blocking sockets) or return immediately with the EAGAIN error code (for non-blocking sockets).
When the connection is broken, recv() returns 0.

Note that if you select() or poll() on a socket, select() will return when data is waiting or the connection is broken. So the only thing you really need to do is to check the return value of recv().

cu,
Prefect
Widelands - laid back, free software strategy
Would someone please give me some code for checking if a socket is connected. I tried everything. I got connect() to tell me that it is already connected. But I need to check if the socket has gotten disconnected for any reason. I couldnt get recv() or select() to tell me if the socket is connected. Someone please give me some code.

Edited by - Xelot on November 3, 2001 9:30:48 AM
Did you read my post at all? All you need to do is call recv(). If it returns 0, the connection is broken. Also, if it returns -1 and errno is not EAGAIN, something's amiss as well. AFAIK recv() returns 0 on normal disconnect (as in, the other side called close()), while an error like EPIPE is returned when the connection just broke or timed out or something.

cu,
Prefect

Edited by - Prefect on November 7, 2001 11:16:30 AM
Widelands - laid back, free software strategy

This topic is closed to new replies.

Advertisement