Socket.Receive blocking even after ShutDown() by client
up vote
1
down vote
favorite
I have a server application with a socket listening:
server = new TcpListener(myEndPoint);
// etc.
socket = server.AcceptSocket();
while (true)
int size = socket.Receive(data); // application waits here for data.
if (size == 0)
// handle disconnect
else
// process data.
Now the client application connects to this...
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteIp, remotePort);
sends the data, and when it's done it calls
socket.ShutDown(SocketShutdown.Both);
At this point, I'd expect the server, which was blocking at socket.Receive(data) to proceed, but with a size of 0.
as per this page:
https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receive?view=netframework-4.7.2
If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.
But it just waits here forever.
The funny thing is, if I connect to the server application with PuTTY, and then close PuTTY, then the Receive() method does complete and return with size = 0. What's PuTTY doing that I'm not?
Note: I've also tried using a TcpClient on the client application but this has the same problem.
c# sockets
add a comment |
up vote
1
down vote
favorite
I have a server application with a socket listening:
server = new TcpListener(myEndPoint);
// etc.
socket = server.AcceptSocket();
while (true)
int size = socket.Receive(data); // application waits here for data.
if (size == 0)
// handle disconnect
else
// process data.
Now the client application connects to this...
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteIp, remotePort);
sends the data, and when it's done it calls
socket.ShutDown(SocketShutdown.Both);
At this point, I'd expect the server, which was blocking at socket.Receive(data) to proceed, but with a size of 0.
as per this page:
https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receive?view=netframework-4.7.2
If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.
But it just waits here forever.
The funny thing is, if I connect to the server application with PuTTY, and then close PuTTY, then the Receive() method does complete and return with size = 0. What's PuTTY doing that I'm not?
Note: I've also tried using a TcpClient on the client application but this has the same problem.
c# sockets
socket.ShutDown disables Send/Receive on the socket. Can you try socket.Close() to close the connection.
– Minu
Nov 9 at 15:49
@Minu - Actually I've just noticed that it works fine if I don't send any data and then either Close() or Shutdown(). However if I do send data and call either, then Receive() continues blocking. I can see from the debugger that theAvailableproperty of the socket is 7 if I've sent data but 0 if not...
– colmde
Nov 9 at 16:10
Ahh figured it out, thanks!
– colmde
Nov 9 at 16:28
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a server application with a socket listening:
server = new TcpListener(myEndPoint);
// etc.
socket = server.AcceptSocket();
while (true)
int size = socket.Receive(data); // application waits here for data.
if (size == 0)
// handle disconnect
else
// process data.
Now the client application connects to this...
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteIp, remotePort);
sends the data, and when it's done it calls
socket.ShutDown(SocketShutdown.Both);
At this point, I'd expect the server, which was blocking at socket.Receive(data) to proceed, but with a size of 0.
as per this page:
https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receive?view=netframework-4.7.2
If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.
But it just waits here forever.
The funny thing is, if I connect to the server application with PuTTY, and then close PuTTY, then the Receive() method does complete and return with size = 0. What's PuTTY doing that I'm not?
Note: I've also tried using a TcpClient on the client application but this has the same problem.
c# sockets
I have a server application with a socket listening:
server = new TcpListener(myEndPoint);
// etc.
socket = server.AcceptSocket();
while (true)
int size = socket.Receive(data); // application waits here for data.
if (size == 0)
// handle disconnect
else
// process data.
Now the client application connects to this...
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(remoteIp, remotePort);
sends the data, and when it's done it calls
socket.ShutDown(SocketShutdown.Both);
At this point, I'd expect the server, which was blocking at socket.Receive(data) to proceed, but with a size of 0.
as per this page:
https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.receive?view=netframework-4.7.2
If the remote host shuts down the Socket connection with the Shutdown method, and all available data has been received, the Receive method will complete immediately and return zero bytes.
But it just waits here forever.
The funny thing is, if I connect to the server application with PuTTY, and then close PuTTY, then the Receive() method does complete and return with size = 0. What's PuTTY doing that I'm not?
Note: I've also tried using a TcpClient on the client application but this has the same problem.
c# sockets
c# sockets
asked Nov 9 at 15:34
colmde
1,60211025
1,60211025
socket.ShutDown disables Send/Receive on the socket. Can you try socket.Close() to close the connection.
– Minu
Nov 9 at 15:49
@Minu - Actually I've just noticed that it works fine if I don't send any data and then either Close() or Shutdown(). However if I do send data and call either, then Receive() continues blocking. I can see from the debugger that theAvailableproperty of the socket is 7 if I've sent data but 0 if not...
– colmde
Nov 9 at 16:10
Ahh figured it out, thanks!
– colmde
Nov 9 at 16:28
add a comment |
socket.ShutDown disables Send/Receive on the socket. Can you try socket.Close() to close the connection.
– Minu
Nov 9 at 15:49
@Minu - Actually I've just noticed that it works fine if I don't send any data and then either Close() or Shutdown(). However if I do send data and call either, then Receive() continues blocking. I can see from the debugger that theAvailableproperty of the socket is 7 if I've sent data but 0 if not...
– colmde
Nov 9 at 16:10
Ahh figured it out, thanks!
– colmde
Nov 9 at 16:28
socket.ShutDown disables Send/Receive on the socket. Can you try socket.Close() to close the connection.
– Minu
Nov 9 at 15:49
socket.ShutDown disables Send/Receive on the socket. Can you try socket.Close() to close the connection.
– Minu
Nov 9 at 15:49
@Minu - Actually I've just noticed that it works fine if I don't send any data and then either Close() or Shutdown(). However if I do send data and call either, then Receive() continues blocking. I can see from the debugger that the
Available property of the socket is 7 if I've sent data but 0 if not...– colmde
Nov 9 at 16:10
@Minu - Actually I've just noticed that it works fine if I don't send any data and then either Close() or Shutdown(). However if I do send data and call either, then Receive() continues blocking. I can see from the debugger that the
Available property of the socket is 7 if I've sent data but 0 if not...– colmde
Nov 9 at 16:10
Ahh figured it out, thanks!
– colmde
Nov 9 at 16:28
Ahh figured it out, thanks!
– colmde
Nov 9 at 16:28
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Turns out it was my fault, I'll leave it here in case someone else has the same problem.
While trying Minu's suggestion I noticed that if I just closed the socket without having sent data, the Receive() completed correctly.
It turns out that elsewhere in my code, I had the server sending back an acknowledgement after receiving the completed data. However, the client's method to receive the acknowledgement had a bug in it that was causing it not to completely receive it from the socket and so there were still 7 bytes Available on the socket which is why it wasn't closing.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Turns out it was my fault, I'll leave it here in case someone else has the same problem.
While trying Minu's suggestion I noticed that if I just closed the socket without having sent data, the Receive() completed correctly.
It turns out that elsewhere in my code, I had the server sending back an acknowledgement after receiving the completed data. However, the client's method to receive the acknowledgement had a bug in it that was causing it not to completely receive it from the socket and so there were still 7 bytes Available on the socket which is why it wasn't closing.
add a comment |
up vote
0
down vote
Turns out it was my fault, I'll leave it here in case someone else has the same problem.
While trying Minu's suggestion I noticed that if I just closed the socket without having sent data, the Receive() completed correctly.
It turns out that elsewhere in my code, I had the server sending back an acknowledgement after receiving the completed data. However, the client's method to receive the acknowledgement had a bug in it that was causing it not to completely receive it from the socket and so there were still 7 bytes Available on the socket which is why it wasn't closing.
add a comment |
up vote
0
down vote
up vote
0
down vote
Turns out it was my fault, I'll leave it here in case someone else has the same problem.
While trying Minu's suggestion I noticed that if I just closed the socket without having sent data, the Receive() completed correctly.
It turns out that elsewhere in my code, I had the server sending back an acknowledgement after receiving the completed data. However, the client's method to receive the acknowledgement had a bug in it that was causing it not to completely receive it from the socket and so there were still 7 bytes Available on the socket which is why it wasn't closing.
Turns out it was my fault, I'll leave it here in case someone else has the same problem.
While trying Minu's suggestion I noticed that if I just closed the socket without having sent data, the Receive() completed correctly.
It turns out that elsewhere in my code, I had the server sending back an acknowledgement after receiving the completed data. However, the client's method to receive the acknowledgement had a bug in it that was causing it not to completely receive it from the socket and so there were still 7 bytes Available on the socket which is why it wasn't closing.
answered Nov 9 at 16:32
colmde
1,60211025
1,60211025
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228758%2fsocket-receive-blocking-even-after-shutdown-by-client%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
socket.ShutDown disables Send/Receive on the socket. Can you try socket.Close() to close the connection.
– Minu
Nov 9 at 15:49
@Minu - Actually I've just noticed that it works fine if I don't send any data and then either Close() or Shutdown(). However if I do send data and call either, then Receive() continues blocking. I can see from the debugger that the
Availableproperty of the socket is 7 if I've sent data but 0 if not...– colmde
Nov 9 at 16:10
Ahh figured it out, thanks!
– colmde
Nov 9 at 16:28