【发布时间】:2019-08-09 11:11:01
【问题描述】:
我正在尝试编写一个简单的客户端-服务器程序,它将客户端机器连接到服务器机器。
到目前为止,我的代码在 localhost 上运行良好,但是当我将客户端代码中的 ip 地址替换为服务器机器的本地 ip 地址时,没有完成连接。我想我对InetAddress 的理解是错误的。
Socket 连接代码:Client.java
InetAddress ip = InetAddress.getByName("my_ip_address");
Socket s = new Socket(ip, 9876); //<- where the connection timeout happens
【问题讨论】:
-
@ScaryWombat 是的,客户端和服务器上的端口或初始化为 9876
-
@Joni 我在
Socket s = new Socket(ip, 9876);出现错误:java.net.ConnectException: Connection timed out: connect -
您在企业网络中尝试吗?防火墙可能会阻止非标准端口访问。
-
@javapedia.net 我没有知识告诉你,有什么办法可以找到
-
是linux客户端吗?尝试像“ncat IP_address port_number”这样的 nc 命令。尝试像 80 这样的标准端口,然后是非标准端口。如果标准端口工作,那么非标准端口可能存在防火墙问题。如果这没有帮助,请改用 machine-name 。 “ping ip”有用吗? NCAT 命令参考:linuxtechi.com/nc-ncat-command-examples-linux-systems
标签: java sockets networking ip-address inetaddress