Two Winsock TCP Questions

Started by
4 comments, last by DividedByZero 12 years, 6 months ago
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.
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.
This is the first result I get when Googling for "nonblocking socket connect".

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.
enum Bool { True, False, FileNotFound };
I'm not sure I follow your logic flow, but why not just pass some data from the server to the client right after it connects to tell it you are done?

For some video tutorials on programming non-blocking TCP connections, have a look on my site here: http://www.marek-knows.com/downloads.php5?vmk=network
I fail to see what the problem is.

If connect returns non-zero, it failed. If it returns zero, it succeeded.

This topic is closed to new replies.

Advertisement