Game Development Reference
In-Depth Information
Parameters:
n Socket: A valid socket handle.
n Backlog: The maximum length of the queue of incoming connections. Set it to
SOMAXCONN if you want the underlying service provider to use its default value.
If a client attempts to connect and the backlog is full, the connection will be
refused.
Return Value:
Returns zero if the function succeeded or SOCKET_ERROR if there was an error.
Here ' s an example of using listen() to set the backlog to 256:
if (listen(m_sock, 256) == SOCKET_ERROR)
{
// HANDLE ERROR HERE
}
accept()
When a remote client attaches to the listen socket with connect() , the server side
will detect input on the listen socket. Exactly how this happens you
ll see in a
moment with the select() function. Once input is detected on a listen socket,
you call accept() to establish the connection.
'
SOCKET accept( SOCKET listenSock, const struct sockaddr* name, int namelen);
Parameters:
n Listen Socket: A valid socket handle to a listen socket.
n Name: A structure that receives the address of the connecting client.
n NameLen: Always sizeof(struct sockaddr) .
Return Value:
Returns zero if the function succeeded or INVALID_SOCKET if there was an error.
There are a few things to be aware of when using accept() . First and foremost, it
will block if there are no client connections ready and the listen socket is set to block-
ing. If the listen socket is set to nonblocking and there are no client connections
ready, it will return an error and could put the listen socket in an unusable state.
Basically, don
t call accept() until you have input on the listen socket connection
and you can be sure you have at least one client ready to start talking. You can check
for this by calling the select() function, which is up next.
'
Search WWH ::




Custom Search