How to find out if TCP Listener is listening?

Asked

Viewed 94 times

3

Library that I use: https://github.com/rdavisau/sockets-for-pcl

Follows code:

await client.ConnectAsync(address, port);

The code above is the client and works normal. Let’s say the server is off, when I run the above code, it takes a short time and I get error:

Connection timed out

It takes about 3 minutes to execute this exception. Is there any way to know if the server is listening before executing the above code ? Or decrease 3 minutes to 5 seconds.

Here’s the answer to how to get the status: /a/285103/54019

  • changes the timeout ?

  • 1

    in system.net.sockets.tcpclient just change: TCPClient.ReceiveTimeout = 5000; TCPClient.SendTimeout = 5000; In the one you’re wearing, there must be an equivalent

  • 1

    is because it’s using a different library, I don’t know it and I don’t know if it’s the same as system.net.sockets.tcpclient. That’s all =]

1 answer

2


You can change the Timeout of the client:

TCPClient.ReceiveTimeout = 5000; 
TCPClient.SendTimeout = 5000;

Note that a very short timeout can cause problems during communication.

  • Rovann, your code only works in synchronous mode. Now that I’ve realized :/

  • 1

    In asynchronous mode I had to do so: await client.ConnectAsync(address, port, true, new CancellationTokenSource(TimeSpan.FromSeconds(5)).Token);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.