Jump to content
  • Advertisement
Sign in to follow this  
QuadMV

Problem Detecting Broken Socket Connection

This topic is 4710 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I’m hoping one of you can help with this. I’m on LINUX writing basic TCP/IP code. Originally after performing a Listen and Accept to acquire a connection from a client, I performed a blocking recv on the socket to read incoming data. If the client was shut down the recv would fail right away with an error indicative of a broken socket, and was easily detectable. I made a change from blocking to non-blocking. I also use PEEK_MSG, although I don’t think this has anything to do with the problem. Now with the socket set to non-blocking, I receive EAGAIN whether the socket is established or not, so now when the client dies, I can’t tell it’s gone or if there just isn’t anything to read. I’m sure I must be doing something wrong, but I’m not sure. Does anyone have any suggestions? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Try using select() to poll the state of the socket, and don't use MSG_PEEK. (The article linked is specific for Windows sockets, but the principle applies to all socket programming.)

EAGAIN doesn't mean an "error" in the sense that "something is borked." EAGAIN means that the call you requested can't be completed because it would cause the socket to block, so try again later. Using select() can help you detect when the socket is ready to fulfill the requested operation, and you can even use select() to block for a controlled amount of time on the socket if you like.

Share this post


Link to post
Share on other sites
A broken or closed tcp socket will behave as if it is ready for reading (with select()).

Reading from a broken or closed socket won't result in EAGAIN, it will either return 0 bytes and no error (meaning normally closed), or return some error that isn't EAGAIN (meaning it's broken in some way).

Trying to send to a broken socket will also result in an error (probably).

Errors should be easy to detect.

Linux also has several other more advanced socket error handling mechanisms not found in other OS; I recommend that you don't use them unless you have a really good handle on the standard ones and they aren't good enough.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!