Java: 连接两台不同电脑的服务器IP地址是多少
Java: what's the IP address of server in order to connect two different computers
我在 java 上设计了一个小型多人游戏,并使用本地主机 ip“127.0.0.1”成功测试了它,但现在我想在两台不同的计算机上测试它,它们都连接到一个无线网络。
问题是我不知道客户端需要使用哪个IP地址来连接到服务器。我使用了 IPv4 地址(在 cmd ipconfig 中),但它们似乎无法连接。
客户:
public class Client {
Socket client;
ObjectOutputStream oos;
ObjectInputStream ois;
public Client(String gameServer) {
try {
client = new Socket(InetAddress.getByName(gameServer),8888);
oos = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
服务器:
public class Server {
ServerSocket ss = null;
Socket server;
ObjectOutputStream oos;
ObjectInputStream ois;
public Server() {
try {
ss = new ServerSocket(8888, 10, InetAddress.getByName("0.0.0.0"));
server = ss.accept();
System.out.println("----->" + server.getInetAddress().getHostName());
System.out.println("Successful connection");
oos = new ObjectOutputStream(server.getOutputStream());
ois = new ObjectInputStream(server.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
基本上我不知道要输入什么作为客户端的构造函数class才能连接这两台计算机
您写道,一般软件在您的本地机器上运行良好。所以问题出在网络连接上,不在软件应用程序上。
如果您在同一个网络(例如一个房子里有两台计算机),请检查端口是否打开。如果你在同一个网络中,你应该使用本地 ip 地址。只需对 linux 使用 sudo ifconfig 命令,对 windows.
使用 ipconfig 命令
在linux你可以使用nmap或者telnet查看端口是否打开:
nmap [local ip] -p [port]
在 windows 上,您可以使用 telnet
进行测试
telnet [local ip] [port]
如果您在不同的网络(例如您和您的朋友在不同的房子里),则不能使用本地 IP 地址。你应该通过路由器做端口转发来找到本地机器。当然,在这种情况下您应该使用全球 IP 地址。
问题是由于防火墙打开。关闭后,一切正常。
我在 java 上设计了一个小型多人游戏,并使用本地主机 ip“127.0.0.1”成功测试了它,但现在我想在两台不同的计算机上测试它,它们都连接到一个无线网络。
问题是我不知道客户端需要使用哪个IP地址来连接到服务器。我使用了 IPv4 地址(在 cmd ipconfig 中),但它们似乎无法连接。
客户:
public class Client {
Socket client;
ObjectOutputStream oos;
ObjectInputStream ois;
public Client(String gameServer) {
try {
client = new Socket(InetAddress.getByName(gameServer),8888);
oos = new ObjectOutputStream(client.getOutputStream());
ois = new ObjectInputStream(client.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
服务器:
public class Server {
ServerSocket ss = null;
Socket server;
ObjectOutputStream oos;
ObjectInputStream ois;
public Server() {
try {
ss = new ServerSocket(8888, 10, InetAddress.getByName("0.0.0.0"));
server = ss.accept();
System.out.println("----->" + server.getInetAddress().getHostName());
System.out.println("Successful connection");
oos = new ObjectOutputStream(server.getOutputStream());
ois = new ObjectInputStream(server.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
基本上我不知道要输入什么作为客户端的构造函数class才能连接这两台计算机
您写道,一般软件在您的本地机器上运行良好。所以问题出在网络连接上,不在软件应用程序上。
如果您在同一个网络(例如一个房子里有两台计算机),请检查端口是否打开。如果你在同一个网络中,你应该使用本地 ip 地址。只需对 linux 使用 sudo ifconfig 命令,对 windows.
使用 ipconfig 命令在linux你可以使用nmap或者telnet查看端口是否打开:
nmap [local ip] -p [port]
在 windows 上,您可以使用 telnet
进行测试telnet [local ip] [port]
如果您在不同的网络(例如您和您的朋友在不同的房子里),则不能使用本地 IP 地址。你应该通过路由器做端口转发来找到本地机器。当然,在这种情况下您应该使用全球 IP 地址。
问题是由于防火墙打开。关闭后,一切正常。