How to check the client connection exist or not

Hi,

I'm new to C socket programming. In my C code i want to check is the particular client is connected or not using the socket descriptor or in some other way. Can any one help in how to do this?
Read about connect() function, you'll get your answer.

no with connect i'm not able to get that details..
Well, in the question you have mentioned that you want to check whether the client connected or not.
This means that client is not yet connected to server.

However, in the subject you have mentioned that you want to check whether the client's connection exist or not.
Well, this means that client is already connected and now you want to check if it's connection still exist or not.

These are two different questions. For the first one, as I said before, you'll need connect() function.
And for the second one you'll need recv() function.
Sorry Kevin,
I'll tell you exactly what i'm looking for..

Assume, 10 clients are connected to my server. At some point of time 3 may disconnected and 5 new clients where connected. I want to get all the details like,

How many clients connected now and who are all?
Who are all disconnected?

I've only the Client socket Descripter of all the clients connected to server so for. With this how to get the details. Clients disconnect may or may not be a proper one. The Moment the Client disconnect, i've to set the Socket Descriptor to zero. How to do all these.

Thanks in advance..
I don't know if this is the best way but here's my 2ยข

How many clients connected now and who are all?


For this you'll have to maintain a count at the server. So that every time a new connection has been established you increment that count. And to store all it's information, probably I would use a linked list or may just list (depending on whether you are using C or C++)
So, when new connection is established you create a new entry into the list and store all the information of the client for ex. it's port number, IP etc. Your struct should also have a variable called as 'active' which could be a boolean variable. This will help you while getting the information of all those clients that are now disconnected. This variable should be true when connection is active and you should set this variable to false when connection from this client gets disconnected.

Who are all disconnected?

As mentioned above. Just traverse through the list of all the nodes where active == false.

If you store all the information, which I don't know why you are storing for disconnected clients, then after it is disconnected you can remove this socket FD from the master Socket FD list and for this entry set 'active = false'

Hope this helps !

see there can be two things you can do..

1. let the client tell you that they want to stop
2. you your self ping all the clients after some time lets say a second or two.

1a. when ever a client wants to disconnect it will send you a specific keyword that he is stopping which you can define in your protocol..lets say he will send -1 or anything.. and you will know that this socket is no more and you delete that entry.
1b. lets say you created different threads for each client connected, in that case the thread will block themself on recv() call. now as the client goes of the recv() call will break giving -1 and you can know that this client has gone and you can delete the entry.

2. in this case you need to ping all the clients after some time, create a thread for this and he will keep on doing this and keep on managing your list.. you then dont have to bother.. you can use fstat() for that.. for each socket descriptor, call the function if you receive negative response the client has gone and remove it from the list.

so check which design fits into your needs. in second one you dont have to create many threads but one which will do your work but it may not be very efficient as you your self are checking and this process will take some time. first one is effective but then you have to manage threads.
thanks alot sharma and kevin..

sure this will help me to solve my problem..
Topic archived. No new replies allowed.