简体   繁体   English

通过nat帮助与套接字通信

[英]Help communicating with sockets through nat

i want to make a simple p2p messenger in java, i have managed to communicate with 2 computers in the same network but when i try to send a message to another computer from outside the network, the message is disappearing... I know that i could make forwards in the router but i don't want to do that, i want to make it like yahoo,gtalk or other im servers do. 我想在java中创建一个简单的p2p messenger,我已经设法与同一网络中的2台计算机进行通信但是当我尝试从网络外部向另一台计算机发送消息时,消息正在消失...我知道我可以在路由器中转发,但我不想这样做,我想让它像雅虎,gtalk或其他即时服务器那样做。 I think that they use the nat system to do that, but i don't know very well how :D. 我认为他们使用nat系统来做到这一点,但我不知道如何:D。

Is somebody who can explain me how NAT works, and if this is the way of solving my problem? 有人可以解释我NAT是如何工作的,如果这是解决我的问题的方法?

I read that I need an static and public ip for the server and when the client(with a private ip) sends a request to the server, his NAT will know what to do with the message that will come from the server next time... but if the client don't send a paket to the server and the server sends a packet to client then the NAT doesn't know where to send the packet inside the network(to the client). 我读到我需要服务器的静态和公共IP,当客户端(使用私有IP)向服务器发送请求时,他的NAT将知道如何处理下次来自服务器的消息。但是如果客户端没有向服务器发送paket并且服务器向客户端发送数据包,则NAT不知道在网络内部(向客户端)发送数据包的位置。

Is it right what i think? 我认为是对的吗?

Thank you very much! 非常感谢你!

You're almost right in your NAT description. 你的NAT描述几乎是正确的。 The only mistake you've made is that pointed out by Peter - the client is always the one that initiates a connection. 您所犯的唯一错误是Peter所指出的 - 客户端始终是启动连接的错误。

Now, if both sides have public IPs, everything is fine and either side can be a server. 现在,如果双方都有公共IP,一切都很好,任何一方都可以成为服务器。

If one side is behind a NAT and the other one has a public IP, then it's okay if the one that's behind a NAT initiates the connection. 如果一方在NAT后面而另一方有公共IP,那么如果NAT后面的那个启动连接就可以了。 In such case the NATing router will remember where to send responses from the server. 在这种情况下,NATing路由器将记住从服务器发送响应的位置。

And finally, if both sides are behind NATs, it looks like it is impossible to initiate a connection, but in fact it's not always so. 最后,如果双方都在NAT之后,看起来似乎无法建立连接,但实际上并非总是如此。 There's one trick that is called " hole punching ". 有一个被称为“ 打孔 ”的技巧。 This is a kind of white magic and it doesn't always work, but it works often enough to be successfully used by Skype, for example. 这是一种白魔法并不总是有效,但它的工作经常足以被Skype成功使用,例如。

The client by definition initiates the request, the server services that request and sends back a response. 客户端按定义启动请求,请求的服务器服务并发回响应。 The assumption is that clients can be trusted to make network connections, servers cannot. 假设客户端可以信任网络连接,服务器不能。 BTW: On some networks, clients are not even trusted to do that and must use proxies. 顺便说一句:在某些网络上,客户甚至不信任这样做,必须使用代理。

I repeat; 我重复; the client is the one creating the connection, whether it is on your network or on the internet. 客户端是创建连接的客户端,无论是在您的网络上还是在Internet上。

All the other system you are mentioning use a server with a well known public IP to help clients communicate with each other. 您提到的所有其他系统都使用具有众所周知的公共IP的服务器来帮助客户相互通信。 Each client communicates with the server and the server sends every message to the appropriate client. 每个客户端与服务器通信,服务器将每个消息发送到适当的客户端。 This is perhaps the only way to do it, because otherwise: 这可能是唯一的方法,因为否则:

  • Every client should have a public IP in order for other clients to reach him. 每个客户都应拥有公共IP,以便其他客户与他联系。 Each client should also act both as client and a server. 每个客户端也应该充当客户端和服务器。
  • It will be very difficult to discover clients as you would need to know their IPs 发现客户端非常困难,因为您需要知道他们的IP

Your system may work inside a local network, but it won't work in the internet. 您的系统可能在本地网络中工作,但它不能在Internet上工作。 Each computer in a network has a local IP assigned to it by a local gateway. 网络中的每台计算机都有一个本地网关为其分配的本地IP。 This local IP is invisible to outsiders, unless there is NAT setting that maps a public IP to it. 除非有将公共IP映射到它的NAT设置,否则本地IP对外人是不可见的。 Also there must be a setting for outgoing messages. 还必须有传出消息的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM