在创建网络应用程序时如何处理NAT? [英] How do you deal with NAT when creating network applications?

查看:87
本文介绍了在创建网络应用程序时如何处理NAT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以创建彼此交谈的程序,只要它们是在同一个路由器下运行。但我无法让他们使用这些私有IP地址在互联网上工作。我希望我的应用程序能够通过互联网进行交谈。



所以我做了几个小时的谷歌搜索和我对整个问题的理解有点好,但我仍然需要一些帮助。



让我尽可能地解释这个问题。



我在拥有私有IP地址的专用网络上。当我通过互联网发送和接收信息时,我的计算机私有IP地址由我的路由器转换(NAT)到公共IP地址。这样做是因为世界上的IP地址数量有限,因为它使配置网络更容易,我认为它与UPNP有关(我不确定)。



我在codeproject.com上找到了以下代码:



string myIP =" a ap address";


IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(myIP),8000) ;


套接字服务器=新套接字(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);


服务器.Connect(ipep);



如果我使用127.0.0.1或者我获得运行服务器应用程序的计算机的私有IP地址并将其用作客户端应用程序连接的IP地址(在使用与服务器应用程序相同的无线路由器的不同计算机上运行的客户端)电脑)然后它会工作。但是,假设我希望您能够在计算机上运行我的程序。如果我查找我的公共IP地址并设置myIP =" my public ip"那会有用吗?我可以告诉你,如果我在另一台通过此路由器连接到互联网的计算机上这样做,它就不起作用。



很明显我很困惑。我认为这将是一件很常见的事情,但我在找到有关它的良好信息方面遇到了很多麻烦。

解决方案

你在一个私人IP网络,例如,在家工作,需要连接到互联网,你需要做一些特别的事情。不幸的是,当前版本的.NET框架不支持这种方式。下一版.NET FW(.NET v4.0)将支持NAT遍历

有关更多详细信息,请参见此处: http://blogs.msdn.com/ncl/archive/2009/07/27/end-端到端连接性与 - NAT穿透-的.aspx

I can create programs that talk to each other as long as they are running under the same router. But I can't get them to work on the internet using those private IP addresses.  I would like my applications to be able to talk across the internet.  

 

So I have done a few hours of googling and I understand this whole problem a little bit better but I still need some help.

 

Let me explain the problem as best I can.

 

I am on a private network with a private IP address. When I send and receive info across the internet my computers private IP addresses is translated (NAT) by my router to a public IP address. This is done because of the limited number of IP addresses in the world, because it makes configuring networks easier, and I think it has something to do with UPNP (I’m not sure).

 

I found the following code on codeproject.com:

 

string myIP = "an ap address";

IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(myIP), 8000);

            Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            server.Connect(ipep);

 

if I use 127.0.0.1 or if I get the private IP address of the computer running the server application and use that as the IP address for the client application to connect to (client running on different computer which uses same wireless router as the server application's computer) then it will work. But let's say that I wanted you to be able to run my program on your computer. If I look up my public IP address and set myIP = "my public ip" will that work? I can tell you that it does not work if I do that on another computer connected to the internet by this router.

 

It's pretty obvious that I am confused. I thought this would be a pretty common thing but I'm having a lot of trouble finding good information about it.

解决方案

When you are in a private IP network, for eg, working at home, and need to connect to the internet, you need to do something special. Unfortunately, that is not supported in the current versions of the .NET framework. NAT traversal will be supported in the next version of .NET FW (.NET v4.0)

For more details, see here: http://blogs.msdn.com/ncl/archive/2009/07/27/end-to-end-connectivity-with-nat-traversal-.aspx


这篇关于在创建网络应用程序时如何处理NAT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆