How could server detect closed client socket using TCP and c++?
Reading some examples on net and copying some codes I was able to build a (frankstein) server that accept connection from one client and receive and send messages to it. The big problem that shows up is that I don't know when client disconnects from my server.
I've been looking for a solution, but no success. I'd read about SO_KEEPALIVE option (which could solve my problem), but I don't know how to use it (how to check the value of it).
I can't use ping because the server (machine) could be running, but not the client (software).
Anyone knows a good tutorial or how to (for beginners like me xD) of TcpIp sockets using c/c++ and how to detect when a client disconnect?
ps: I don't know sockets very well
Thanks in advance.
Actually, that's an interesting question - with a nontrivial answer.
Here are a couple of links. I'd also recommend getting Stevens: "Unix Network Programming"
Thanks for your reply paulsm4!
I read the links (and a lot of other links), but no practical solution yet. My project is like a data logger, so I just need that my client connect, receive some data from me, disconnect, connect again, receive data... and repeat this procedure when the client need to get some information (that means any time of day).
I'll try to do some tests here... but I'm losing my hope of find a simple solution :/
Thanks anyway. :D
It sounds like everything you're trying to do, sockets already does for you! Specifically:
1. You start your server.
He sets himself up, and starts listening for client requests.
2. You start your client. He wants to exchange information with the server.
3. The client "connects".
At that point, a NEW socket is created on the server, specifically for THAT connection.
Your server can do whatever he wants with that connection to service it - including start a new thread, or fork off a new process.
4. At some point, the server goes back and resumes listening for new connection requests. If he forked a new process, he can do it immediately (both the "server" process and the "service handler" process can run at the same time). Otherwise, you can't read any new connections until you're done servicing the current one (which is often perfectly acceptable).
5. Either way, at some point: the client is done, and he closes his socket. The server is done, and he closes his socket. The connection closes, and everybody's happy.
It's usually really as simple as that!
One other link that might help: Beej's Guide:
Your .. PSM
Thanks again for your help. The problem is that I don't know when the client finished his job, moreover, the server will send some status to the client like a 'hey client, someone pushed my button here!' (obviously, when the client is connected) in a kind of 'online communication'. Unfortunately, I can't use poll :/.
I solved this issue by using select function inside a thread.
Perhaps the following code will serve as a guide:
P.S. The usage of select() is not paramount; the main thing is to examine the return value from recv() to determine if the client sent data or has disconnected. In the example above, select() will block until there is activity on the socket. If this is not desirable, then a timeout period (struct timeval) should be provided at the 5th arg to select(). Otherwise, calling recv() directly on a blocking socket will result in the app being blocked until the client performs some action.
My current code is similar to your suggestion.
I didn't know the timeout parameter, it will be very useful.
|All times are GMT -5. The time now is 08:41 PM.|