客户端-服务器 IM 应用程序中外部 IP 上的服务器没有响应
我正在关注教程@ http ://www.geekpedia.com/tutorial239_Csharp-Chat-Part-1---Building-the-Chat-Client.html 尝试收集网络基础知识。对于那些不想立即开始的人来说,这是一个快速演示,演示如何编写一个简单的客户端-服务器模型聊天应用程序。
当我尝试运行图特中的代码时,只要客户端和服务器都在同一网络上,它就可以正常工作,但第二次我尝试在外部执行此操作(让一个伙伴运行客户端应用程序,然后运行我这边的服务器应用程序),这一切都变得很糟糕。代码在同一网络中工作的事实使我相信这不是编码问题,而是我的网络设置方式的问题。
我试图在我的 IP 地址上的端口 21719 上运行服务器,我已打开该端口,但其他人仍然无法连接到我的服务器,根本无法获得任何形式的响应。
用于服务器侦听连接的代码(来自教程)是:
public void StartListening()
{
IPAddress ipaLocal = ipAddress; //ipAddress is parsed from txtIP
tlsClient = new TcpListener(ipaLocal, 21719);
tlsClient.Start();
ServRunning = true; //for the running loop
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
现在,教程实际上指出了 IP 地址 ipaLocal = ip 地址;
会导致某些配置出现问题,我开始担心我的配置可能会包含在其中。
那么,有人能为我提供解决方案吗?
谢谢, 山姆
I'm following a tutorial @ http://www.geekpedia.com/tutorial239_Csharp-Chat-Part-1---Building-the-Chat-Client.html to try and gather the basics of networking. For those not wanting to hit the jump, it's a quick tut demonstrating how to program a simple client-server-model chat application.
When I try and run the code in the tut, it works fine as long as both the client and the server are on the same network, but the second I try and do it externally (getting a mate to run the client app, and running the server app my side), it all goes to pot. The fact that the code works when in the same network leads me to believe that it's not a coding issue, but an issue with the way my network is set up.
I'm trying to run the server on my IP address at port 21719, which I have opened, but still other people can't connect to my server, not able to get any form of response at all.
The code (from the tut) that is being used for the server to listen to connections is:
public void StartListening()
{
IPAddress ipaLocal = ipAddress; //ipAddress is parsed from txtIP
tlsClient = new TcpListener(ipaLocal, 21719);
tlsClient.Start();
ServRunning = true; //for the running loop
// Start the new tread that hosts the listener
thrListener = new Thread(KeepListening);
thrListener.Start();
}
Now, the tutorial does actually point out that
IPAddress ipaLocal = ipAddress;
Will cause issues on some configurations, and I'm beginning to fear that my configuration may be included in that.
So, does anyone have any solution for me?
Thanks,
Sam
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用的本地 IP 地址是什么? (ipAddress) 如果是 127.0.0.1,那就不正确(我也不知道它内部是如何工作的,但 Windows 似乎有时会使用魔法)。另外,如果您的本地计算机中有多个网卡,也许端口转发仅设置为转发到其中一个网卡,而您正在使用另一个网卡的 IP?
如果这不是问题,这里有一些通用建议:
获取 netcat 的副本。它是一个小型网络测试实用程序,其唯一的工作是形成一个简单的 TCP 连接。这将允许您消除代码作为所有这一切中的变量。如果 netcat 可以建立连接,那么您就知道问题出在您的代码上。如果没有,则您已确认这是您的路由器。
您可以使用 WireShark(或 TShark)来查找 ICMP 数据包。在远程计算机上捕获 ICMP 数据包。如果您从路由器收到“Destination Unreachable”,则您再次证明这是您的路由器。
正如 Spencer 所说,您需要确保在路由器上设置端口转发,以将端口 21719 上传入的所有数据包转发到您的内部计算机。至于具体如何做到这一点,在不知道路由器类型的情况下很难说。
What is the local IP address that you're using? (ipAddress) If it's 127.0.0.1, that's not correct (I don't know how it would work internally either, but Windows seems to use magic from time to time). Also, if you have multiple NICs in your local machine, maybe the port forwarding is only set up to forward to one of them, and you're using the IP of the other?
If that's not the problem, here are a few generic suggestions:
Grab a copy of netcat. It's a small network testing util whose only job is to form a simple TCP connection. That will allow you to eliminate your code as a variable in all this. If netcat can form a connection, then you know the problem is your code. If not, you've confirmed that it's your router.
You can use WireShark (or TShark) to look for ICMP packets. Capture ICMP packets on the remote machine. If you get "Destination Unreachable" from the router, you've again proved that it's your router.
As Spencer said you need to make sure Port Forwarding is setup on your router, to forward all packets that come in on port 21719 to your internal machine. As for exactly how to do that, it's hard to say without knowing what type of router.
您是否让人使用您的外部(互联网)IP 地址? (请参阅此处。)
您是否对路由器进行了针孔处理,以将所有通信从端口 21719 转发到您的服务器?
Are you having people use your external (internet) IP address? (See yours here.)
Have you pinholed your router to forward all communications from port 21719 to your server?
一些提示:
Some tips: