C# 无法建立连接,因为目标机器主动拒绝它 127.0.0.1:3446
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9695224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No connection could be made because the target machine actively refused it 127.0.0.1:3446
提问by fiberOptics
I'm using the WCF4.0 template -REST. I'm trying to make a method that uploads a file using a stream.
我正在使用 WCF4.0 模板 - REST。我正在尝试制作一种使用流上传文件的方法。
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
Windows 窗体项目:用于测试
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?
我已关闭防火墙和 Internet 连接,但错误仍然存在。有没有更好的方法来测试上传方法?
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)
在 System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) 在 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout,例外&例外)
采纳答案by Yaur
"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.
启动托管服务的进程后,请尝试netstat -anb(需要管理员权限)验证它是否正在运行并在预期端口上侦听。
update: On Linux you may need to do netstat -anpinstead.
更新:在 Linux 上,您可能需要这样做netstat -anp。
回答by Tanveer-Ibn- Haresh
Check if any other program is using that port.
检查是否有任何其他程序正在使用该端口。
If an instance of the same program is still active, kill that process.
如果同一程序的实例仍处于活动状态,则终止该进程。
回答by Matt
If you use WCF storm, can you even log-in to the WCF service endpoint? If not, and you are hosting it in a Windows service, you probably forgot to register that namespace. It's not very well advertised that this step is required, and it is actually annoying to do.
如果你使用WCF Storm,你甚至可以登录WCF服务端点吗?如果没有,并且您将它托管在 Windows 服务中,则您可能忘记注册该命名空间。没有很好地宣传这一步是必需的,而且这样做实际上很烦人。
I use this tool to do this; it automates all those cumbersome steps.
我使用这个工具来做到这一点;它使所有这些繁琐的步骤自动化。
回答by Cary
I got a similar error message like TCP error code 10061: No connection could be made because the target machine actively refused itin my current project. I find this 10061 error code cannot distinguish the case that the service endpoint is not started and the case that it is blocked by the firewall. Often, the firewall can be switched off, but the problem is still there.
我收到类似TCP 错误代码 10061的类似错误消息:无法建立连接,因为目标机器在我当前的项目中主动拒绝了它。我发现这个10061错误代码无法区分服务端点未启动的情况和被防火墙阻止的情况。通常,可以关闭防火墙,但问题仍然存在。
You can test your code in the below two ways.
您可以通过以下两种方式测试您的代码。
- Insert code to get time A that service is started and time B that client sends the request to the server. If B is earlier than A, it can cause this problem.
- Change your server port to another port that is also available in the system. You will find the same error code reported.
- 插入代码以获取服务启动的时间 A 和客户端向服务器发送请求的时间 B。如果 B 早于 A,则可能会导致此问题。
- 将您的服务器端口更改为系统中也可用的另一个端口。您会发现报告的错误代码相同。
Above is my fix. It works on my machine. I hope it helps!
以上是我的修复。它适用于我的机器。我希望它有帮助!
回答by Nick Gotch
I had a similar issue. In my case the service would work fine on the developer machine but fail when on a QA machine. It turned out that on the QA machine the application wasn't being run as an administrator and didn't have permission to register the endpoint:
我有一个类似的问题。在我的情况下,该服务在开发人员机器上可以正常工作,但在 QA 机器上会失败。事实证明,在 QA 机器上,应用程序没有以管理员身份运行,也没有注册端点的权限:
HTTP could not register URL http://+:12345/Foo.svc/]. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353for details).
HTTP 无法注册 URL http://+:12345/Foo.svc/]。您的进程无权访问此命名空间(有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkId=70353)。
Refer here for how to get it working without being an admin user: https://stackoverflow.com/a/885765/38258
请参阅此处了解如何在没有管理员用户的情况下使其工作:https: //stackoverflow.com/a/885765/38258
回答by gradosevic
You don't have to restart the PC. Restart IIS instead.
您不必重新启动 PC。而是重新启动 IIS。
Run -> 'cmd'(as admin) and type "iisreset"
运行 -> 'cmd'(以管理员身份)并输入“iisreset”
回答by Sarath Avanavu
回答by PinoyDev
With this error I was able to trace it, thanks to @Yaur, you need to basically check the service (WCF) if it's running and also check the outbound and inbound TCP properties on your advance firewall settings.
有了这个错误,我能够跟踪它,感谢@Yaur,您需要基本上检查服务(WCF)是否正在运行,并检查您的高级防火墙设置中的出站和入站 TCP 属性。
回答by Hung Vu
I had the same problem on my web server "No connection could be made because the target machine actively refused it 161.x.x.235:5672". I asked the Admin to open the port 5672 on the web server, then it worked fine.
我在我的网络服务器上遇到了同样的问题“无法建立连接,因为目标机器主动拒绝了它 161.xx235:5672”。我让管理员在 Web 服务器上打开端口 5672,然后它工作正常。
回答by Paul Grobbelaar
I had a similar problem
rejecting localhost and 127.0.0.1.
cmd(admin) netstat -anbfound the port running on 169.254.80.80 (dont know were that ip came from because my network ip was 10.0.0.5.
after putting in this IP it worked.
This Gives correct IP:
我在拒绝 localhost 和 127.0.0.1 时遇到了类似的问题。cmd(admin)netstat -anb发现在 169.254.80.80 上运行的端口(不知道那个 ip 来自,因为我的网络 ip 是 10.0.0.5。放入这个 IP 后它工作了。这给出了正确的 IP:
IPAddress ipAddress = ipHostInfo.AddressList[0];
Console.WriteLine(ipAddress.ToString());

