I want a server program which should accept only maximum of one connection and it should discard other connections. How can I achieve this?
3 Answers
Only accept()
a single connection.
Here is a typical server routine:
s = socket(...);
bind(s, ...);
listen(s, backlog);
while (-1 != (t = accept(s, ...))) {
// t is a new peer, maybe you push it into an array
// or pass it off to some other part of the program
}
Every completed accept()
call, returns the file descriptor for a new connection. If you only wish to receive a single connection, only accept()
once. Presumably you're done listening after this, so close your server too:
s = socket(...);
bind(s, ...);
listen(s, backlog);
t = accept(s, ...);
close(s);
// do stuff with t
If you wish to only handle a single connection at a time, and after that connection closes, resume listening, then perform the accept()
loop above, and do accept further connections until t
has closed.

- 112,946
- 110
- 377
- 526
-
Thanks for your valuable comments, Actually how can I know whether the first connection is already closed or not? because if my first connection is ideal for certain period of time? if I receive a new connection I have to check whether first connection is active or not before accepting this new connection. Do we have any way to achieve this scenario? – Thangaraj Nov 13 '10 at 05:10
-
When you `read()` from a socket that the peer has closed, it will return 0. Also you may close it yourself, by calling `close()` or `shutdown()`. Just don't return from your function handling `t` until you are satisfied that you are done with the connection. Keep in mind it is possible to handle more than one connection at a time, but I presume that handling a single connection is what you are after. – Matt Joiner Nov 13 '10 at 08:57
-
Really awesome answer Matt Jonier, here we have one more problem, assume the read/write is handled by its child process and connection is handled by its parent process. In this case we need IPC to send data from parent to child process if we actually read some data using read() to check the connection status, is this not overhead? Do we have solution for this too? I have one solution. We can use getpeername() to check the status of current active connection, would there be any side effect? I am not sure whether this would correct solution? please share your thought. – Thangaraj Nov 13 '10 at 13:24
-
You don't need any IPC to use the connection in the child. If you wish to communicate that the connection is finished, you are better off terminating the child, and having the parent process `wait()` on it. You are not using both the server socket, and accepted socket simultaneously, so there is no need to `fork()`. Also if you do `fork()`, the child should close its handle to the server socket, and the parent its handle to the accepted socket. – Matt Joiner Nov 14 '10 at 07:07
Corrections see underneath:
You can define the amount of accepted requests in the listen method.
listen(socketDescription, numberOfConnectionsPending);
Second parameter is for setting the number of pending connections and not the amount of connections itself..
If you set the numberOfConnections to 1 all the other clients which sends a request to the server will receive a timeout error..
Here you can find more informations: http://shoe.bocks.com/net/#listen
I read the listen documentation wrong. You should work with the accept method which is described in Matt's answer.
-
How can I alert the user that already some other connection is active and this time out is not due to network outage? – Thangaraj Nov 12 '10 at 12:50
-
Is there any way to check, whether last opened socket descriptor is active or not? – Thangaraj Nov 12 '10 at 13:10
-
1I think you could check this at the accept method. But I am not sure.. accept() returns a positive integer which is the socket descriptor for the accepted socket. If an error occurs, -1 is returned and errno is set to indicate the cause. Check this website: http://shoe.bocks.com/net/#accept – Prine Nov 12 '10 at 13:57
-
This is incorrect, that value is the _backlog_, it's the number of unaccepted connections that may queue. – Matt Joiner Nov 12 '10 at 20:52
-
Thanks for the correction! I editted my answer. Unfortunately I read the documentation of listen wrong... – Prine Nov 13 '10 at 04:42
Do you want to reject all connection or to make a queue? I think that what you are looking for is the so-called "singleton". Look at the wikipadia for the Singleton design pattern.

- 8,590
- 21
- 84
- 121