Bug 25365 - Setting ReceiveTimeout property of TCPClient before connecting has no effect
Summary: Setting ReceiveTimeout property of TCPClient before connecting has no effect
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System (show other bugs)
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2014-12-13 23:33 UTC by Greg Najda
Modified: 2017-08-10 09:13 UTC (History)
3 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Greg Najda 2014-12-13 23:33:34 UTC
Setting the ReceiveTimeout property of a TCPClient object before connecting has no effect. Setting the ReceiveTimeout property after connecting works as expected.

The bug looks to be caused by Connect (IPAddress[] ipAddresses, int port) and Connect (string hostname, int port) (which calls the previous overload) containing the code

if (values != 0) {
    SetOptions ();
}

but the Connect (IPEndPoint remoteEP) and Connect (IPAddress address, int port) overloads do not call SetOptions().

Here is a test program demonstrating the bug. It can be run in 3 modes: "S" for server, "C1" for client setting ReceiveTimeout before connecting, and "C2" for client setting ReceiveTimeout after connecting. To demonstrate the bug, compile and run "mono TCPClientRepro.exe" and response "S" to the prompt to start the server. Then in another console run "mono TCPClientRepro.exe" again and response "C1" to the prompt. The client sets a timeout of 1 second before connecting, the server sleeps 3 seconds before sending the client a message, but the client does not get a timeout error even though it should. Verify that setting the timeout after connecting works by running it and responding with "C2". Running with "C2" makes the client timeout as expected.

Both C1 and C2 timeout on MS .NET. I am running mono 3.2.8.




using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace TCPClientRepro
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("S for server, C1 for client setting ReceiveTimeout before connecting (which has no effect, and is the bug this program demonstrates), C2 for client setting ReceiveTimeout after connecting. Clients should get a timeout error but C1 does not.");
            string response = "";
            while (!response.Equals("C1", StringComparison.OrdinalIgnoreCase) && !response.Equals("C2", StringComparison.OrdinalIgnoreCase) && !response.Equals("S", StringComparison.OrdinalIgnoreCase))
            {
                response = Console.ReadLine();
            }

            if (response.Equals("C1", StringComparison.OrdinalIgnoreCase))
            {
                Client(setTimeoutBeforeConnect: true);
            }
            else if (response.Equals("C2", StringComparison.OrdinalIgnoreCase))
            {
                Client(setTimeoutBeforeConnect: false);
            }
            else
            {
                Server();
            }
        }

        private static void Server()
        {
            // Server: Create socket listening on 9843, when connected, sleep 3 seconds, send "Hello" and close.
            using (Socket listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                listeningSocket.Bind(new IPEndPoint(IPAddress.Loopback, 9843));
                listeningSocket.Listen(10);
                while (true)
                {
                    Console.WriteLine("Listening for a connection...");
                    using (Socket socket = listeningSocket.Accept())
                    {
                        Console.WriteLine("Sleeping for 3 seconds...");
                        Thread.Sleep(3000);
                        Console.WriteLine("Sending message");
                        string message = "Hello";
                        byte[] messageBytes = Encoding.UTF8.GetBytes(message);
                        SocketSendAll(socket, messageBytes);
                        Console.WriteLine("Message sent.");
                    }
                }
            }
        }

        private static void Client(bool setTimeoutBeforeConnect)
        {
            //Client 1: Create TCPClient, set receive timeout to 1 second, connect to 127.0.0.1:9843, read all from stream, convert to string using UTF8, print
            //Client 2: Create TCPClient, connect to 127.0.0.1:9843, set receive timeout to 1 second, read all from stream, convert to string using UTF8, print
            using (TcpClient client = new TcpClient())
            {
                if (setTimeoutBeforeConnect)
                {
                    Console.WriteLine("Setting read timeout to 1 second.");
                    client.ReceiveTimeout = 1000;
                }
                Console.WriteLine("Connecting");
                client.Connect(IPAddress.Loopback, 9843);
                Console.WriteLine("Connected");
                if (!setTimeoutBeforeConnect)
                {
                    Console.WriteLine("Setting read timeout to 1 second.");
                    client.ReceiveTimeout = 1000;
                }
                Console.WriteLine("Reading from server. This should trigger a timeout exception.");
                using (NetworkStream stream = client.GetStream())
                using (MemoryStream buffer = new MemoryStream())
                {
                    stream.CopyTo(buffer);
                    string message = Encoding.UTF8.GetString(buffer.ToArray());
                    Console.WriteLine("Message from server: {0}", message);
                }
            }
        }

        private static void SocketSendAll(Socket socket, byte[] bytes)
        {
            int numSent = 0;
            while (numSent < bytes.Length)
            {
                int numSentThisTime = socket.Send(bytes, numSent, bytes.Length - numSent, SocketFlags.None);
                numSent += numSentThisTime;
            }
        }
    }
}
Comment 1 xambugz 2015-01-27 09:44:45 UTC
This may or may not be related - the TcpClient returned by TcpListener.AcceptTcpClient() does not timeout properly, even though the timeout is set on the TcpClient after connection was established.  Have not yet looked at source to figure out why.

Note You need to log in before you can comment on or make changes to this bug.