Jump to content
  • Advertisement
Sign in to follow this  
Woltan

TCP accept non-blocking

This topic is 3747 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

Hey guys, I have a problem concerning the accept socket function. Maybe one or two could gimme a hand out of my dilemma. Here is my situation: I have to accept recv and send stuff in a single thread, and do not have access to any window callback function. To do this, I used the select function in combination with the FD_SET etc. macros.
static SOCKET sListen, sClients[8];
//... Init socket and wsa stuff

while(1)
{
FD_SET(sListen, &fdset);
if(select(0, &fdset, NULL, NULL, &timeval) > 0)
{
    accept(...);
}

for(int i = 0; i < 8; i++)
{
FD_SET(sClients, &fdset);
if(select(0, &fdset, NULL, NULL, &timeval) > 0)
{
    recv(...);
}
}

}

This seemed to be working just fine, unter I closed my client before I shut down my server. What it did: My server was expecting a connection attempt, because the sListen socket had stuff to be read. But since this is not the case, I am stuck in the accept() function and my whole program stops right there! Does any of you have an idea how I could solve this problem? As said before: I cannot open another thread and do not have access to a window callback function. Thx in advance for any hint/link/suggestion/tipp/ANYTHING! ;) Regards Woltan

Share this post


Link to post
Share on other sites
Advertisement
Did you try setting the socket to non blocking by calling ioctlsocket() with a FIONBIO argument?

Share this post


Link to post
Share on other sites
The whole point of select() is that, if it returns true, then you are GUARANTEED to not block on the next call to that socket.

However, I see a few problems with that code:

1) You're not clearing the socket set before you FD_SET the socket identifiers in it. This means that old bits may still be set. This is probably the cause of the bug you're seeing.

2) Your loop setting the client sockets calls select() once per socket. That's quite wasteful. It's better to loop through the sockets, set all the identifiers, call select() once, and then loop over the result, calling recv() on each socket that is returned in the fd_set.

3) Passing 0 for the first argument to select() works for Windows, but fails big on UNIX.

Share this post


Link to post
Share on other sites
in sdlnet

newConnection = NULL;
if (newConnection = SDLNet_TCP_Accept(ListenSocket))
{
//Do things
}


has worked just fine for me, no blocking.

Share this post


Link to post
Share on other sites
Hey folks,
thank you very much for your input! I very much hope that this will solve the problem, and I am will give it a shot on monday. For the weekend, I'll try to get it of my mind as much as possible ;)

I'll probably have to get back to you on monday, with other problems that come up then ;)


Cherio and thx again for your help
Regards
Woltan

Share this post


Link to post
Share on other sites
Hey folks,
I just wanted to add the quick note, that the ioctlsocket function did the trick! Futhermore, I changed the FD_SET things according to the suggestion of hplus0603.
This community is the best!!!
best regards (with hopes that i maybe once give some advice in return myself)
Woltan

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!