Am Donnerstag, 12. Dezember 2002 13:43 schrieb Kai-Uwe Schmidt:
-----Ursprüngliche Nachricht----- Von: dries [mailto:dries@irssystems.nl] Gesendet: Donnerstag, 12. Dezember 2002 17:33 An: SuSE Programming English Betreff: Re: [suse-programming-e] How to check in which state is a
socket
?
Kai-Uwe Schmidt wrote : | Hi folks, | | i have a server process which collects socket connects from clients.
Those
| connects are stored in a chain. On a special event on the server,
the
| process has to deliver some data to the clients. So far so good,
but..
| how do i recognize when i client disconnects, or how can i check the
state
| of the socket ? my process crash when it tries to send/write data on
the
| old-socket handle. it all written in C, so there is no way to use
stuff
| like try() catch() and i dont beleave, i should write an own signal
handler
| for this write/send statement. there must be a clean way to
check/monitor
| the state of the socket, but how ?
You can determine if a socket is closed if a read from a socket that
says
there is data (via select) returns a number of 0 bytes on a read.
There is also a trick reading zero bytes from the socket (that won't
block
and can be done at all times) and then checking the the nr of bytes
written.
I don't know exactly how to do it but try something like this to find
out
unsigned char c; int res;
res = read(socket,&c,0); printf("%d\n",res);
i think res will be negative if socket is not available anymore (you
might
even be able to check errno to see what happened).
[Kai-Uwe Schmidt] hm, this sounds like an idea, but my tests show that status=select(iClientConnect+1,NULL,&fs_write,NULL,&timeout) returns 1 and status=read(iClientConnect,&c,0) returns 0. Even when I do a system("netstat -an | grep <myport>"); before. The system() shows nothing after 20 sec where I killed the client-connect. Means IMHO that the kernel doesnt know anything about the socket anymore.
But iWritten=send(iClientConnect,"Test",strlen("Test"),0) crashes the process even without writing a core file.
No clue how to proceed, sounds like a bug in my eyes.
Kai-Uwe
Hot Tip W. Richard Stevens UNIX Network Programming in german Programmieren von Unix -Netzwerken robert