如何通过外部IP连接socket(Mac)
我的问题是,如何连接到远程机器上的套接字? 我只能连接同一网络上的套接字。
我编写了一个简单的代码(在c中),模拟服务器(打开套接字并侦听客户端)。在Mac中。
我正在尝试将这个套接字作为客户端从 iPhone 连接(使用简单的 objectiv-c 代码)。
如果我的服务器和客户端上的互联网位于同一网络(WiFi)上,并且在客户端中我尝试连接到 192.168.1.x,则它可以正常工作。 但是,当在客户端中,我尝试通过外部 IP(具有相同端口)进行连接时,连接失败。
我以前从未这样做过。也许我错过了一些东西......我试图关闭我的防火墙。这没有帮助。
谢谢。
编辑:如果不清楚..我的Mac是通过路由器连接的。
My question is, how to connect to socket on romote mechine?
I can only connect sockets on same network..
I wrote a simple code (in c), that simulate a server (open socket and listen for client). in Mac.
I'm trying to connect this socket as a client from iPhone (with simple objectiv-c code).
If my internet on both, server and client, is on the same network (WiFi) and in client I trying to connect to 192.168.1.x, it's working.
But when, in client, I'm trying to connect via external IP (with the same port) connection is failed.
I never did this bofore. Maybe I miss somthing.. I've tried to turn my FireWall off. It did not help.
Thanks.
Edit: If it's not clear.. my Mac is connected by router.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这样的设置中,“外部 IP”通常是路由器的 IP。您很可能需要配置路由器以将相关端口转发到内部 IP 地址。
也可能是这样的情况:要使端口转发正常工作,请求必须来自外部 (WAN) 接口。这取决于路由器的配置方式。如果是这种情况,您需要确保您是通过蜂窝网络而不是 iPhone 上的 Wi-Fi 连接访问外部 IP。
In a setting like this, the "external IP" would typically be the IP of the router. In all likelihood you'll need to configure the router to forward the relevant port to the internal IP address.
It could also be the case that for the port forwarding to work, the request has to come in on the external (WAN) interface. This depends on how the router is configured. If that's the case, you'll need to make sure that you're accessing the external IP via the cellular network and not the Wi-Fi connection on your iPhone.
如果您通过本地 IP 地址连接到服务器(即,您通过委派的 DHCP 地址连接到同一本地路由器上的另一台计算机),那么您的问题出在网络上,而不是该服务器的 IP 堆栈上。本地机器。
您需要检查您的路由器设置……许多路由器会阻止许多服务,尤其是自定义端口上的服务,以防止来自本地网络外部来源的恶意攻击。
If you're connecting to your server via a local IP address (i.e., you're connecting to another machine on the same local router via a delegated DHCP address), then your issue is on the network, not the IP-stack of the local machine.
You'll need to look into your router settings ... many routers will block a number of services, especially those on custom ports, in order to prevent malicious attacks from sources external to the local network.