[英]Connection time out in Java Server-Client
我有一个小程序,其中Server-Client
程序正在同一网络上连接,但是同一程序在客户端程序中显示connection time out
错误。 我已经使用LAN
电缆连接了两个系统。
服务器
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class DateServer {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9090);
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();
}
}
}
客户
import java.io.BufferedReader;
import java.io.IOException ;
import java.io.InputStreamReader;
import java.net.Socket;
import javax.swing.JOptionPane;
public class DateClient {
public static void main(String[] args) throws IOException {
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
Socket s = new Socket(serverAddress, 9090);
BufferedReader input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
String answer = input.readLine();
JOptionPane.showMessageDialog(null, answer);
System.exit(0);
}
}
由于代码在同一台计算机上运行,因此我想到了三种可能性:
例如,设置以下地址:
客户端IP:192.168.1.10子网掩码:255.255.255.0
服务器IP:192.168.1.11子网掩码:255.255.255.0
仅使用LAN电缆连接两个系统是不够的。 您必须确保它们具有不同的IP地址,都位于同一IP子网中,并且/或者定义了适当的IP路由表。 更典型地,您将同时通过路由器连接两者。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.