简体   繁体   English

套接字编程WiFi问题

[英]Socket programming WiFi issue

I am implementing a mobile chat application, I am using socket connection for implementing p2p connectivity. 我正在实现一个移动聊天应用程序,我正在使用套接字连接来实现p2p连接。 My chat is working fine with android devices. 我的聊天在android设备上运行正常。 My issue is I can connect a device in 3g network or with in the same WiFi network but the connection is not working when a device form outside WiFi try to connect a device in the WiFi network. 我的问题是我可以连接3g网络中的设备或具有相同WiFi网络的设备,但是当WiFi外部的设备形式尝试连接WiFi网络中的设备时,连接不起作用。 I know it's because of the local IP of the device assigned by the WiFi. 我知道这是由于WiFi分配的设备的本地IP。 How can I root and connect a device in the WiFi? 如何生根并连接WiFi中的设备?

Sounds more like a router settings issue, than an app issue (meaning that users of your app would need to do this for their own networks as well). 听起来更像是路由器设置问题,而不是应用程序问题(这意味着您应用程序的用户也需要针对自己的网络执行此操作)。

Your wifi router needs to know how to forward communication to your device. 您的wifi路由器需要知道如何将通信转发到设备。 So say that your app is connecting on port 1234, then you need to tell your router to forward communication from the outside on port 1234 to the internal IP of your phone in your wifi network. 假设您的应用正在端口1234上进行连接,那么您需要告诉路由器将来自端口1234外部的通信转发到wifi网络中手机的内部IP。

The phone that is outside of the network should target your network's external IP and port 1234. 网络外部的电话应以网络的外部IP和端口1234为目标。

Sajan, your issue is not only an IP mapping issue, but also a NAT traversal issue. Sajan,您的问题不仅是IP映射问题,还是NAT遍历问题。 It is not always possible to punch holes in NATs. 并非总是可以在NAT中打孔。 And when it is, it must be done with some sophisticated techniques including a super peer located outside of your wifi/lan which will read the translated address from your inside peer. 如果需要,则必须使用一些复杂的技术来完成,其中包括位于wifi / lan外部的超级对等方,它将从内部对等方读取转换后的地址。

Unless you are using UDP and the NAT is friendly, it is not possible to send TCP communication directly to the inside peer. 除非您使用UDP并且NAT友好,否则无法将TCP通信直接发送到内部对等方。 In most case, you'll have to check what is possible with the NAT, and if there is something possible, you'll need to perform mapping prediction and tell both peer to attempt com on predicted IP addresses. 在大多数情况下,您将必须检查NAT可能进行的操作,如果有可能,则需要执行映射预测,并告诉两个对等方尝试对预测的IP地址进行通信。

That's valid if you don't want you users to have to configure their NATs. 如果您不希望用户必须配置其NAT,那将是有效的。 And even in that case, such configuration will not always be enough (if there are cascades of NATs, or proxies for example). 即使在那种情况下,这样的配置也将始终不够(例如,如果存在NAT级联或代理)。

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

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