Jump to content
  • Advertisement
Sign in to follow this  
Haytil

Two Winsock TCP Questions

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

Hello,

I'm trying to write a system that uses Winsock non-blocking sockets for TCP communication, using Beej's tutorial and other resources. I have two questions that I have not been able to answer:


-How can I tell when a client has given up on connecting?

That is, if I attempt to connect() with my client while the server is running, everything is fine. If I attempt to connect() with my client before starting my server program, but start my server program within a short time (a few seconds), the connection still succeeds and everything is still fine. However, if I attempt to connect() with my client before starting my server program, and delay startup of my server program by more than a minimum length of time, the connection is never completed.

What I assume is happening is that connect() makes several attempts, spaced out over a few seconds, to connect to the server, before finally giving up after a certain number of failures. So how can I tell (on the client's side) when the client's attempts to connect() have ultimately failed (and thus require another explicit call to connect() later on)?


-How can my client tell when the server has called the accept() function in response to my client's call to connect()?

Currently, I use select() and FD_ISSET() on the client side, after a call to connect(), to tell when I can start writing to the socket (i.e., the connection is ready to send data). However, after experimentation, I found that I receive success from FD_ISSET() as long as the server had called listen() before the connect() attempt, even BEFORE the server has a chance to call accept().

So it seems to me that using select()/FD_ISSET() on the client side in the manner that I do only works as a check to see if a connection is POSSIBLE to the server (i.e., a network route can be made and the server has called listen() on the right port), but it does not work as a check to confirm that the server has actually finalized the connection (by calling accept()). So how can I see, on the client side, whether or not the server has yet called accept()?

Thank you.

Share this post


Link to post
Share on other sites
Advertisement
Looking at MSDN, both your questions can be handled by the same solution. You can go with using WSAAsyncSelect or WSAEventSelect, I'm guessing you'd want to use the latter if you're going cross-platform. I don't have any experience implementing them, so I can't help you there.

Share this post


Link to post
Share on other sites

Looking at MSDN, both your questions can be handled by the same solution. You can go with using WSAAsyncSelect or WSAEventSelect, I'm guessing you'd want to use the latter if you're going cross-platform. I don't have any experience implementing them, so I can't help you there.



Neither WSAAsyncSelect nor WSAEventSelect are very robust APIs. I recommend using select(), or perhaps OVERLAPPED I/O on the socket/handle.

If a connect() has timed out, select() will return the socket as ready, and operating on it will return an error.

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!