简体   繁体   English

Java 在 PC 上运行 ServerSocket

[英]Java run ServerSocket on PC

A common problem for a common project: I'm trying to run a ServerSocket Java program on my local PC and connect to it from an Android client.常见项目的一个常见问题:我正在尝试在本地 PC 上运行 ServerSocket Java 程序并从 Android 客户端连接到它。 As others have experienced, things go smoothly when the client connects to the local address (eg. 192.168.xxx.xxx).正如其他人所经历的那样,当客户端连接到本地地址(例如 192.168.xxx.xxx)时,事情进展顺利。 If I try to connect through the internet, nothing happens:如果我尝试通过互联网连接,则没有任何反应:

Now, here are the things I tried:现在,这是我尝试过的事情:

  • Created inbound/outbound rules in Windows Firewall to allow traffic for the Server Port (3000)在 Windows 防火墙中创建入站/出站规则以允许服务器端口 (3000) 的流量
  • Used the public IP in the client socket (IP obtained here)使用客户端套接字中的公共 IP(此处获得的 IP)
  • Set up port forwarding on my router: inbound connection on port 3000 are redirected to the local IP of the server (eg. 192.1686.xxx.xxx again)在我的路由器上设置端口转发:端口 3000 上的入站连接被重定向到服务器的本地 IP(例如 192.1686.xxx.xxx 再次)
  • Check that the IP address of the server is listening to the port 3000 using "netstat -anp tcp".使用“netstat -anp tcp”检查服务器的 IP 地址是否正在侦听端口 3000。
  • Disabled any firewall on my router, just in case.禁用我路由器上的任何防火墙,以防万一。

Still, there is no connection.尽管如此,还是没有联系。 Using this tool ( https://www.canyouseeme.org/ ) I triedd checking that the port was visible (with the server app running of course), but nothing, all I get is a time out.使用这个工具( https://www.canyouseeme.org/ )我试着检查端口是否可见(当然服务器应用程序正在运行),但没有,我得到的只是超时。

I'm out of options, I was hoping someone could show me what I'm missing.我别无选择,我希望有人能告诉我我错过了什么。

Thanks.谢谢。

(1) Does your PC where your server runs on have a public IP? (1) 您运行服务器的 PC 是否具有公共 IP? If you haven't applied for one and are running your code in a home or working environment, your computer only has a private IP.如果您还没有申请并在家庭或工作环境中运行您的代码,那么您的计算机只有一个私有 IP。 And considering the fact, your server and client connect well in the same LAN, the problem may be the IP.考虑到你的服务器和客户端在同一个局域网内连接良好,问题可能是IP。

(2)If you do have a public Ip, does your server listen on all IP address or just the LAN? (2)如果你有一个公共IP,你的服务器是监听所有IP地址还是只监听局域网? Check the server bind IP is '0.0.0.0', and in ServerSocket constructor you can just set it to be null.检查服务器绑定 IP 是否为“0.0.0.0”,在 ServerSocket 构造函数中,您可以将其设置为 null。

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

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