C# TcpClient Connect() gets timeout for unknown reason - c#

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.
The server is running and listening for new connections
I am able to ping the server from all my devices including my Android device
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
When using telnet from my Android device I also see the server receive the connection and say that a new connection has been established
I have rebooted both my Android device as well as the server machine
Firewalls are completely disabled on the server machine
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.

Related

UWP server application connect from same machine fails with 127.0.0.1

I'm developing an UWP app which needs to be the server for a win32 client application, which is not to be changed if possible. Now the problem is I can connect to my uwp server application if I connect to the hostname or IP address of my computer. In contrast if I use localhost or 127.0.0.1 I can't get a connection.
I'm running this command constantly to enable the connection without running it, it doesn't work at all. "CheckNetIsolation.exe LoopbackExempt -is -n=Packagename"
I also executed some tests, and found out that on the computer under company IT, it only works when using the IP address. Therefore with the identical setup on a computer which is not under the companies IT, it works as expected.
So I expect there's a problem with the way my machine is configured.
We have an proxy for all outgoing connections. Another thing when I use a win32 server and client I have no problem to connect to localhost, so it seems that my machines dns configuration is in general right.
Do you have any ideas how to torubleshoot this, or what I can try to find out where my connection attempt is going. Maybe the UWP application is trying to connect over the proxy?

Connect two devices with websockets

I am new to websockets, please excuse me if the answer to my question is too obvious.
I'm using websocket-sharp, as my goal is to build a Unity3D client that connects to a server. (Actually I want to build it for webGL platform, so the client will be written in javascript). For now, both client and server are written in C#.
When compiled on localhost it works perfectly, also with multiple clients and also over a secure connection using a self-signed certificate. It also works fine if the client connects to an echo server (ws://echo.websocket.org).
So I moved forward and I tried to connect two of my computers. The idea was to run the server on linux and execute the client on a mac, so I substituted "localhost" for the linux IP on both client and server codes. When I execute it, Unity (client) keeps saying me "Not a local IP address" and it doesn't connect.
I thought this could be a firewall issue, and that first I should open the port used... Is that the case? If so, how can I open a concrete port on mac and on linux?
Check the bindings.
WebSocketServer(System.Net.IPAddress.Any, port);
Test if you can make a connection to ip:port.
iptables -L for listing the configured rules in Linux.
Finally I solved it. It was an issue with the server code, I don't know where was the error. Instead of using my c# server I used Node.js, and it works fine. I can connect from other computers without problems.

Set up TCP/IP connection over the internet issue

Hi,
I'm trying to connect my chat client to my server, but I keep getting an error. I have chat server which is running in a machine which is published in internet and it has fixed IP address and it is running on port 5001. When my chat client tries to connect to that server then I am get the error. The line which is raising error as follows
ipAddr = IPAddress.Parse(txtIp.Text);
// Start a new TCP connections to the chat server
tcpServer = new TcpClient();
tcpServer.Connect(ipAddr, 5001);
When I run both my chat client & server in my local machine with local machine IP, it works.
Please tell me what I need to changes in code as a result my chat server & client both can communicate over the internet. Thanks
Fixed IP might not be enough if it goes first through some network of computers.
You might need to Forward Port if it is simple network, or if it is more complicated probably it has own direct line but have closed Ports so you have to open them through Firewall within system as well within possible hardware that can filter traffic.
It probably not the code issue at all.
As well when you create server socket, try to Bind it to IPAddress.Any, because if you bind it to specific device it might be source of problem as well.

Error Code 10060 in a Winforms application using Sockets (TCP)

I have a server program and a client program. I built it based from the appliation from this tutorial. The thing is, it works (connects successfully and communicates) when I run both the program on the same computer. But when I tried running the client app in a different computer from where the server runs and connect it to the server app, this exception happens:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 169.254.212.31:1986
I made sure that the client app will connect to the IP Address that the Server App listens to. I am running my application in a LAN. What should I do to resolve this problem?
UPDATE:
I'm sorry, forgot to put the link of the tutorial I was talking about. Here it is.
Solved the problem by opening a port (in my case, Port 1986).
I followed this guide:
How to open a port in windows 7 firewall

Network connections only work if device connected via cable

Ok, I tried for the last couple hours and I give up:
I develop for a mobile device (Win CE on Unitech HT660) and have a weird thing occurring:
I try to communicate with a service on my PC and I'm using TCPClient for it.
This works great except for one big problem:
Once I unplug the USB-Cable I use to copy the files from VS on program start TCPClient throws a SocketException that no socket connection could be made because the target machine actively refused it (not the case, Firewall is off, no third-party installed and the service is listening)
And it gets weirder: If the cable is plugged in and I remove it after the program made the connection everything works perfectly fine, I can send and receive data without the cable, I can just not connect without the cable.
Btw: It's the same story with MySQLConnection from the MySQL .NET Connector. It works with the cable, but if it is removed without an established connection no connection can be made.
Has anyone ideas on that?
Thanks in advance!
PS: One thing I forgot to mention: I use wireless to connect to my network, and IE as well as pinging the computer with the service and mysql database on it works.
If you're using Vista / Win7, check if the wireless network is set as private / office network.
Having it set to public network causes very weird symtoms.
Thanks for all the good advice, however in the end it was nothing with my code or the network or the server.
I checked it on another mobile device with no changes and it works.
Seems that the Unitech HT660 has some issue with TCP/IP connections under .Net.
Googled this and I'm not the only one with this problem...

Categories