Re: socket read,recv / signal handling

phil-news-nospam_at_ipal.net
Date: 04/14/04


Date: 14 Apr 2004 20:28:24 GMT

On 13 Apr 2004 10:47:09 GMT Florian Zschocke <edv@zschocke-berlin.de> wrote:
| Materialised <Materialised@privacy.net> wrote:
|>
|>int nbytes, sockfd;
|>char buf[1000]; //Or whatever
|>
|>//... rest of code
|>if ((nbytes = recv(sockfd, buf, sizeof(buf), 0)) <= 0) {
|> perror("recv");
|> // Do whatever else you wanna do
|>}
|>
| Thank you,
| I did test that before- sorry thought I wrote that.
| There is no value <= 0 and the socket is gone.
| In the debugger there is the last statement: recvfrom16 and the program quits with bad_access.

Are you saying that after the socket is closed by the server, that doing
a recv() gives you a return value that is not -1? If there is any error
at all, it should give you a -1 return value and put more information as
to what the error is in the variable errno. A socket closed on the other
end would be an error once the fact of that closure has reached the host
the recv() call is taking place on.

Is your program design doing a main loop waiting for some kind of event
or I/O status? A typical setup in Unix is a loop within which select()
or poll() is called on multiple file descriptors, and that call wakes up
when a condition is ready on one or more of them (or an optional timeout).
The peer closing a socket would be such an event, and doing a recv() on
that socket would get the -1 return value, and errno would have the code
for the error reason.

If OS X is not returning -1 on error in recv(), then it does not conform
to POSIX.

-- 
-----------------------------------------------------------------------------
| Phil Howard KA9WGN       | http://linuxhomepage.com/      http://ham.org/ |
| (first name) at ipal.net | http://phil.ipal.org/   http://ka9wgn.ham.org/ |
-----------------------------------------------------------------------------


Relevant Pages

  • Re: Synchronization with CAsyncSocket in CE 6.0
    ... number of bytes in the first packet, followed by packets of, say 1024 bytes. ... recv() operations, reassembling it into the format that you need. ... don't forget that recv can return zero, if the socket has ... So, CAsyncSocket while possibly a bad way to do things, does not appear to ...
    (microsoft.public.windowsce.embedded)
  • Re: Receive no data
    ... It is a best practice to shutdown ... a socket for sending once all data has been sent. ... I see multiple recv() statements in the code. ...
    (microsoft.public.win32.programmer.networks)
  • RE: BUG in Winsock on P4 HT CPU
    ... If you read the help on WSAAsyncSelect it states ... Network transport stack receives 100 bytes of data on socket s and causes ... The application issues recv(s, buffptr, 50, 0) to read 50 bytes. ... on CPU's that do not support HT, or CPUs with HT disabled. ...
    (microsoft.public.win32.programmer.networks)
  • Re: recv() hangs until SIGCHLD ?
    ... Both lsof and ls /proc//fd show that the socket used is in ESTABLISHED mode but when checking on the host on which it's connected we can't find the corresponding client socket. ... We are correctly handling EINTR in sendand recv() by restarting the call in case they get interrupted this way. ... However since this problem does not occur without threads, we can be sure that the blame is still on the receiver. ... In a practical case, we have a thread blocked in recvfor more than 12 hours, which is way beyond the timeout of the sender connection. ...
    (Linux-Kernel)
  • Re: how to avoid recv() blocking issue?
    ... using WSAEventSelect. ... we have a weak link in a chain: a timeout. ... drop the connection abnormally such that the blocking recv will never ... TerminateThread- btw, it's not a good idea, because a socket ...
    (microsoft.public.win32.programmer.networks)

Quantcast