471,075 Members | 1,332 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,075 software developers and data experts.

Socket.Send() succeeds, but remote end point closed the connection

Hi,

Here is my client/server scenario:
Step1: Client connects to server and sends data sucessfully (using
Socket.Send()).
Step2: Server gracefully exists (calls Socket.Shutdown() and
Socket.Close()). I see the server connection status go from ESTABLISHED to
FIN_WAIT2, the client connection go from ESTABLISHED to WAIT_CLOSE.
Step3: client.Send() succeeds and return the number of bytes sent (over the
closed connection!)
Step4: same as step3, Send() throws, which is expected, but was expected in
step3

Question 1: Shouldn't step3 behave like step4? If step3 can happen, then the
transport is not reliable.
Question 2: Is there a socket status event the client can subscribe to?

Thanks
Jun 13 '06 #1
2 12809
yvan wrote:
Hi,

Here is my client/server scenario:
Step1: Client connects to server and sends data sucessfully (using
Socket.Send()).
Step2: Server gracefully exists (calls Socket.Shutdown() and
Socket.Close()). I see the server connection status go from ESTABLISHED to
FIN_WAIT2, the client connection go from ESTABLISHED to WAIT_CLOSE.
Step3: client.Send() succeeds and return the number of bytes sent (over the
closed connection!)
Step4: same as step3, Send() throws, which is expected, but was expected in
step3

Question 1: Shouldn't step3 behave like step4? If step3 can happen, then the
transport is not reliable.
Question 2: Is there a socket status event the client can subscribe to?

....

This is by design of TCP protocol.

What you describe is not graceful close by server. In CLOSE_WAIT state,
send is allowed. CLOSE_WAIT means that other side has shutdown it's
outgoing side of connection (you will not receive any data on that
connection), but you may send until you shutdown your side (remember
that tcp is full-duplex). Server should not exit or call close on socket
until it has receive shutdown from other side (it has to make transition
from FIN_WAIT2 to TIME_WAIT state).

Gracefull close is:
Server calls Shutdown with SD_SEND
client receives close notification (read will return 0)
client may send data and call shutdown with SD_SEND when finished
server must loop in read until it receives close notification (receive 0
from read). Only then it may close socket.

Only if you close gracefully, you are sure data is delivered. Please
note that there are linger options that can be set which affect this
Check in MSDN:

http://msdn.microsoft.com/library/de..._closure_2.asp

Send does not ensure data is sent, it ensures only it is buffered by the
OS (TCP protocol). IOW, number of bytes returned from send call > 0 does
not mean data is received by other side.
Regards,
Goran
Jun 14 '06 #2
Goran,

Thanks for taking the time to answer, this is really helpful.

"Goran Sliskovic" wrote:
yvan wrote:
Hi,

Here is my client/server scenario:
Step1: Client connects to server and sends data sucessfully (using
Socket.Send()).
Step2: Server gracefully exists (calls Socket.Shutdown() and
Socket.Close()). I see the server connection status go from ESTABLISHED to
FIN_WAIT2, the client connection go from ESTABLISHED to WAIT_CLOSE.
Step3: client.Send() succeeds and return the number of bytes sent (over the
closed connection!)
Step4: same as step3, Send() throws, which is expected, but was expected in
step3

Question 1: Shouldn't step3 behave like step4? If step3 can happen, then the
transport is not reliable.
Question 2: Is there a socket status event the client can subscribe to?

....

This is by design of TCP protocol.

What you describe is not graceful close by server. In CLOSE_WAIT state,
send is allowed. CLOSE_WAIT means that other side has shutdown it's
outgoing side of connection (you will not receive any data on that
connection), but you may send until you shutdown your side (remember
that tcp is full-duplex). Server should not exit or call close on socket
until it has receive shutdown from other side (it has to make transition
from FIN_WAIT2 to TIME_WAIT state).

Gracefull close is:
Server calls Shutdown with SD_SEND
client receives close notification (read will return 0)
client may send data and call shutdown with SD_SEND when finished
server must loop in read until it receives close notification (receive 0
from read). Only then it may close socket.

Only if you close gracefully, you are sure data is delivered. Please
note that there are linger options that can be set which affect this
Check in MSDN:

http://msdn.microsoft.com/library/de..._closure_2.asp

Send does not ensure data is sent, it ensures only it is buffered by the
OS (TCP protocol). IOW, number of bytes returned from send call > 0 does
not mean data is received by other side.
Regards,
Goran

Jun 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robert A. van Ginkel | last post: by
5 posts views Thread by Morten | last post: by
6 posts views Thread by Bruce Vander Werf | last post: by
5 posts views Thread by John Sheppard | last post: by
13 posts views Thread by coloradowebdev | last post: by
4 posts views Thread by schwehr | last post: by
2 posts views Thread by manasap | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.