Skip to content
Advertisement

How to ensure a posix c socket is still valid

I,m working on an embedded linux kernel 2.6 device and need to know if previously established socket is still valid or not,Also I can not do this with usual send function and check the returned value,because if I send to the invalid socket descriptor,my application will crash and linux will shut down my process.Is there any other function/suggestion for this ?

EDIT: There are an installed app manager in device and when I try to send to socket descriptor which is not refer to the open socket,app manager will end my application,then if i close a socket connection and try to write to it,my application will be turned off by lower level app-manager.Also I’m using TCP sockets,WBr.

Advertisement

Answer

I think this question is either misstated or based on false premises. There is no sense of “invalidity” which a socket could come to have asynchronously by the action of another process/host. The closest thing is probably the other end of the socket being closed, which does not invalidate your socket, but it does cause subsequent writes to your socket to result in an EPIPE error and SIGPIPE signal if not blocked. SIGPIPE in turn terminates your process by default. If that’s your problem, the easiest way to avoid it is to block SIGPIPE with sigprocmask/pthread_sigmask, or ignore it with signal(SIGPIPE, SIG_IGN).

Advertisement