使用套接字在 nat 后面连接一个 ip [英] Connect an ip behind nat using sockets

查看:13
本文介绍了使用套接字在 nat 后面连接一个 ip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一部手机通过手机 A 连接到 wifi,地址为 Dynamic Ip ex:192.168.0.34 并通过 port 7567 的服务器套接字进行侦听, 在如果我有另一部电话 B 连接公共 ip 上的电话 B 说 10.0.0.56 并且我有 wifi 路由器 ip 说 ex 55.56,我可以通过什么方式使用任何编程语言连接到该套接字.89.76 ?

Consider a phone which is connected to wifi with phones A, address as Dynamic Ip ex:192.168.0.34 and its listening over a server socket at port 7567, In what way can i connect to that socket using any programming language if i have another phone B to connect that which is on public ip say 10.0.0.56 and i have the wifi router ip say ex 55.56.89.76 ?

推荐答案

如果不使用端口转发,则无法直接连接到 NAT 后面的客户端.但是有一种称为打孔的技术可以通过 NAT 打开端口.

It is not possible to connect directly to a client behind a NAT if you don't use port forwarding. But there is a technique called hole punching to open a port thrue a NAT.

来自 维基百科:

打孔是一种计算机网络技术,用于在位于限制性防火墙之后的不同组织中的两方之间建立通信.用于在线游戏、P2P 和 VoIP 等应用程序,两个客户端都与不受限制的第三方服务器建立连接,该服务器为它们揭示外部和内部地址信息.由于每个客户端都向服务器发起了请求,因此服务器知道为该会话分配的 IP 地址和端口号,并与另一个共享.拥有有效的端口号会导致防火墙接受来自每一侧的传入数据包.ICMP打孔、UDP打孔和TCP打孔分别使用Internet控制消息、用户数据报和传输控制协议.使用 TCP 恶意打孔,可以将压缩的 SYN 数据包发送到公共 ACK 路径.许多软件都这样做.

Hole punching is a computer networking technique for establishing communications between two parties in separate organizations who are both behind restrictive firewalls. Used for applications such as online gaming, P2P and VoIP, both clients establish a connection with an unrestricted third-party server that uncovers external and internal address information for them. Since each client initiated the request to the server, the server knows their IP addresses and port numbers assigned for that session, which it shares one to the other. Having valid port numbers causes the firewalls to accept the incoming packets from each side. ICMP hole punching, UDP hole punching and TCP hole punching respectively use Internet Control Message, User Datagram and Transmission Control Protocols. Using TCP nefarious hole punching, it's possible to send compressed SYN packets through into a common ACK path. Numerous software does this.

另请参阅与此主题相关的问题.

See also the questions related to this topic.

这篇关于使用套接字在 nat 后面连接一个 ip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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