[en] Timeout

Topics: Technical Support
Mar 31 at 10:18 PM
Hi...

I have this piece of code:
try
{
    var _protocol = new TcpDuplexClientProtocolSetup(encryption: true);
    _protocol.MaxAttempts = 1;
    var _address = _protocol.FormatUrl(_syncIpPort.IpAddress, _syncIpPort.Port, SharedFunctions.ServerName);
    var _setup = new ZyanConnectionSetup();

    m_Connection = new ZyanConnection(_address, _protocol);

    DataFunctions = m_Connection.CreateProxy<IDataFunctions>();
}
catch (Exception ex)
{
    var _a = 10;
}
How can I use/put a timeout when the connection takes to much time to connect to server?

Once that when the client does not connect, its don´t fire any exceptions and the code stay blocked.

How to solve this?

Regards...

Nelson
Coordinator
Apr 1 at 11:45 AM
Hi Nelson,
 
its don´t fire any exceptions and the code stay blocked
 
It shouldn't stay blocked, Socket.Connect method throws an exception when it's timed out.
The timeouts for TCP connections are set on the operating system level, as far as I remember.
The default timeout for connecting to localhost is like 5-10 seconds, for other hosts 30-60 seconds.

In my application I create ZyanConnection asynchronously, so that it doesn't block the main thread.
The easiest way to do it is like this:
var connection = await Task.Run(() => new ZyanConnection(...));
Hope that helps!

Regards, Alex
Apr 4 at 6:34 PM
Edited Apr 4 at 6:36 PM
Hi Alex...

First sorry, but I forget to explain some points of my problem.

1) On this case, I'm using Android as a server and client.
2) When I said "blocked" there is nothing with the UI, but with the program. It's freeze and won't process another subsequents methods.
3) I can wait more than 60 seconds and it does not fire any exception. This wait does not have a finish.

Can you help me with this?

I can wait until you come back from your vacation. Enjoy it. ;-)

Best regards

Nelson
Coordinator
Apr 17 at 12:02 AM
Hi Nelson,

I'm sorry I can't access my development machine right now. But I'll try to address your points.
 
When I said "blocked" there is nothing with the UI, but with the program. It's freeze and won't process another subsequents methods.
 
That's right, Socket.Connect method call is blocking. When an application calls it, it freezes until the connection is established (or not established due to an error or a timeout). That's why I suggest to call it from the worker thread using Task.Run(...) so your app stays responsive.
 
I can wait more than 60 seconds and it does not fire any exception. This wait does not have a finish.
 
That's very strange... By the way, does your application manifest requests all the proper permissions required for using the raw sockets? Does it connect properly when you specify the proper address of a working server or it simply always freezes no matter what?

If Socket.Connect method freezes forever when connecting to an invalid address, I don't really know how to deal with that. Of course, using tasks, you can always cancel any operation after a timeout, but in this case it looks like leaking a file handle (not nice, but may be the only option):
        var timeout = TimeSpan.FromSeconds(10);
        var task = Task.Run(() => connection = new ZyanConnection());

        Console.WriteLine("Connecting to server using a 10-second timeout...");
        if (await Task.WhenAny(task, Task.Delay(timeout)) == task)
        {
            // connected within the timeout
            Console.WriteLine("Success!");
        }
        else
        {
            // timeout logic
            Console.WriteLine("Timeout!");
        }
Let me know what you think.

Regards, Alex