I have built a windows service, that connects to a local network device via tcpClient, using an IP and a portnumber. The network device streams out data, that i receive an read. That has been working for MONTHS for multiple setups (Always 1 machine->1 network device).
Now recently almost all the connection-attempts started failing!
The weired thing is, that i can still ping the device via command prompt.
In putty, no data is transferred.
With netstat i didn't see any remote-adress listed.
In my service there is the following error-log message:
Exception by Establishing TCP/IP Connection#ServiceName System.Net.Sockets.SocketException (0x80004005): 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 XXX.XXX.XXX.XX:XXXX (translated into english ;D)
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPEndPoint remoteEP)
at System.Net.Sockets.TcpClient.Connect(IPAddress address, Int32 port)
Is this network-related? Or has it to do with windows-updates or anything?
My up-to-recently working code
public void readDevice()
{
TcpClient Client = new TcpClient();
System.Net.IPAddress ip = System.Net.IPAddress.Parse(ConfigurationManager.AppSettings["IP"]);
/*
* Establishing TCP/IP Connection
*/
try
{
// IP and Port number
log.Debug("Try Client.Connect:" + ip);
Client.Connect(ip, 5000);
}
catch (Exception ex)
{
/* Handle the socket exception.... */
//this error is thrown
log.Debug("Exception by Establishing TCP/IP Connection#ServiceName "+ ex);
}
NetworkStream MessageStream = Client.GetStream();
//Magic be here
}
I am really stuck, because of the ping-able but not connectable device.
Any help greatly appreciated
I don't know how much help this will be, but it sounds like a network problem. If it just stopped working and nothing has changed code wise, I would start investigating there.
I had an issue like this with a server of mine, and the problem ended up being that the port forwarding tables got messed up so I was trying to connect to the wrong machine. I just fixed the forwarding, and blam-o! Everything worked great.
I hope your problem is as easy to solve.
Related
I have a server and a client app, the server throws no exception and seems to listen properly but the clients aren't able to connect for some reasons, I tried with both local network and public IP.
-With local network's IP I can connect to it only when the client app is runned on the same computer than the server.
-With public IP nothing can connect to the server.
However in both cases when the connection fails, it behaves always the same : waits for aproximately 5-10 seconds and then throws System.Net.Socket.SocketException. I tried with port 1507 first then tried with port 80 but it didn't change anything
Server
IPAddress adress = IPAddress.Parse(MyIp);
TcpListener listener = new TcpListener(adress, 80);
listener.Start();
TcpClient Client = await listener.AcceptTcpClientAsync();
Client
TcpClient client = new TcpClient();
client.ConnectAsync(IPAddress.Parse(MyIp), 80).GetAwaiter().GetResult();
I don't understand where the problem comes from, is it coming from my code or not? If not the what could it be and how could I fix it?
Have you tried Telnet from outside network on specific port?
If you can't connect probably is router port redirection or access is blocked by firewall.
I made client & server programs in C# based on this example code.
Server program:
TcpListener tcpListener = new TcpListener(8080);
tcpListener.Start();
First time I ran this, Windows Firewall popped up and asked me if it should be allowed network access.
Client program:
IPHostEntry ip = Dns.GetHostEntry(tbServer.Text);//"MyComputer-MSI"
string addr = ip.AddressList[0].ToString();
TcpClient clientSocket = new TcpClient(addr, 8080);
At the last line above, I got the message:
An unhandled exception of type 'System.Net.Sockets.SocketException'
occurred in System.dll
Additional information: No connection could be
made because the target machine actively refused it
I made sure both programs (Client.exe and Server.exe) were allowed by Windows Firewall. No other antivirus currently enabled (as far as I know).
Checked for conflicts with command netstat -a -b:
[KillerService.exe]
TCP 127.0.0.1:8080 MyComputer-MSI:0 LISTENING
The strange thing is that KillerService.exe (which I can't find in any of the tabs when I CTRL-ALT-DEL) changes its address to match whichever port I use for the server program.
I am using msdn Asynchronous Server Socket Example. link
On this server i am getting data in string format from a device.
The problem is after some time of getting data successfully i get below error and application hang:-
socket exception was unhandled:
An existing connection was forcibly closed by the remote host.
c# debug shows error in this part of code.
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener);
how to resolve the error.
As i understand, server established connection with the client and start sending the data. If this is right than server created socket, bind it and start tcp transmission correctly. Afterwords server destroys the socket due to exception, and connection is lost.
All I can think of is that this problem is related to the message size. You can check buffer-size, and the time-out intervals.
To help resolve error:
Verify you are not having network issues.
Confirm the remote host is not closing the connection (most common cause of this error).
Confirm you are providing a sufficient backlog to handle all your users:
listener.Listen(100);
where 100 = the maximum length of the pending connections in queue.
You can catch the remote host disconnect in your ReadCallback:
SocketError errorCode;
int bytesRead = handler.EndReceive(ar, out errorCode);
if (errorCode != SocketError.Success)
{
bytesRead = 0;
//handle remote host disconnect...
}
If these all check out, then you may have issues else where in your code. You may need to post more details about how you create, start, stop your listener and how the remote host is handling the connection.
so I am currently programming a simple server/client to play some basic games i did in the past.
The problem is that I can only connect while I'm on the same computer, and not via LAN (as I would want it to work), here's the code I'm working with:
Server:
IPEndPoint Ep = new IPEndPoint(IPAddress.Any, 8000);
listener = new TcpListener(Ep);
listener.Start();
Client:
IPAddress direc = IPAddress.Parse(ipManager);
Ep = new IPEndPoint(direc, 8000);
The problem is that when I try connect from another computer (connected to the same Wi-Fi obviously) I get the following error:
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 192.168.0.5:8000
I shuold also add that this is the code when I try to connect:
Sever:
TcpClient client = listener.AcceptTcpClient();
Client:
client.Connect(Ep);
Apparently when I am connecting the server never "accepts" the connection, but I haven't been able to figure out why's that.
Since it works locally, you are probably using 127.0.0.1 or localhost as the server's listening interface. Try using 0.0.0.0 so it will listen on all interfaces or specify the IP address of the interface you want the server to listen on.
Try to connect to the port using telnet or netcat. Check if the port is accessible.
$ nc 192.168.0.5 8000
$ telnet 192.168.0.5 8000
I'm working on a simple hello world TCP/IP client server app in C# and am unable to get my client to connect. Can anyone offer any additional troubleshooting steps? I'm starting to run out of ideas...
Here are the relevant sections of code:
server:
Console.Out.WriteLine("About to bind address");
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
Console.Out.WriteLine("Choose a port to bind...");
String port = Console.In.ReadLine();
int iPort = Int32.Parse(port);
TcpListener myList = new TcpListener(ipAd, iPort);
myList.Start();
Console.WriteLine("The server is running at: "+myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
client:
Console.Out.WriteLine("enter address: ");
string address = Console.In.ReadLine();
Console.Out.WriteLine("enter port: ");
int port = Convert.ToInt32(Console.In.ReadLine());
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
Console.Out.WriteLine("Address: " + address + ":" + port);
tcpclnt.Connect(address, port);
I am able to ping the server from the client machine, however I am unable to telnet to the server using the bound port. I've tried a variety of ports (a few in the low 8000s and a few up around 40000). I have disable windows firewall on both systems. The systems are connected to a router which is not on the internet. I've tried with and without port forwarding set to forward incoming requests on the given port to the server machine with no effect.
The only exception that I've been able to trap is thrown by the client:
No connection could be made because
the target machine actively refuses
it.
I checked for an InnerException but it seems that there are none - that is the base exception. Could that be right?
Not sure what else I should be looking at - any additional troubleshooting steps would be helpful.
Thanks!
I've run into this before. The trick is to bind to 0.0.0.0 rather than 127.0.0.1. When you bind to 127.0.0.1 the server will only accept connections from localhost. Binding to 0.0.0.0 it will accept all requests.
You also may want to nmap the host machine from the client and see what ports it sees as being open.
EDIT: If you hard code the IP address of the machine in it the listener will only listen on that network interface. If you use 0.0.0.0 the listener will listen on all available network interfaces. This includes interfaces between your computer and a USB attached hand held device, a second network card or a VPN link.
The code above is listening to request coming from the loopback address. This will effectively only listen to connection on that network, and that network only includes your machine.
Have you tried listening to the address bound to the network from which the connection should be coming? On a local network it should be something like 192.168.x.x or 10.x.x.x
try netstat -al on your machine (the exact command line varies between Windows and unix) and see if the server is listening on the port
Why don't you use .NET remoting? It is better than doing a TCP/IP client server. You can pass messages between objects.
Have you tried running the client server on the same machine to make sure the connection is made first? Beyond that try using the router assigned or static IP of the machine running the server vs binding to loopback.