No connection could be made because the target machine actively refused it
This error is a network-related error occurred while establishing a connection to the Server. It means that the error is occurring because there is no server listening at the hostname and port you assigned. It literally means that the machine exists but that it has no services listening on the specified port .
Generally, it happens that something is preventing a connection to the port or hostname. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that specific port. This may be because it is not running at all or because it is listening on a different port.
Firewall or Proxy
You might have a firewall rule in the way, or are trying to run it through a proxy without having the proxy up and running. The easiest way to check would be to disable your firewall or proxy and try again.
Disable proxy at web.config fileAlso, check your .config files , mostly this error is caused by a wrong port number you assigned, or incorrect name of the PC (which should be localhost if you are testing everything in one PC).
There are some other possible reasons for this error is 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.
netstat
If none of the above work, try running netstat -anb from the command line to see if there's anything listening on the port you were entered. If you get nothing, try changing your port number and see if that works for you.
- On Linux you may need to do netstat -anp instead.
In Windows operating systems, you can use the netstat services via the command line (cmd.exe) .
If it happens occasionally , it is likely because the server has a full 'backlog' . Regardless of whether you can increase the server backlog , you do need retry logic in your client code, sometimes it cope with this issue; as even with a long backlog the server might be receiving lots of other requests on that port at that time.
If you wrote the server, you might have heavy processing in the accept of your client requests , and it is better moved to a separate worker-thread so your accept is always ready to receive more connection requests. There are various architecture choices you can explore that mitigate queuing up sockets and processing them sequentially.
The target machine actively refused it 127.0.0.1:xxx
This exception message says you're trying to connect to the same host ( 127.0.0.1 ), while you're stating that your server is running on a different host. This 127.0.0.1 represents a 'loopback' . It allows the computer to communicate with itself via network protocol .
Dns.GetHostEntry(IPAddress.Loopback).HostName returns the host name of your machine. When you pass a host name to TcpClient , it will resolve it to one or more IP addresses using Dns.GetHostAddresses(hostName) . This includes the public and local IP addresses of your machine (e.g. 192.168.12.10), but not 127.0.0.1 (loopback address). So your client is trying to connect to any of the non-loopback addresses of your machine, while your server is listening only on the loopback address . So, no connection can be established. The solution to this problem is that connect to the same end point your server is listening on.
- How to send email from C#
- How to send email with attachment from C#
- How to send html email from C#
- How to send cdo email from C#
- How to find hostname of a computer
- How to find IP Adress of a computer
- How to read URL Content from webserver
- How to C# Socket programming
- C# Server Socket program
- C# Client Socket program
- C# Multi threaded socket programming
- C# Multi threaded Server Socket programming
- C# Multi threaded Client Socket programming
- How to C# Chat server programming
- How to C# Chat Server
- How to C# Chat Client
- How to web browser in C#
- System.Net.Sockets.SocketException (0x80004005)
- C# HttpClient - HTTP requests with HttpClient in C#
- C# HttpClient status code