简体   繁体   中英

C# TcpClient Connect() gets timeout for unknown reason

I have had code that has been working fine for almost two years untouched which connects to a server that I created. Suddenly, I can't get the TcpClient to connect any more.

The client that is connecting runs on my Android device (Android Version 6.0).

TcpClient tcpClient = new TcpClient();
bool success = tcpClient.ConnectAsync(ServerIP, Port).Wait(TimeSpan.FromSeconds(5));
if (success)
    stream = tcpClient.GetStream();

This task always returns false. I tried using the blocking function tcpClient.Connect() and it gets a timeout exception.

  1. The server is running and listening for new connections
  2. I am able to ping the server from all my devices including my Android device
  3. I am able to connect to the server using telnet from my Android device by passing in the same exact address and port as I do in my code above
  4. When using telnet from my Android device I also see the server receive the connection and say that a new connection has been established
  5. I have rebooted both my Android device as well as the server machine
  6. Firewalls are completely disabled on the server machine
  7. I have not tried restarting the router/modem. I want to find the source of the problem if at all possible.

I even ran the server with Visual Studio attached to see if it receives any sort of connection before disposing of it, but it never receives anything.

Any ideas?

[UPDATE]

I wrote a test application on my computer to try to connect using the same exact code I have written above. It did not connect using the given IP address. But, I changed the IP to the servers local IP and it connected.

With that, I tried connecting with my Android without Wifi and it still did not work.

So what would prevent this from connecting using an actual IP address instead of a local?

[ANSWER]

I tried resetting my router/modem and that did not work. So, I changed the local IP on the server to something else (was using 192.168.0.2 , I changed it to 192.168.0.4 ) and it worked.

I double checked to make sure there was no IP conflicts on my network and all seemed fine, so I am not sure why 192.168.0.2 was causing a failure to connect.

All is good now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM