[英]Java Networking - Connecting two computers
I'm trying to write a simply program client-server program that would connect a Client machine to a Server machine. 我正在尝试编写一个简单的程序客户端服务器程序,该程序将客户端计算机连接到服务器计算机。
My code so far works well on the localhost, but when I replace the ip address in the Client code to the local ip address of the Server machine no connection is done. 到目前为止,我的代码在localhost上运行良好,但是当我将客户端代码中的IP地址替换为服务器计算机的本地IP地址时,则未建立连接。 I think my understanding of
InetAddress
is off. 我认为我对
InetAddress
了解已关闭。
Socket connect code: Client.java 套接字连接代码:Client.java
InetAddress ip = InetAddress.getByName("my_ip_address");
Socket s = new Socket(ip, 9876); //<- where the connection timeout happens
You don't call getBytes()
from a String
to get your ip address like that; 您不会从
String
调用getBytes()
来获取您的IP地址,而是这样。 option 1: call getByName(String)
like 选项1:像这样调用
getByName(String)
InetAddress ip = InetAddress.getByName("127.0.0.1");
Option 2: construct the proper byte[]
. 选项2:构造适当的
byte[]
。 Like, 喜欢,
InetAddress ip = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.