![]() |
|
This thread is locked; no one can reply to it.
![]() ![]() |
1
2
|
winsocks listen() |
Frank Drebin
Member #2,987
December 2002
![]() |
what is the backlog in the int listen (SOCKET s, int backlog); function good for |
gnolam
Member #2,030
March 2002
![]() |
Quoted directly from MSDN: Quote: <b>listen[/b} The listen function places a socket in a state in which it is listening for an incoming connection. int listen( Parameters s So the answer is that it's the size of the pending connections queue... -- |
ReyBrujo
Moderator
January 2001
![]() |
When you create a socket, and put it to listen, you need to specify the amount of connection you want to handle. I believe Linux refuses new connections. -- |
Frank Drebin
Member #2,987
December 2002
![]() |
so if i want a server where 32 clients can connect at max i'll use 32 ? |
ReyBrujo
Moderator
January 2001
![]() |
Yep -- |
Frank Drebin
Member #2,987
December 2002
![]() |
and it just set the socket in the listen mode or does it wait until 32 clients have connected? |
ReyBrujo
Moderator
January 2001
![]() |
No, it sets the socket in listen mode. You then call accept to get incoming calls. Usually, you do a while like: listen(oldsocket, 32); while ((newsocket = accept(oldsocket, ...)) > 0) { process_new_client(newsocket); ... } That piece of code is blocking -- |
Frank Drebin
Member #2,987
December 2002
![]() |
[edited] |
ReyBrujo
Moderator
January 2001
![]() |
listen returns immediately. accept is the one which blocks the socket until a client connects. You can use fnctl (Linux for sure... maybe Windows has it too?) to declare the socket as non blocking, or use select to simulate the non-blocking stuff. -- |
Frank Drebin
Member #2,987
December 2002
![]() |
and the send and receive functions a re also blocking ones. |
ReyBrujo
Moderator
January 2001
![]() |
Yes, you can use select to multiplex a program. Select has five parameters, the first one is the maximun descriptor plus one of socket to check, the second parameter is a bag (fd_set) with sockets to check for incoming information (receive data), the third parameter is a bag for outcoming information (send data), the fourth is a bag with sockets for exceptions, and the last parameter is the timeout select should wait before stopping checking the sockets. So, you can set sockets to listen into the second bag, and whenever one is set, you receive the information. This is the example in Linux:
(Edited: In FD_SET(0, &rfds); he sets socket 0 (stdin) in the bag for sockets to listen. Then calls select with 1 as first parameter (highest descriptor in all bags, in this case 0, plus one). tv holds how long select will wait for connections. Here is set for five seconds, so the program will block for 5 seconds in select. If you set 0, it becomes non-blocking.) -- |
Frank Drebin
Member #2,987
December 2002
![]() |
ok and what to do that the receive function isn't blocking? |
ReyBrujo
Moderator
January 2001
![]() |
The only way I remember to make recv non blocking is using fcntl. With select, you peek the socket, if it has something, you call recv (which is still blocking, but since you know there is something to read, it returns immediately. If you need some code example from me, you need to wait until I go back home -- |
Frank Drebin
Member #2,987
December 2002
![]() |
1. |
gillius
Member #119
April 2000
|
backlog is NOT the number of clients you may have connected! The backlog parameter refers to the number of clients that are waiting for you to call accept. If you call listen with backlog 16, and before you call accept 17 people try to connect, 16 people will make it through, and 1 will be refused. When you call accept once, there will be 15 people pending. If 2 people try to connect then before the next accept call, one will be refused. So on and so forth. It is the number of pending connections waiting to be accepted before they are completely rejected. Gillius |
ReyBrujo
Moderator
January 2001
![]() |
1. Yep. You peek the connection. If there is a new connection, you accept it, if you have data to be received, you receive it. -- |
Frank Drebin
Member #2,987
December 2002
![]() |
hmm so if i want to accept 32 clients then i have to set the backlog at least to 32 but better very high for there is the chance for as much as possible clients to wait (in a qeue) for my acception? |
gnolam
Member #2,030
March 2002
![]() |
Quote: hmm so if i want to accept 32 clients then i have to set the backlog at least to 32 but better very high for there is the chance for as much as possible clients to wait (in a qeue) for my acception? No. backlog is the amount of connections you can have pending - read gillius' explanation again. Here's a simple example: think of it as a queue outside a club - the queue size determines the number of people (connections) who can wait outside (pending connections) to be let inside (accept()ed) by the bouncers . If the club seats 32 people they can still have a queue with room for only one, as long as the guests arrive one at a time before being let in. However, if two people arrive at the same time (or between checks by the bouncer if there's someone to be let in), the first one will be put in the queue while the other is simply refused entry and is lost to the club as a customer. With a queue size of two, two people can wait outside, etc. Hope that was simple enough -- |
ReyBrujo
Moderator
January 2001
![]() |
Ok, here is a very small program. Put this into a file and add it to a VC project, add Ws2_32.lib library for linking, and compile it. Under MinGW, use g++ main.cpp -o main.exe -lwsock32. Execute it. By default, it is a non blocking program. There is a number counting advancing to show you it is not blocking the execution. Now, run a telnet (telnet localhost 4344). Telnet will connect, and everytime you type something, it will send the letter to the server. You can connect up to 32 telnets. Every time you type a letter, the server prints the socket and the letter. If you see the Task Manager, the program would be taking like 99% of CPU. If you want your server to wait for 1 second before continuing processing, pass 1 as argument. You will notice I set the new delay. This time, the server will wait for 1 second before continuing with the program. Finally, if you want to make the program blocking, pass -1 as argument. If you check the code, I call select with NULL as last parameter to block it. Be careful! I am not doing error handling when a telnet shuts down, so if you do it will get mad (Edited: Non-Windows support is not tested, too sleepy to turn my other computer on, but should work with one or two hacks). -- |
Krzysztof Kluczek
Member #4,191
January 2004
![]() |
Two notes: If you'd like to look at wrapper I've written and C++ with STL doesn't scare you, click links below. Linux support isn't implemented yet, but it won't be hard. ________ |
ReyBrujo
Moderator
January 2001
![]() |
MSDN said: Compatibility Could it be that TCP/IP limits it to 5, and so anytime using *_INET family it is limited to 5? -- |
Frank Drebin
Member #2,987
December 2002
![]() |
i think i got it now btw what version of winsock is "the best" to use? |
X-G
Member #856
December 2000
![]() |
If you don't need features present only in 2.0 you don't really have a reason to use it... -- |
Frank Drebin
Member #2,987
December 2002
![]() |
so you think better using 1.1 for better compatibility? |
X-G
Member #856
December 2000
![]() |
Yes. It's likely that everything you need is in 1.1, and that will also ensure portability as you will mostly be using BSD socket functions, which are easily ported to any *nix platform later. -- |
|
1
2
|