您好我在我的电脑上运行客户端并希望连接到另一台连接到不同网络的电脑上的服务器但是当我尝试连接到服务器时我得到了这个例外:
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at social_network.Client.main(Client.java:30)
Java Result: 1
在客户端源中插入服务器网络的公共IP;这是客户的代码:
public class Client {
public static void main(String[] args) throws IOException
{
Client c = new Client();
Socket s = new Socket("151.54.151.121", 4444); //public IP of server network
s.close();
}
}
在不同网络上运行的服务器代码:
public class Server {
public static void main(String[] args) throws IOException, SQLException, ClassNotFoundException
{
ServerSocket server = new ServerSocket(4444);
System.out.println("Waiting for clients to connect...");
while (true)
{
Socket s = server.accept();
InetAddress clientAddress = s.getInetAddress();
System.out.println("Incoming connection from: " + clientAddress.getHostName() + "[" + clientAddress.getHostAddress() + "]");
s.close();
}
}
}
出了什么问题?