Meh
Meh

Reputation: 177

c linux sockets: check for existing connections from client side

I have the following setup:

So... my question is: Is it possible to check if there are other applications already connected to the socket from the client application? I want only one process connected to the socket at any given time and I want to check this from the client application. Is this possible? After 3h googling I couldn't find anything relevant :(

(sorry, no experience with network programming)

Upvotes: 1

Views: 826

Answers (3)

alk
alk

Reputation: 70883

No, a client is not able to see how many other clients are connected to a server.

To be able to retrieve this information an application specific protocol needs to be used on client and server.

Anyhow there is this one special case: If the client knows that a maximum of N clients can connect to the server, and it's own try to connect is refused it could assume that N clients are connected to the server already.


To set the maximum number of connections ncat handles in parallel use it's option -m/--max-conns. Verbatim form man ncat:

-m numconns, --max-conns numconns (Specify max number of connections) . The maximum number of simultaneous connections accepted for an Ncat instance. 100 is the default.

Upvotes: 2

jman
jman

Reputation: 11586

Run:

netstat -an | grep <your server port port number> 

on your client machine to see any existing TCP connections.

Upvotes: 1

Joe
Joe

Reputation: 7798

Can you not close the listening socket on the server after you've accepted one client? If there's no listening socket no more clients will be able to connect. Once you've dropped your one client, you can then open the listening socket again, ready for one more. That way the client will see a "failure" to connect if the server is busy, or will succeed normally otherwise.

The down side of this approach is that the client won't be able to determine exactly why it can't connect, it could be because the client is busy (has its one client) or it could be because of other issues. Does this matter?

Upvotes: 0

Related Questions