Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No connection could be made because the target machine actively refused it 127.0.0.1:3446

I'm using the WCF4.0 template -REST. I'm trying to make a method that uploads a file using a stream.

The problem always occur at

Stream serverStream = request.GetRequestStream(); 

Class for streaming:

namespace LogicClass {     public class StreamClass : IStreamClass     {         public bool UploadFile(string filename, Stream fileStream)         {             try             {                 FileStream fileToupload = new FileStream(filename, FileMode.Create);                 byte[] bytearray = new byte[10000];                 int bytesRead, totalBytesRead = 0;                 do                 {                     bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);                     totalBytesRead += bytesRead;                 } while (bytesRead > 0);                  fileToupload.Write(bytearray, 0, bytearray.Length);                 fileToupload.Close();                 fileToupload.Dispose();             }             catch (Exception ex) { throw new Exception(ex.Message); }             return true;         }     } } 

REST project:

[WebInvoke(UriTemplate = "AddStream/{filename}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare)] public bool AddStream(string filename, System.IO.Stream fileStream) {     LogicClass.FileComponent rest = new LogicClass.FileComponent();     return rest.AddStream(filename, fileStream); } 

Windows Form project: for testing

private void button24_Click(object sender, EventArgs e) {     byte[] fileStream;     using (FileStream fs = new FileStream("E:\\stream.txt", FileMode.Open, FileAccess.Read, FileShare.Read))     {         fileStream = new byte[fs.Length];         fs.Read(fileStream, 0, (int)fs.Length);         fs.Close();         fs.Dispose();     }      string baseAddress = "http://localhost:3446/File/AddStream/stream.txt";     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress);     request.Method = "POST";     request.ContentType = "text/plain";     Stream serverStream = request.GetRequestStream();     serverStream.Write(fileStream, 0, fileStream.Length);     serverStream.Close();     using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)     {         int statusCode = (int)response.StatusCode;         StreamReader reader = new StreamReader(response.GetResponseStream());     } } 

I've turned off the firewall and my Internet connection, but the error still exists. Is there a better way of testing the uploading method?

Stack trace:

at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)

like image 242
fiberOptics Avatar asked Mar 14 '12 02:03

fiberOptics


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.

Why is 127.0 0.1 refused to connect?

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 do you fix 10061 No connection could be made because the target machine actively refused it?

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.


1 Answers

"Actively refused it" means that the host sent a reset instead of an ack when you tried to connect. It is therefore not a problem in your code. Either there is a firewall blocking the connection or the process that is hosting the service is not listening on that port. This may be because it is not running at all or because it is listening on a different port.

Once you start the process hosting your service, try netstat -anb (requires admin privileges) to verify that it is running and listening on the expected port.

update: On Linux you may need to do netstat -anp instead.

like image 200
Yaur Avatar answered Oct 09 '22 18:10

Yaur