Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Socket Receive Problems

Socket Receive Problems

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpsysadminhelpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    James Smith
    wrote on last edited by
    #1

    Hi All, I am having some problems with .NET Socket Receive Method. I have a client application which I am sending data to a server app. The send works properly, the server receives it. I am expecting a response back from the server, but due to unforseen problems the server doesn't respond. My client application does not block at the Socket.Receive() and loops indefinately waiting for data. I would expect it to block (or wait a specified timeout) until either an exception is thrown (timeout, or closed socket), or data is received. Debugging the connection socket properties on the client reveals that the application thinks the socket is still Connected, and there is no data available. I loaded up WireShark, and traced the connection. It appears that the server is initiating a shutdown, by sending a FIN packet. I see a lower level FIN ACK being sent back to the server from the client, however the application code doesn't seem to detect this. Could this be why the socket doesn't block at the Receive? Is there a way to detect that the server wishes to close the connection? FYI, the client application is currently written in C# using .NET 3.5 framework. Any suggestions would be greatly appreciated. Thanks for your help in advance. Kind Regards, James

    L L 2 Replies Last reply
    0
    • J James Smith

      Hi All, I am having some problems with .NET Socket Receive Method. I have a client application which I am sending data to a server app. The send works properly, the server receives it. I am expecting a response back from the server, but due to unforseen problems the server doesn't respond. My client application does not block at the Socket.Receive() and loops indefinately waiting for data. I would expect it to block (or wait a specified timeout) until either an exception is thrown (timeout, or closed socket), or data is received. Debugging the connection socket properties on the client reveals that the application thinks the socket is still Connected, and there is no data available. I loaded up WireShark, and traced the connection. It appears that the server is initiating a shutdown, by sending a FIN packet. I see a lower level FIN ACK being sent back to the server from the client, however the application code doesn't seem to detect this. Could this be why the socket doesn't block at the Receive? Is there a way to detect that the server wishes to close the connection? FYI, the client application is currently written in C# using .NET 3.5 framework. Any suggestions would be greatly appreciated. Thanks for your help in advance. Kind Regards, James

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      James, you did not specify your protocol or connection method. If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host. If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket..::.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

      led mike

      J 1 Reply Last reply
      0
      • L led mike

        James, you did not specify your protocol or connection method. If you are using a connection-oriented protocol, you must either call Connect to establish a remote host connection, or Accept to accept an incoming connection prior to calling Receive. The Receive method will only read data that arrives from the remote host established in the Connect or Accept method. If you are using a connectionless protocol, you can also use the ReceiveFrom method. ReceiveFrom will allow you to receive data arriving from any host. If no data is available for reading, the Receive method will block until data is available, unless a time-out value was set by using Socket..::.ReceiveTimeout. If the time-out value was exceeded, the Receive call will throw a SocketException. If you are in non-blocking mode, and there is no data available in the in the protocol stack buffer, the Receive method will complete immediately and throw a SocketException. You can use the Available property to determine if data is available for reading. When Available is non-zero, retry the receive operation.

        led mike

        J Offline
        J Offline
        James Smith
        wrote on last edited by
        #3

        Hi Mike, Thanks for the reply. I open the socket connection to the client as follows:

        connectionSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        if (traceFlag)
        {
        LogMessage(Severity.ER_SEVERITY_INFO_RETURN, myFilename,
        "Socket.Open(): Attempting connection to {0}:{1}", hostName, port);
        }

        /*======================================================================*/
        /* Connect our socket to the other end */
        /*======================================================================*/
        try
        {
        // .NET Framework 1.1 format commented out, use new 2.0 connect
        // EndPoint ep = new IPEndPoint(IPAddress.Parse(hostName), (int)port);
        connectionSocket.Connect(hostName, port);
        }
        catch (Exception ex)
        {
        Close();
        throw ex;
        }

        The portion of the receive that isn't behaving is as follows:

        while (bIncoming[0] != (byte)MessageWrapper.SOM)
        {
        // Loop Waiting for and Start Of Message Character
        tryRead = 1;
        read = connectionSocket.Receive(bIncoming, 0, tryRead, SocketFlags.None);
        }

        The above receive loops forever, with zero bytes received. When I debug the properties of "connectionSocket" I see Connected = true and Available is 0. I would imagine this is because the server has initiated a shutdown, however, it appears the framework is not detecting it. Hopefully the code snippet helps. Let me know if you need more info. Thanks for your help. Kind Regards, James

        1 Reply Last reply
        0
        • J James Smith

          Hi All, I am having some problems with .NET Socket Receive Method. I have a client application which I am sending data to a server app. The send works properly, the server receives it. I am expecting a response back from the server, but due to unforseen problems the server doesn't respond. My client application does not block at the Socket.Receive() and loops indefinately waiting for data. I would expect it to block (or wait a specified timeout) until either an exception is thrown (timeout, or closed socket), or data is received. Debugging the connection socket properties on the client reveals that the application thinks the socket is still Connected, and there is no data available. I loaded up WireShark, and traced the connection. It appears that the server is initiating a shutdown, by sending a FIN packet. I see a lower level FIN ACK being sent back to the server from the client, however the application code doesn't seem to detect this. Could this be why the socket doesn't block at the Receive? Is there a way to detect that the server wishes to close the connection? FYI, the client application is currently written in C# using .NET 3.5 framework. Any suggestions would be greatly appreciated. Thanks for your help in advance. Kind Regards, James

          L Offline
          L Offline
          liRetro
          wrote on last edited by
          #4

          I have found that timeouts and keep-alive messages are a must when using socket connections. I know this doesn't answer your question, but did you know that if you have 3 switches in between the client and the server, then the middle switch dies, and your applications will still think the connections are fine until they try to send a message? The spec. for the keep-alive of the so called connection-oriented TCP/IP is optional.

          Assert(this);

          J 2 Replies Last reply
          0
          • L liRetro

            I have found that timeouts and keep-alive messages are a must when using socket connections. I know this doesn't answer your question, but did you know that if you have 3 switches in between the client and the server, then the middle switch dies, and your applications will still think the connections are fine until they try to send a message? The spec. for the keep-alive of the so called connection-oriented TCP/IP is optional.

            Assert(this);

            J Offline
            J Offline
            James Smith
            wrote on last edited by
            #5

            I just tried setting "KeepAlive" socket option to true but it didn't have any affect. The bizzare part here is that the lower level (seen through the network trace) shows that the connections is being shutdown by the server. The network layer of the client responds with an acknowledgement, but the Framework doesn't seem to detect it. It is very possible, (and probable) that i am doing something wrong in my code. I have found in the past that it is difficult to detect that a remote host has disconnected when sending data... Send always seems to succeed unless the TCP buffer is full at the network layer. The "detection" usually comes when the application tries the next receive... but alas.. not in this case...

            1 Reply Last reply
            0
            • L liRetro

              I have found that timeouts and keep-alive messages are a must when using socket connections. I know this doesn't answer your question, but did you know that if you have 3 switches in between the client and the server, then the middle switch dies, and your applications will still think the connections are fine until they try to send a message? The spec. for the keep-alive of the so called connection-oriented TCP/IP is optional.

              Assert(this);

              J Offline
              J Offline
              James Smith
              wrote on last edited by
              #6

              Interestingly enough, your post got me thinking, and I added the bold code below.

              while (bIncoming[0] != (byte)MessageWrapper.SOM)
              {
              // Loop Waiting for and Start Of Message Character
              tryRead = 1;
              read = connectionSocket.Receive(bIncoming, 0, tryRead, SocketFlags.None);
              Byte[] b = new byte[1];
              b[0] = 0x30;
              connectionSocket.Send(b, 1, SocketFlags.None);

              }

              Now, I get an exception that the host has aborted the transaction on the second time through the loop! The first receive appears to be successful, the send is successful, then the subsequent receive fails. Any idea how to create/catch the exception without the need of a Send? The send will mess up the server when the server does decide to respond properly. Thanks for your help. James

              L 1 Reply Last reply
              0
              • J James Smith

                Interestingly enough, your post got me thinking, and I added the bold code below.

                while (bIncoming[0] != (byte)MessageWrapper.SOM)
                {
                // Loop Waiting for and Start Of Message Character
                tryRead = 1;
                read = connectionSocket.Receive(bIncoming, 0, tryRead, SocketFlags.None);
                Byte[] b = new byte[1];
                b[0] = 0x30;
                connectionSocket.Send(b, 1, SocketFlags.None);

                }

                Now, I get an exception that the host has aborted the transaction on the second time through the loop! The first receive appears to be successful, the send is successful, then the subsequent receive fails. Any idea how to create/catch the exception without the need of a Send? The send will mess up the server when the server does decide to respond properly. Thanks for your help. James

                L Offline
                L Offline
                led mike
                wrote on last edited by
                #7

                James Smith wrote:

                The send will mess up the server when the server does decide to respond properly.

                James Smith wrote:

                I would imagine this is because the server has initiated a shutdown, however, it appears the framework is not detecting it.

                How is the send going to mess up the server if it already shutdown the socket which caused the whole thing to start with? :confused:

                led mike

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups