Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No connection could be made because the target machine actively refused it?

Sometimes I get the following error while I was doing HttpWebRequest to a WebService. I copied my code below too.


 System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:80    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)    at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP)    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)    --- End of inner exception stack trace ---    at System.Net.HttpWebRequest.GetRequestStream() 

ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);  request.PreAuthenticate = true; request.Credentials = networkCredential(sla); request.Method = WebRequestMethods.Http.Post; request.ContentType = "application/x-www-form-urlencoded"; request.Timeout = v_Timeout * 1000;  if (url.IndexOf("asmx") > 0 && parStartIndex > 0) {     AppHelper.Logger.Append("#############" + sla.ServiceName);      using (StreamWriter reqWriter = new StreamWriter(request.GetRequestStream()))     {                                 while (true)         {             int index01 = parList.Length;             int index02 = parList.IndexOf("=");              if (parList.IndexOf("&") > 0)                 index01 = parList.IndexOf("&");              string parName = parList.Substring(0, index02);             string parValue = parList.Substring(index02 + 1, index01 - index02 - 1);              reqWriter.Write("{0}={1}", HttpUtility.UrlEncode(parName), HttpUtility.UrlEncode(parValue));               if (index01 == parList.Length)                  break;               reqWriter.Write("&");              parList = parList.Substring(index01 + 1);          }      }  }  else  {      request.ContentLength = 0;  }   response = (HttpWebResponse)request.GetResponse(); 
like image 850
hsnkvk Avatar asked Jun 04 '10 08:06

hsnkvk


People also ask

How do you fix no connection could be made because the target machine actively refused it?

You can try below solution: 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.

How do you fix no connection could be made because the target machine actively refused it 10061?

1-Disable WMI services : run - services. msc - Windows Management Instrumentation(WMI) - stop the service. 2-Delete the files under C:\Windows\System32\wbem\Repository 3-Open regedit: Go to HKEY_LOCAL_MACHINE > Software and HKEY_CURRENT_USER > Software. Delete the Palo Alto Networks folder.

Could not connect to server No connection could be made because the target machine actively refused it Connection failed Winscp?

“No connection could be made because the target machine actively refused it.” 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.


2 Answers

If this happens always, it literally means that the machine exists but that it has no services listening on the specified port, or there is a firewall stopping you.

If it happens occasionally - you used the word "sometimes" - and retrying succeeds, it is likely because the server has a full 'backlog'.

When you are waiting to be accepted on a listening socket, you are placed in a backlog. This backlog is finite and quite short - values of 1, 2 or 3 are not unusual - and so the OS might be unable to queue your request for the 'accept' to consume.

The backlog is a parameter on the listen function - all languages and platforms have basically the same API in this regard, even the C# one. This parameter is often configurable if you control the server, and is likely read from some settings file or the registry. Investigate how to configure your server.

If you wrote the server, you might have heavy processing in the accept of your socket, and this can be better moved to a separate worker-thread so your accept is always ready to receive connections. There are various architecture choices you can explore that mitigate queuing up clients and processing them sequentially.

Regardless of whether you can increase the server backlog, you do need retry logic in your client code to 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.

There is a rare possibility where a NAT router would give this error should its ports for mappings be exhausted. I think we can discard this possibility as too much of a long shot though, since the router has 64K simultaneous connections to the same destination address/port before exhaustion.

like image 178
Will Avatar answered Sep 22 '22 06:09

Will


The most probable reason is a Firewall.

This article contains a set of reasons, which may be useful to you.

From the article, possible reasons could be:

  • FTP server settings
  • Software/Personal Firewall Settings
  • Multiple Software/Personal Firewalls
  • Anti-virus Software
  • LSP Layer
  • Router Firmware
  • Computer Turned Off
  • Computer Not Plugged In
  • Fiddler
like image 28
Chathuranga Chandrasekara Avatar answered Sep 20 '22 06:09

Chathuranga Chandrasekara