我正在使用C#编写一个简单的hello world TCP/IP客户机服务器应用程序,但无法让我的客户机连接。任何人都可以提供任何其他疑难解答步骤我开始江郎才尽......由于目标机器主动拒绝,因此无法建立连接
这里是代码的有关章节:
服务器:
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);
客户端:
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);
我能从客户端计算机ping服务器,但是我无法使用绑定的端口telnet到服务器。我尝试了各种端口(少数在8000和40000左右)。我已在两个系统上禁用Windows防火墙。系统连接到不在互联网上的路由器。我尝试了使用和不使用端口转发设置将给定端口上的传入请求转发到服务器计算机,但没有任何效果。
,我已经能够捕获唯一的例外是由客户端抛出:
无连接可以作出,因为 目标机器积极地拒绝它 。
我检查了一个InnerException
但似乎没有 - 这是基本的例外。这是对的吗?
不知道还有什么我应该看 - 任何其他故障排除步骤将是有益的。
谢谢!
我应该刚刚问过你 - 首先就是这样。任何想法为什么它有所作为? – 2009-02-13 18:55:58
您的机器同时坐在多个IP网络上,在您的情况下,环回和本地网络。你明确要求只听127.0.0.1,而127.0.0.1/32上的唯一一台机器是你的。机器可以连接到自己(通过127.0.0.1/32),但没有其他人可以。 – Coincoin 2009-02-13 20:45:24