如何连接客户端和服务器?
即使UDP是无连接的,我也想在客户端和服务器之间建立某种连接。 问题是,一旦服务器收到来自客户端的信息,我唯一能做的就是使用来自客户端的 IP 和端口来发送我的答案。
好吧...在几乎所有情况下,这都不是客户端的 IP,因为客户端在互联网上没有 IP:只有其路由器有。 我想知道如何将数据发送回客户端,即使他位于路由器后面。
我开始相信这是不可能的......但它必须是可能的:不要告诉我视频游戏仅使用 UDP 将数据从客户端发送到服务器!
Even if UDP is connection-less, I would like to establish some kind of connection between a client and a server.
The thing is, once the server has received something from the client, the only thing I can do send my answer is use the IP and the Port from the client.
And well... this will, in almost EVERY CASE, not be the IP of the client, because the client doesn't have an IP on the Internet : only its router does.
I would like to know how to send back data to the client, even if he's behind a router.
I'm starting to believe it's impossible... but it MUST BE possible : don't tell me videogames use UDP only for sending data from a client to a server !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
防火墙后面的 UDP 选项:
Options for UDP behind firewalls:
只需通过同一个套接字发送回客户端,NAT 就会自动解析并将其传递到实际的盒子。
Just send back to the client through the same socket, the NAT will automagically resolve that and pass it on to the actual box.
您的路由器会注意到它已重写传出数据包,使其看起来像是来自路由器上的端口,并记住这一点。如果它随后收到发送到同一端口的 UDP 数据包地址,它会说“哦,我知道将该数据发送到哪里!”并将其转发到原始数据来自的网络内部端口。
通常路由器会记住路由一段时间,然后如果在该端口上没有看到任何内容,就会忘记它们。
Your router will notice that it's rewritten an outgoing data packet to look as if it came from a port on the router, and remember that. If it then receives a UDP packet address to that same port it will say "Oh, I know where to send this!" and forward it on to the port on the inside of the network that original data came from.
usually the router remembers routes for a certain time then forgets them if it doesn't see anything on that port.