tags:

views:

66

answers:

2

hi,

how to inform the server that is a client is interrupted, and close the socket?

Thanks!

+4  A: 

If the other end of a socket is closed, your end will be marked as readable and return 0 from read - this is the "end of file" indication.

If you try to write to such a socket, you will recieve the SIGPIPE signal, and the write will return error with errno set to EPIPE ("Broken Pipe"). You must be prepared to handle this event, because the other end can close the socket at any time.

caf
Thanks! could you plz give more details on how to do it? also when e.g, ctrl-c is pressed on the side of client, should we return also a SIGINt signal? thanks again for your help
Apollo
@Apollo Signals aren't sent between different computers
qrdl
If ctrl-c is used to terminate the client application, the OS will close the connection and the server process will be able to detect that as an end-of-file condition.
caf
Isn't this too X-nix specific?
sevity
Thanks! actually I am working on different OSs (Unix and win_32). How to inform the server which is running windows that client is interrupted? Can you plz help me? thanks again
Apollo
A: 

You need TCP_KEEPALIVE

vitaly.v.ch
thanks! but if ctrl-c is pressed , how should we deal with it?
Apollo
Keepalive (SO_KEEPALIVE is name of socket option, btw) has nothing to do with it. OP didn't ask about detecting half-opened connections
qrdl
thanks! I know that Keepalive function has nothing to do with interruption ...
Apollo