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 ()
Version: unspecified
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-12-13 23:33 UTC by Greg Najda
Modified: 2017-08-10 09:13 UTC (History)
3 users (show)

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 25365 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

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

        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));
                while (true)
                    Console.WriteLine("Listening for a connection...");
                    using (Socket socket = listeningSocket.Accept())
                        Console.WriteLine("Sleeping for 3 seconds...");
                        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, read all from stream, convert to string using UTF8, print
            //Client 2: Create TCPClient, connect to, 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;
                client.Connect(IPAddress.Loopback, 9843);
                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())
                    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.