2 years ago

#59604

test-img

Hannah.C

Socket.Send/ReceiveTimeout is not working

I am wondering if the timeout mechanism is working in my APP.
So I make an experiment to check and I found that the socket timeout of the following in my code not working!

  1. Socket Connect timeout

    To check the timeout is working or not, my phone is not connected to the test device, the log print S4 to S5 only spends about 100-200 ms but not 2 sec.
    My imagining result: S4 -> (wait for 2 sec) -> S5.
    The actual result: S4 -> (spend 100-200 ms) -> S5.

  2. Socket Send timeout

    To check the timeout is working or not, my phone is connected to the device at the beginning, then I power off the test device at Log-S6. Then I will get an exception with Log-S10 and no print Log-S7. The timeout I thought will be 4 sec but log print S7 to S10 spends about 1-6 sec randomly.
    My imagining result: S7 -> (wait for 4 sec) -> S10.
    The actual result: S7 -> (spend 1-6 sec randomly) -> S10.

  3. Socket Receive timeout

    To check the timeout is working or not, my phone is connected to the device and sends the illegal command to the test device, so the test device will not respond. The timeout I thought will be 7 sec but log print S7 to S8 spends about 6-10 sec randomly.
    My imagining result: S7 -> (wait for 7 sec) -> S8.
    The actual result: S7 -> (spend 6-10 sec randomly) -> S8.

Have I missed something to be declared?
Or am I misunderstanding the meaning of each timeout setting?

string Log_Record = "";
public byte[] SocketConnectCommendd(string host, int portd, byte[] sendBytes)
        {
            try
            {
                Log_Record += "S1";
                IPAddress ip = IPAddress.Parse(host);
                IPEndPoint ipe = new IPEndPoint(ip, portd);
                Socket sk = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                sk.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, true);
                sk.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, true);

                sk.SendTimeout = 4000;
                sk.ReceiveTimeout = 7000;
                Log_Record += "S2";
                IAsyncResult result = sk.BeginConnect(ip, portd, null, null);

                Log_Record += "S3";
                bool success = result.AsyncWaitHandle.WaitOne(2000, true);
                Log_Record += "S4";

                if (!success)
                {
                    Log_Record += "S5";
                    sk.Close();
                    throw new ApplicationException("Failed to connect server.");
                }

                Log_Record += "S6";
                sk.Send(sendBytes, sendBytes.Length, 0);
                Log_Record += "S7";

                rcvlend = sk.Receive(buffersd, buffersd.Length, 0);
                Log_Record += "S8";
                byte[] rcvdata = new byte[rcvlend];
                for (int i = 0; i < rcvlend; i++)
                {
                    rcvdata[i] = buffersd[i];
                }

                sk.Close();
                Log_Record += "S9";
                return rcvdata;
            }
            catch (Exception ex)
            {
                Log_Record += "S10";
                return null;
            }
        }

c#

sockets

timeout

0 Answers

Your Answer

Accepted video resources