CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2009
    Posts
    12

    send() return value

    Hello, i got a question, and would be nice if someone can help me.

    1) Is it safe to closesocket() right after send() in case send returns the correct ammount of bytes sent?

    I thought it was, but now im thinking maybe im wrong.
    After having problems with a program im writing, i did some research and it seems that the return value of send (bytes sent if no error) is the ammount of bytes "sent" to some TCP buffer, wich will later send the bytes to the peer.

    2) So, closing the socket right after send() can make it impossible to the peer to recv if the socket has been closed before the bytes were actually sent through the network?

    Thanks in advance.

    ****************************
    Solved

    The answer is (in case someone is interested)
    Yes, that can happen.
    Last edited by stratoforce; June 22nd, 2009 at 05:15 PM. Reason: Solved

  2. #2
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: send() return value

    Quote Originally Posted by stratoforce View Post
    Is it safe to closesocket() right after send() in case send returns the correct ammount of bytes sent?
    The right sequence of closing a TCP connection without loosing data is:

    Call shutdown() (how parameter = 1) when you finish sending the data.
    Call closesocket() after recv() returns 0.



    Regards
    Doron Moraz

  3. #3
    Join Date
    May 2009
    Posts
    12

    Re: send() return value

    Thanx for the reply

    I had a problem where the sending process closed the socket too early.
    This happens once every 1000 packets, sometimes after 4k, its very random.

    Im talking about custom packets, with a couple of fields, being the relevant ones for this post the "length" and some binary content.
    So, i have been trying stuff and there are 2 solutions that worked.

    1)I made the receiving process to do a shutdown() only after it received all the data, the entire packet ( i know the lenght because it is included at the beginning of each packet).
    Then i do an aditional recv() on the sending process, and close the socket only after it returned zero.
    Dunno if this is the correct way, but it worked.

    2) Close the socket in the receiving process after it received all the data, then do an aditional recv() in the sending process, and close the socket only after it returned zero.
    This approach workted too.

    Maybe in this particular situation im having, shutdown() is not necesary. Any opinions/advices?
    I feel there's something about shutdown im missing.

    Btw, parameter 1 (SD_RECV) also sends a FIN?
    Last edited by stratoforce; June 25th, 2009 at 10:34 PM.

  4. #4
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: send() return value

    Hi stratoforce,

    Take a look at:
    http://msdn.microsoft.com/en-us/libr...81(VS.85).aspx


    Reagrds
    Doron Moraz

  5. #5
    Join Date
    May 2009
    Posts
    12

    Re: send() return value

    Thanks

    I had already read that, but reading it again + testing some stuff cleared things.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured