Click to See Complete Forum and Search --> : Winsock: listen and backlog-parameter


martho
October 6th, 2004, 02:21 AM
Could someone explain me the backlog-paramter in the listen-function in winsock2?

Wombat
October 6th, 2004, 02:44 AM
From MSDN:

The maximum length of the queue of pending connections. If this value is SOMAXCONN, then the underlying service provider responsible for socket s will set the backlog to a maximum "reasonable" value. There is no standard provision to find out the actual backlog value.

martho
October 6th, 2004, 02:58 AM
Hi wombat, thanks for your reply. I found what MSDN says but I didn't understand it... Sorry, I'm new to winsocks. Perhaps I should explain what I want to do: I want to build a server with blocking winsocks (embedded in threads). To keep it simple in the beginning the server should just receiving and tracing text-msg which it receives from the clients.

I do:
1. Create a socket with socket()
2. bind() the socket
3. listen() to the socket
4. accept() the connection -> acceptSocket
5. recv from the acceptSocket

My problem is: I don't understand how to receive multiple data. That means: While receiving another client could try to connect and si refused because the server is still receiving. How could this be handled?

Wombat
October 6th, 2004, 03:14 AM
To solve your problem, after you accept each client connection, you can create a new thread to service that particular client request.

Something like that:

while(!Terminate)
{
// Accept new client connection

// Create new thread to service new client request

};

In this case, the chances that multiple clients crashing with one another will be minimized. If new client do arrive when the server is creating new thread to service the current client's request, the new request would be placed in the queue automatically according to the backlog parameter. If the queue is already full, then the client request will be dropped. The client would receive an error from the connect() method, and may try to connect again at a later time.

martho
October 6th, 2004, 03:19 AM
Ah, now I understand. Thanx a lot!!

Mathew Joy
October 6th, 2004, 11:46 AM
Could someone explain me the backlog-paramter in the listen-function in winsock2? To provide additional explanation…the listen() winsock call places your socket on a listen mode. Once it is on this mode, it can accept incoming connections on this interface and port. The actual connections are actually place on a queue (which is what is said in the MSDN ) and the backlog parameter signifies the length of the queue.
The default value depends on the OS (5 for 9x and 200 for 2000) and it is recommended that the backlog value is set to SOMAXCONN. Even if you give a high no for the backlog parameter, this will be silently ignored (as said in the MSDN).

Hope you understood what MSDN mean :)

:thumb: