-
June 15th, 2009, 06:21 PM
#1
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
-
June 24th, 2009, 04:11 PM
#2
Re: send() return value
Originally Posted by stratoforce
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
-
June 25th, 2009, 09:58 PM
#3
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.
-
June 27th, 2009, 04:51 AM
#4
-
June 27th, 2009, 08:54 PM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|