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.










share|improve this question





















  • 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










  • Ahh figured it out, thanks!
    – colmde
    Nov 9 at 16:28














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.










share|improve this question





















  • 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










  • Ahh figured it out, thanks!
    – colmde
    Nov 9 at 16:28












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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















  • 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










  • 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












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.






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer
























      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.






      share|improve this answer






















        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 16:32









        colmde

        1,60211025




        1,60211025



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus