'No connection could be made because the target machine actively refused it - c#

I am running a client application which should connect to a server side application in .NET.
Are there any restrictions on the number of connections a listening port can receive on the server?
I'm asking because I got an exception Message:
No connection could be made because the target machine actively refused it 1.9.64.212:62131

The error "No connection could be made because the target machine actively refused it" means that your request got through so it is not a firewall issue. This message means that noone listens to this port. Make sure your server application is running and that it listens to the right port number (and IP address). On your server computer you can run "netstat -a -b" to find port numbers and server applications that listen to those ports.

There are some possible reasons for that:
The server is not running. Hence it wont listen to that port. If it's a service you may want to restart the service.
The server is running but that port is blocked by Windows Firewall or other firewall. You can enable the program to go through firewall in the Inbound list.
There is a security program on your PC, i.e a Internet Security or Antivirus that blocks several ports on your PC.

I've seen this message when your service throws an unhandled exception, so check that.

This must be a firewall issue (or another connectivity issue), or the server was not started correctly.

Related

C# Program Listening on 0.0.0.0 but not 192.168.x.x

I have a software made in C# that opens up a listener on port 5000 to listen over the network for incoming traffic. To test to make sure it is working I have a program with the following code set:
Int32 port = 11000;
IPAddress localAddr = IPAddress.Parse("192.168.x.x")
client = new TcpClient();
client.Connect(localAddr, port);
Per my netstat -an my listener program is listening as this:
0.0.0.0:11000 * LISTENING
It is not showing listening on 192.168.X.X.
When I run the test program it gives me a socket 80004005 error saying the connection was actively refused. When I run the test program on another computer it says it does not receive a signal back and times out. When I check the firewall log it says that nothing was blocked. I even have firewall rules to allow the port and even the .exe just to make sure it gets all traffic.
I have another computer with the listening software on it and it is showing this with netstat
192.168.x.x:11000 * LISTENING
0.0.0.0:11000 * LISTENING
For this computer it is working fine. The test program will work without a problem.
If the firewall is not the problem as I can see through logs and the rules allow all traffic to the listening program why is it being actively refused and not talking back? And why on this machine is it not listening on the 192.168.x.x address as the working one?
I can't say yet exactly what's going on, but I can suggest some things to check:
Remember there are two firewalls involved: the server's firewall to limit incoming connections, and also your local machine's firewall limiting outbound connections. Make sure to check both of those; sometimes uncommon outbound ports will be blocked.
You say netstat shows your listener using 0.0.0.0:1000. This indicates port 1000. So far I've seen in the question and edit history attempts connecting to ports 5000 and 11000, but never 1000.
Make sure your antivirus software isn't interfering. It may see this traffic on unusual ports as indicating a virus. In fact, speedguide indicates port 1000 has been used by trojans in the past.
Found the issue. The person who installed the software did not restart the system after the firewall rules and installation of the software was complete. I will be sure to hit them with a mouse fail as I asked them to do that multiple times.
Once restarted it started listening on the 192.168.x.x address and all was working.

No such host is known —> System.Net.Http.HttpRequestException:

I am developing bot solution, in which I am making REST call to get some details from other server.
In local host my code is working fine, but after publishing the code in Azure I am getting the below error:
System.Net.WebException: No such host is known No such host is known —> System.Net.Http.HttpRequestException: No such host is known —> System.Net.Sockets.SocketException: No such host is known
I am using HttpWebRequest option.
May I know what could be the reason?
I'm not sure the solution below will work for you but it looks like the solution is worth to be checked out. Here is an answer that may have useful information. Take a look at this answer and check your solution accordingly:
The problem description:
I deployed a .NET Windows Service on Azure Virtual Machine running
Windows Server with an opened port that allow me to connect to it,
this service is like a server using socket. The problem is that when I
try to connect from my PC to that hosted server it doesn't work and I
get this exception: System.Net.Sockets.SocketException: 'No such host
is known'. The port is opened and visible from my PC. Can someone tell
me why I get that exception? If I run locally all works ok.
The answer:
The Exception seems to be a DNS issue. I am not familiar with C#, from
networking, you could check the followings on your side:
Windows service is running and the port is listening on the Azure VM.
The port is allowed for outbound traffic from your PC and Inbound
traffic on your Azure VM. Check the VM firewall settings on both sides
between your PC and Azure VM. Also, you could check the NSG settings
follow this. You could use telnet vmpublicIP port on your PC CMD to
verify network connectivity.
Verify your PC can resolve the address of the hosted server if you connect
it via its DNS name. You could use the NSlookup or DIG to verify this.
If it's DNS issue, you also could add a line in hosts file (located in
%WINDIR%\system32\drivers\etc on windows) mapping the hosted server's
IP to a name. Hope this helps.
.NET Service System.Net.Sockets.SocketException: 'No such host is known'

I can't connect to the server (C#)

I have Server and Client scripts in C# programming language. When I create server on my computer and from another computer I'm trying to connect to the server it says:
an existing connection was forcibly closed by the remote host
It's happening when I change the IP of the server to 0.0.0.0 (IPAdress.Any)
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
It' happening when I change the IP of the server to IPv4 which is 10.0.0.6 (On the ipconfig - IPv4).
Also I turned off the Firewall, And I opened port (11000) on website of the router.
I put in the client the public IP address of the server (which is on whatismyip.com).
The program works perfectly when it is on the local network (I mean when the both computers have the same network).
Scripts:
Server code : http://pastebin.com/awsrkjk4
Client code : http://pastebin.com/2Xrm8YnA
Does anyone know how to solve it?
First of all, check to make sure your server is actively listening on the TCP port you expect. If it is a Windows machine, from the command line try:
netstat -ano | findstr ***LISTENINGPORT***
In your case I'm guessing you should be listening on 11000.
If so, on the client side, let me know exactly where your code is breaking. I'm assuming for now it's this:
sender.Connect(new IPEndPoint(IPAddress.Parse(ip), 11000));
.. and also run a WireShark capture on the client machine.
There are plenty of quick start-up guides - basically this will allow you to see the request your Client is making across the network, & see if a connection (if any) is being established on the server.
If you ARE making a connection, then it is just being closed for another reason, then you have a code issue. If NOT, then you have a network/configuration issue. This should help you make progress.

I can't get TcpClient to connect to a TcpListener on another host

I have a simple client and server application that talk to each other over TCP/IP using TcpClient and TcpListener. If I run both the server and the client on the same computer with an IP address of 192.168.10.150, they can talk to each other just fine. However, if I move the client to a different computer on my home network, it can’t connect to the server any more (which is still running on 192.168.10.150). From the client computer, I can ping 192.168.10.150. I have turned off Windows Firewall on both computers. I am using port 2244, which I don’t think is used by something else. The two computers are both connected to the same wireless router. But this line of code in the client:
TcpClient client = new TcpClient("192.168.10.150", 2244);
Throws the following exception:
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.10.150:2244
Both computers are running XP. Any ideas how I get these to connect?
Thanks for the responses. It seems that a couple of you are correct that the network traffic was being stopped by the router. I hooked up the two computers with a switch, and they were able to talk. Unfortunately I haven't been able to successfully configure the router to allow the traffic, but that's OK, as I can just use the switch for now.

How to know if tcp server is in listening state or not

I am developing one tcp server application using tcp listener in Windows service using c#.
After starting my service it works fine and accepts new clients. After some time when ever the new client is trying to connect to server it gives the following exception:
connection failed, is the server running? No connection could be made
because the target machine actively refused it.
At that time the service is in running state only.
How can I verify the tcp server is in listening state or not?
netstat -ab
This command will give you information about all active connections with name of the programs.
Another option is TcpView from Mark Russinovich
If your server is still running and hasn't close the listening socket, it is still in listening state. However if the platform is Windows and your server's backlog queue is full, a windows will refuse further connections. That means that your server is slow accepting new connections.
Not sure if you mean programatically or not, but for the latter, you can try telnet: telnet host port
telnet yahoo.com 80
telnet localhost 3306
etc.
If you want to check if the server is listening, and the server is Windows, you can simply use (e.g. 8888 is your listening port)
netstat -ano|findstr "8888"
Then you'll see the PID of your server instance.
In addition, if you want to check why the TCP connection attempt is fail, you could use Wireshark to capture all the TCP packets between server & client, then the reason will be quite clear.
E.g. if the server has reached the limitation of open socket number, you'll see a SIN from client to server, and how the server is responding.

Categories

Resources