帮助通过nat与socket通信

发布于 2024-10-07 17:27:09 字数 381 浏览 0 评论 0原文

我想用java制作一个简单的p2p信使,我已经设法与同一网络中的两台计算机进行通信,但是当我尝试从网络外部向另一台计算机发送消息时,该消息正在消失......我知道我可以在路由器中进行转发,但我不想这样做,我想让它像 yahoo、gtalk 或其他 IM 服务器那样。 我认为他们使用 nat 系统来做到这一点,但我不太清楚如何:D。

有人可以向我解释 NAT 的工作原理吗?这是否可以解决我的问题?

我读到我需要一个静态的公共IP作为服务器,当客户端(具有私有IP)向服务器发送请求时,他的NAT将知道如何处理下次来自服务器的消息。但是如果客户端不向服务器发送数据包并且服务器向客户端发送数据包,则 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.
I think that they use the nat system to do that, but i don't know very well how :D.

Is somebody who can explain me how NAT works, and if this is the way of solving my problem?

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).

Is it right what i think?

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤千羽 2024-10-14 17:27:09

你的 NAT 描述几乎是正确的。您犯的唯一错误是彼得指出的 - 客户端始终是发起连接的人。

现在,如果双方都有公共IP,一切都很好,任何一方都可以成为服务器。

如果一侧位于 NAT 后面,而另一侧具有公共 IP,则由位于 NAT 后面的一方发起连接就可以了。在这种情况下,NATing 路由器将记住从服务器发送响应的位置。

最后,如果双方都位于 NAT 后面,看起来就不可能发起连接,但实际上并非总是如此。有一种技巧叫做“打洞”。这是一种白魔法,并不总是有效,但它经常有效,足以被 Skype 等成功使用。

You're almost right in your NAT description. The only mistake you've made is that pointed out by Peter - the client is always the one that initiates a connection.

Now, if both sides have public IPs, everything is fine and either side can be a server.

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. In such case the NATing router will remember where to send responses from the server.

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. 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.

小傻瓜 2024-10-14 17:27:09

根据定义,客户端发起请求,服务器服务该请求并发回响应。假设客户端可以被信任进行网络连接,而服务器则不能。顺便说一句:在某些网络上,客户端甚至不被信任可以这样做,并且必须使用代理。

我再说一遍;客户端是创建连接的人,无论是在您的网络上还是在 Internet 上。

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.

旧时光的容颜 2024-10-14 17:27:09

您提到的所有其他系统都使用具有众所周知的公共 IP 的服务器来帮助客户端相互通信。每个客户端都与服务器通信,服务器将每条消息发送到适当的客户端。这也许是唯一的方法,因为否则:

  • 每个客户端都应该有一个公共 IP,以便其他客户端能够联系到他。每个客户端还应该既充当客户端又充当服务器。
  • 发现客户端将非常困难,因为您需要知道他们的 IP。

您的系统可能在本地网络内工作,但在互联网上无法工作。网络中的每台计算机都有一个由本地网关分配给它的本地 IP。该本地 IP 对于外部人员来说是不可见的,除非有 NAT 设置将公共 IP 映射到它。还必须有一个用于发送消息的设置。

All the other system you are mentioning use a server with a well known public IP to help clients communicate with each other. 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. 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

Your system may work inside a local network, but it won't work in the internet. Each computer in a network has a local IP assigned to it by a local gateway. This local IP is invisible to outsiders, unless there is NAT setting that maps a public IP to it. Also there must be a setting for outgoing messages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文