Network connections only work if device connected via cable - c#

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...

Related

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.
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.

Failover to 3G and back

I'm developing an app for remote monitoring of a small power plant. The idea is to keep the remote connection all the time.
We hava a small PC with broadband connection and in addition 3G/4G modem attached.
Checking the connectivity when I have broadband is quite easy for me.
fist I check if the interface that Im using is up, and then just with simple web client i check the response from google or any other site that I assume that should be online :-)
Im using System.Net.NetworkInformation.NetworkInterface.*
Then if there is no connection over the lan network Im connecting to 3G modem and starting 3G connection.
The problem is how to check if my lan connection again got the internet connection and disconnect 3G modem? How to check that particular interface from C# code that it has internet access?
Can I make some manual routing trough the interfaces, or its done somewhere inside the OS it self?
Some advice? Someone has got similar project?
Well you could fetch the Lan connection profile with the same library as the Profile Name is "Ethernet" and then to check you'll first need to connect to a LAN connection and then check and if the connection has internet access the stay else you'll have to switch back. I tried it it takes roughly 30 seconds to do a full route

Error in connecting wcf service from android. Error: Failed to connect to /10.0.2.2

While calling wcf ksoap2 from android.
htp.call(SOAP_ACTION, soapEnvelop);
I am getting this exception.
java.net.ConnectException: failed to connect to /10.0.2.2 (port 52442)
after 20000ms: connect failed: ENETUNREACH (Network is unreachable)
My code was working fine till last night but now its not.
Thanks in advance
It's obvious it's not any router-firewall related problem as you are under the same net, so there are only three possibilities:
There's nothing listening on that port on that IP
There's a local firewall on that machine that is blocking that connection attempt
You are not using WIFI so you're not under the same net.
Can you open that URL from your browser in your computer manually? If yes, I'd suggest using some debugging tool to trace TCP packets (I don't know either what kind of operating system you use on the destination machine; if it's some linux distribution, tcpdump might help).
All that assuming you have the android.permission.INTERNET permission in your AndroidManifest.xml file.

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

WP7: TCP connection from phone to PC via USB

I'm trying to get a connection via TCP sockets from my Phone that is plugged via USB to the PC. It does not seem to work. The Socket.ConnectAsync returns a Success, so the hostname (name of my PC) is correctly resolved. However, the first Socket.SendAsync ends up in a Connection Reset.
It works with the emulator and if the PC and phone are in the same private WiFi network, but never via the USB connection.
This is kind of strange, since HTTP traffic seems to work quite well: The phone's browser can connect to my PC's webserver (with both PC and phone beeing offline, just connected via USB) and a small test app can connect via HTTP (WebClient) to the PC, like asked in this SO question here.
So my question is, as there must be some security restriction that I did not discover yet, or some technical restriction: How can I get it to work, and if not, why?
Thanks in advance for any help with this!
Have you tried doing a connection using the Socket class to the web server (ie. port 80) on your computer?
I'm pretty sure the USB Connection Sharing for WP7 is restricted on the ports that it actually shares. I could be wrong, of course, but this would explain your situation.

Categories