如何让Java套接字使用公共IP? [英] How do you get Java sockets working with public IPs?

查看:218
本文介绍了如何让Java套接字使用公共IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用localips的套接字没有问题,但是一旦我将代码更改为使用public ips,我就一直得到java.net.ConnectException。

I have had no trouble getting the sockets working with localips, but once I made the code change to use public ips, I've consistently gotten java.net.ConnectException.

我正在使用端口8084,据我所知,它没有在别处使用
在命令提示符下,netstat -a | grep 8084显示:
文件STDIN:
TCP用户-9114eb19a8:8084 user-9114eb19a8:0 LISTENING

I am using the port 8084, which as far as I know, is not used elsewhere In the command prompt, netstat -a | grep 8084 shows: File STDIN: TCP user-9114eb19a8:8084 user-9114eb19a8:0 LISTENING

我已进入我的路由器并确保它是开放的
我使用 http://www.whatismyip.org

I have gone into my router and ensured that it is open I get my public ip using a request to http://www.whatismyip.org

服务器:

serverSocket = new ServerSocket (8084);

客户:

socket = new Socket (hostaddr, 8084); //hostaddr is a string containing my public-IP
//it works when the program is run on a localnetwork and I am using my local-ips


推荐答案

什么类型的路由器?不要忘记,即使路由器上的端口可能已打开,您需要将路由器转发端口8084连接到目标计算机的内部IP,否则因为网络地址转换(NAT)路由器/防火墙一旦到达那里就不知道如何处理流量。

What type of router? Don't forget that even though a port may be open on your router, you need to have your router Forward port 8084 to the destination computer's internal IP, otherwise because of Network Address Translation (NAT) the router/firewall doesn't know what to do with the traffic once it gets there.

例如说你的客户端PC在加利福尼亚的IP为10.1.1.100,有一个使用公共IP为70.62.50.42的路由器。

For example say your Client PC is in California with the IP 10.1.1.100, with a router using public IP of 70.62.50.42.

您的服务器PC位于纽约,IP为192.168.1.121,位于内部IP为192.168.1.1的路由器后面公共知识产权40.20.26.63。您需要确保进入路由器并将端口8084(TCP)转发到内部地址192.168.1.121(内部服务器PC)。

Your Server PC is in New York with an IP 192.168.1.121, that is behind a router with internal IP 192.168.1.1 and public IP 40.20.26.63. You will need to make sure that you go into the router and forward port 8084 (TCP) to internal address 192.168.1.121 (the internal Server PC).

取决于你的路由器的级别这个术语将是端口转发,虚拟IP或静态NAT转换(还有一些其他但你明白了)。

Depending on the level of your router this terminology will be Port Forwarding, Virtual IPs, or Static NAT Translation (there are a few others but you get the idea).

你不会必须调整客户端路由器上的任何设置,因为该路由器已经知道如何将您的请求路由到互联网。

You won't have to adjust any settings on the Client router, because that router already knows how to route your request out to the internet.

这篇关于如何让Java套接字使用公共IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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