C# 无法建立连接,因为目标机器主动拒绝它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/547134/
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 refuses it
提问by Brian Sweeney
I'm working on a simple hello world TCP/IP client server app in C# and am unable to get my client to connect. Can anyone offer any additional troubleshooting steps? I'm starting to run out of ideas...
我正在用 C# 开发一个简单的 hello world TCP/IP 客户端服务器应用程序,但无法让我的客户端连接。任何人都可以提供任何其他故障排除步骤吗?我开始没有想法了...
Here are the relevant sections of code:
以下是代码的相关部分:
server:
服务器:
Console.Out.WriteLine("About to bind address");
IPAddress ipAd = IPAddress.Parse("127.0.0.1");
Console.Out.WriteLine("Choose a port to bind...");
String port = Console.In.ReadLine();
int iPort = Int32.Parse(port);
TcpListener myList = new TcpListener(ipAd, iPort);
myList.Start();
Console.WriteLine("The server is running at: "+myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
client:
客户:
Console.Out.WriteLine("enter address: ");
string address = Console.In.ReadLine();
Console.Out.WriteLine("enter port: ");
int port = Convert.ToInt32(Console.In.ReadLine());
TcpClient tcpclnt = new TcpClient();
Console.WriteLine("Connecting.....");
Console.Out.WriteLine("Address: " + address + ":" + port);
tcpclnt.Connect(address, port);
I am able to ping the server from the client machine, however I am unable to telnet to the server using the bound port. I've tried a variety of ports (a few in the low 8000s and a few up around 40000). I have disable windows firewall on both systems. The systems are connected to a router which is not on the internet. I've tried with and without port forwarding set to forward incoming requests on the given port to the server machine with no effect.
我能够从客户端机器 ping 服务器,但是我无法使用绑定端口 telnet 到服务器。我尝试了各种端口(一些在 8000 左右,一些在 40000 左右)。我在两个系统上都禁用了 Windows 防火墙。系统连接到不在互联网上的路由器。我已经尝试过设置和不设置端口转发来将给定端口上的传入请求转发到服务器机器,但没有任何效果。
The only exception that I've been able to trap is thrown by the client:
我能够捕获的唯一异常是客户端抛出的:
No connection could be made because the target machine actively refuses it.
无法建立连接,因为目标机器主动拒绝它。
I checked for an InnerException
but it seems that there are none - that is the base exception. Could that be right?
我检查了一个,InnerException
但似乎没有 - 这是基本异常。那可能是对的吗?
Not sure what else I should be looking at - any additional troubleshooting steps would be helpful.
不确定我还应该查看什么 - 任何其他故障排除步骤都会有所帮助。
Thanks!
谢谢!
采纳答案by Coincoin
The code above is listening to request coming from the loopback address. This will effectively only listen to connection on that network, and that network only includes your machine.
上面的代码正在监听来自环回地址的请求。这将有效地仅侦听该网络上的连接,并且该网络仅包括您的机器。
Have you tried listening to the address bound to the network from which the connection should be coming? On a local network it should be something like 192.168.x.x or 10.x.x.x
您是否尝试过侦听绑定到连接应该来自的网络的地址?在本地网络上,它应该类似于 192.168.xx 或 10.xxx
回答by Nick Fortescue
try netstat -al
on your machine (the exact command line varies between Windows and unix) and see if the server is listening on the port
netstat -al
在您的机器上尝试(确切的命令行在 Windows 和 unix 之间有所不同)并查看服务器是否正在侦听端口
回答by David Basarab
Why don't you use .NET remoting? It is better than doing a TCP/IP client server. You can pass messages between objects.
为什么不使用 .NET 远程处理?它比做一个 TCP/IP 客户端服务器要好。您可以在对象之间传递消息。
回答by Quintin Robinson
Have you tried running the client server on the same machine to make sure the connection is made first? Beyond that try using the router assigned or static IP of the machine running the server vs binding to loopback.
您是否尝试在同一台机器上运行客户端服务器以确保首先建立连接?除此之外,尝试使用路由器分配的或运行服务器的机器的静态 IP 与绑定到环回。
回答by Mykroft
I've run into this before. The trick is to bind to 0.0.0.0
rather than 127.0.0.1
. When you bind to 127.0.0.1
the server will only accept connections from localhost. Binding to 0.0.0.0
it will accept all requests.
我以前遇到过这个。诀窍是绑定到0.0.0.0
而不是127.0.0.1
。当您绑定到127.0.0.1
服务器时,将只接受来自 localhost 的连接。绑定到0.0.0.0
它会接受所有请求。
You also may want to nmapthe host machine from the client and see what ports it sees as being open.
您可能还想从客户端nmap主机并查看它认为打开的端口。
EDIT:If you hard code the IP address of the machine in it the listener will only listen on that network interface. If you use 0.0.0.0
the listener will listen on all available network interfaces. This includes interfaces between your computer and a USB attached hand held device, a second network card or a VPN link.
编辑:如果您在其中硬编码机器的 IP 地址,则侦听器将仅在该网络接口上侦听。如果您使用0.0.0.0
侦听器将侦听所有可用的网络接口。这包括您的计算机和 USB 连接的手持设备、第二块网卡或 VPN 链接之间的接口。