Reputation: 59
I am working on socket programming using c language and linux platform and my requirement is to make a server listen only for two or three clients. How is it possible ?
Upvotes: 1
Views: 1657
Reputation: 3209
you make that by specifying second argument on listen() call.
i assume that you are using TCP protocol.
from man pages:
int listen(int sockfd, int backlog);
The backlog argument defines the maximum length to which the queue of pending connections for sockfd may grow. If a connection request arrives when the queue is full, the client may receive an error with an indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored so that a later reattempt at connection succeeds.
so for two clients - you call listen like: listen(fd, 2);
suggested reading: https://beej.us/guide/bgnet/html/multi/syscalls.html#listen
Upvotes: 4
Reputation: 310884
Close the listening socket when you reach the limit, and open it again when one of the clients disconnects.
Or
Leave it open but stop accepting connections while you're at the limit.
The backlog parameter to listen()
has nothing to do with any of this, contrary to other answers and comments here.
Upvotes: 0