How to check in which state is a socket ?
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 ?
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). Grtz Dries -- <End of message>
-----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,
| 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
| 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 the 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
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
Kai-Uwe Schmidt wrote : | > 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 Well, imho there should be a kernel routine to see what status your connection is in, but there isn't. I got the read zero - bytes trick from the web somewhere. I myself however use the 'select sees there is data to be read but we read 0 bytes' trick, that works for me. I'll try to see where i got the read 0 bytes trick from ! Grtz dries -- <End of message>
-----Ursprüngliche Nachricht----- Von: dries [mailto:dries@irssystems.nl] Gesendet: Donnerstag, 12. Dezember 2002 23:06 An: 'SuSE Programming English' Betreff: Re: AW: [suse-programming-e] How to check in which state is a socket ?
Kai-Uwe Schmidt wrote :
| > even be able to check errno to see what happened). | | [Kai-Uwe Schmidt] hm, this sounds like an idea, but my tests show
| 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
| the kernel doesnt know anything about the socket anymore. | | But iWritten=send(iClientConnect,"Test",strlen("Test"),0) crashes
| process even without writing a core file. | | No clue how to proceed, sounds like a bug in my eyes. | | Kai-Uwe
Well, imho there should be a kernel routine to see what status your connection is in, but there isn't. I got the read zero - bytes trick from the web somewhere. I myself however use the 'select sees there is data to be read but we read 0 bytes' trick, that works for me. [Kai-Uwe Schmidt] I found out, that my programm works on a 2.2.19 Kernel but crashes on 2.4.19-4GB and 2.4.7-4GB (Slox Distri and SEMS3.1 Distri). On those distributions the select() returns always 1, even when
that that the the socket vanishes from the kernel. Select() returns 0 on a e.g. 2.2.19. Kai-Uwe
Kai-Uwe Schmidt wrote : | [Kai-Uwe Schmidt] I found out, that my programm works on a 2.2.19 Kernel | but crashes on 2.4.19-4GB and 2.4.7-4GB (Slox Distri and SEMS3.1 | Distri). On those distributions the select() returns always 1, even when | the socket vanishes from the kernel. | Select() returns 0 on a e.g. 2.2.19. | | Kai-Uwe Ooh. . thanx for the info, this will probably mean i need to change my app if we upgrade the server running these programs using the select trick !! grtz Dries -- <End of message>
participants (4)
-
dries
-
Kai-Uwe Schmidt
-
Kai-Uwe Schmidt
-
Robert Jenni