News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

winsock non blocking accept

Started by gelatine1, December 02, 2014, 02:42:26 AM

Previous topic - Next topic

gelatine1

I am currently working on a non-blocking server and if I call the accept function for the first time it is very likely that no connection is present to be accepted. I have used WSAAsyncSelect to notify the FD_ACCEPT function. Does this mean that whenever another program calls the connect function to this server socket that I receive the FD_ACCEPT message ? And when the program receives this message, should it call the accept function again ?

I am asking this because on msdn I read that in a similar situation when the connect function is called then it is very likely to fail with the WSAWOULDBLOCK error message but whenever the socket is done with connecting the program receives the FD_CONNECT message and then the connect function should not be called again right ?

so for connect it is fine to simply call it once and for accept it should be called multipe times ? or am I just totally messing up ? Any help is welcome.

Thanks in advance
Jannes

Tedd

When you have a listening socket (with WSAAsyncSelect), and receive FD_ACCEPT, it's a notification that there is an incoming connection -- you still need to handle the connection.
So, you should then call accept, which will return a new socket for that incoming connection (between the server and that particular client). The old listening socket is still there and is still listening - when there is another incoming connection, you will receive another FD_ACCEPT, which you can accept and get yet another socket for that particular connection, and so on.
accept will only block if there is no incoming connection ready, but you know there is because you received FD_ACCEPT, so it will not block.

FD_CONNECT is received when a connection (e.g. from the client to a server) has completed, i.e. the server 'accepted' the connection and now you can send your first "hello" or whatever the protocol requires. For a client connecting to the server, it only needs to call connect once, because it only wants one connection; while a server can have many incoming connections from many clients.

I recommend reading the whole of the WSAAsyncSelect article. Then read it all again.
Potato2

gelatine1

But the call to the connect function should be used outside of the FD_CONNECT message handler right ?

Tedd

You make a call to connect and then once the connection is ready you will receive FD_CONNECT.
You obviously can't receive a notification that the connection is ready before you have made the connection.
Potato2