我遇到这个问题已经有一段时间了,尽管我的努力和 friend 的帮助,我似乎无法克服这个问题。
我的问题是我试图使用套接字在客户端和服务器之间建立连接,这实际上很常见,但由于某种原因客户端似乎无法连接到服务器不知道为什么,这是我的尝试解决问题
1-我用过http://portforward.com/打开我的路由器上使用的端口,其类型为“zone” 2-我多次更改端口,每次我都使用 PFPortChecker 来查看我的端口是否打开
我的代码相当简单,它打开服务器,当客户端连接到它时,服务器发送日期和时间
我的服务器代码如下所示
public class DateServer {
/** Runs the server. */
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(6780);
try {
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
} finally {
socket.close();
}
}
} finally {
listener.close();
}
}
}
我的客户端代码如下所示
public class DateClient {
/** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */
public static void main(String[] args) throws IOException {
//I used my serverAddress is my external ip address
Socket s = new Socket(serverAddress, 6780);
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
尽管我继续尝试
3-我关闭了防火墙以防万一
4-我在服务器套接字中添加了连接超时
经过我的所有尝试,我总是收到此错误
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at DateClient.main(DateClient.java:13)
注意 DateClient.java:13 是这一行 Socket s = new Socket(serverAddress, 6780);
请帮我解决这个问题,提前致谢
最佳答案
我尝试运行你的代码。首先,localhost(127.0.0.1)可以解决您的问题。另一方面,我将端口和IP更改为我自己的,并且它工作正常(甚至是我的外部IP)。所以您的端口/IP 可能有问题。
如果它使用本地主机工作,则您的 IP 不正确,或者您计算机上的某些内容阻止了外部连接。 您的客户端代码应该如下所示,由于某种原因
new Socket(String host, int port)
不起作用。public class DateClient { /** Runs the client as an application. First it displays a dialog box asking for the IP address or hostname of a host running the date server, then connects to it and displays the date that it serves. */ public static void main(String[] args) throws IOException { //I used my serverAddress is my external ip address InetAddress serverAddress = InetAddress.getByName(String host); Socket s = new Socket(serverAddress, 6780); BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); String answer = input.readLine(); JOptionPane.showMessageDialog(null, answer); System.exit(0); }
}
如果使用本地主机无法正常工作,则说明您的端口未正确转发。尝试登录到您的路由器并从那里转发端口。
事实上,就像 @Audrius Meškauskas 所说,您可能想在关闭 ServerSockect listener
之前关闭服务器上的 PrintWriter。
关于java - 解决java中客户端连接到服务器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16572239/