Thread: recv returns with 0

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    15

    recv returns with 0

    Hello.

    I have a strange problem with TCP sockets. I have a client which connects to the server and sends some 8-byte string message. the send() function returns value 8. Now on the server side when the recv() function unblocks it returns 0, meaning the client has disconnected. But that is not the case since the client loops and no close() or shutdown() was called.

    I've also checked the packets with sharkwire and it turns out there was no error at all. There's the 3-way handshake upon connection and then the PSH packet from the client. After that the server responses with ACK and that's that. I made a syscall trace but between connect() and recvfrom() (or sendto() on the client side) there is no single call to close() on the socket.

    What could be wrong?

    Greets,
    Luka

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    code?

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    The code is too large to post. With syscall trace I can assume a simple tcp server/client code can be taken. I'm not doing anything special witch the code. Just initialization, connection and sending/recieving. All other functions return without errors(socket(), connect(), bind(), accept(), listen()). The problem is just with the recv() on the server side. It should not return 0 because I'm not calling close() anywhere.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    You sure you couldn't trim it down? You could attach it or post it up on a file sharing site.

    It's kind of hard to figure it out without the code.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    I'm afraid trimming down the source takes too much time. But like I said the could could look as a simple tcp server/client.

    I've made some more packet inspection and found out that after the server ACK-s the receiving packet, it then sends a RST,ACK packet back to the client and terminates the connection.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Are you passing any values for the 'flags' ?
    What is the value of errno when it returns 0?
    Is the socket non-blocking?

    > I'm afraid trimming down the source takes too much time.
    How big was the code before you noticed this problem?

    If it's just appeared, then look at the differences with the previous version.
    If you don't have a previous version, consider this to be a good lesson in the value of a source control system which always allows you to get back to a "known good state".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Quote Originally Posted by Salem View Post
    Are you passing any values for the 'flags' ?
    What is the value of errno when it returns 0?
    Is the socket non-blocking?
    The sockets are blocking. When receiving, I pass MSG_WAITALL to recv(), the send() flag is 0. The errno value is 2.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    > Is the socket non-blocking?

    I thought this too, but the man page said it would return -1 and set errno to EAGAIN on nonblock, not return 0.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Well there are several "modules" that use the same network library for network IO. The strange thing is while other modules work, it's only when I began writing a new module this error appeared.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Is the code multithreaded? Do you have well defined boundaries for how to handle networking code across threads and/or "modules"?

    > The errno value is 2.

    You have to find out what error that corresponds to on your system. When did it get set?
    Last edited by robwhit; 07-28-2008 at 10:06 AM.

  11. #11
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Quote Originally Posted by robwhit View Post
    Is the code multithreaded? Do you have well defined boundaries for how to handle networking code across thread and/or "modules"?

    > The errno value is 2.

    You have to find out what error that corresponds to on your system. When did it get set?
    It's "No such file or directory"

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    When did it get set? Keep in mind that errno values stick around until you clear them.

    Does the firewall on the server allow you to connect to that port with that address?
    Last edited by robwhit; 07-28-2008 at 10:20 AM.

  13. #13
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    It's likely that no error codes are set since a return value of 0 basically means the connection was 'gracefully' closed at the other end. Try calling recv without the MSG_WAITALL (or any) flag set. Also, if the network library is being used in a multithreaded context, make sure there aren't any globals/statics being thrashed. Also, have you tried running the server on a different machine to see if the results were the same?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recv() returns 0 the second time
    By Overlord in forum Networking/Device Communication
    Replies: 7
    Last Post: 07-10-2009, 04:09 AM
  2. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 02:10 PM
  3. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  4. recv()
    By afisher in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-24-2004, 05:32 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM