开发者

Server Client Program in C

In server-client program server can listen for a number of clients.

listen(sockfd,5);

Does it mean that server can handle 5 clients at the same time. O开发者_如何学运维r I have to use multi threading for that?


No, it means 5 clients can connect without you calling accept. After those 5 clients connect (actually slightly more than 5) new connections will fail.

The stack "accepts" connections (completes the handshake) without your intervention. So without you calling accept, if you use a sniffer you'll see successful handshakes. When you actually decide to accept(2) a connection, the stack simply gives it to you.


It means that their is a queue of up to 5 connections before connections fail unless you start accepting them. It is working looking at the Apache source code as I think it is an excellent template to implement a server.


I'd say yes, the second parameter gives you the maximum length of the queue of pending connections (from the man pages http://linuxmanpages.com/man2/listen.2.php ). And no, there's no need for multi threading.


The above expression means that 5 clients are being queued and the 6th client will be ignored if the queue is full. you have to use accept() to read the queue so that others can connect. you can read this link http://linux.die.net/man/2/connect and further read about select() for advance socket programming. you can use multithreading if you want to serve more than 1 client at a time using fork().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜