Hello,
How do I cleanly close a socket from the client? I listen on a port, accept a connection, and dump a whole bunch of data out to it. I then telnet into that port and get the data, but have no way of aborting with ctrl-c or anything.
Alternatively, once I close telnet, I could keep checking the client via ping or something and close the connection if I don't get a response.
Any thoughts on this?
Thanks!
Christoph
Close socket from client
Re: Close socket from client
You should read from the socket after you write your data. As soon as you close the telnet, the client will close its connection and a FIN will be sent your server. When the server gets this it will return from the read call with EOF.
'ping'ing the client will do you no good. ping is done with ICMP messages and has nothing to do with any open(closed) sockets. As long as your client is on the network and responds to ICMP echo messages you will always get a response.
If this is always going to be a one way data transfer, and the amount of data you are sending is less than the MTU, you might consider using UDP in lieu of TCP. You lose the overhead of establishing a TCP connection and the need to worry about the state of that socket. A UDP design would simplify things to an event/response model, i.e. your client sends a UDP packet to server requesting the data, your server sends a UDP packet back with the data, end of story.
'ping'ing the client will do you no good. ping is done with ICMP messages and has nothing to do with any open(closed) sockets. As long as your client is on the network and responds to ICMP echo messages you will always get a response.
If this is always going to be a one way data transfer, and the amount of data you are sending is less than the MTU, you might consider using UDP in lieu of TCP. You lose the overhead of establishing a TCP connection and the need to worry about the state of that socket. A UDP design would simplify things to an event/response model, i.e. your client sends a UDP packet to server requesting the data, your server sends a UDP packet back with the data, end of story.