我正在编写一个需要多人参与的交易游戏。我意识到,我的客户端只能通过服务器运行的pc上的"localhost"访问。我该如何判断服务器是否在线,以便知道是防火墙还是其他什么?如果有人已经知道解决方案,我会更高兴。
谢谢!
编辑:我的操作系统是Windows7。
更新:我设法用ipconfig找到的两个地址连接到了同一台计算机上的服务器。一个是我的根给我的,我不知道另一个来自哪里。首先,我在谷歌上搜索"我的IP是什么",但没有成功。我仍然无法通过连接到同一路由器(fritz box)的其他电脑连接到服务器。
Server:
ExecutorService executor = Executors.newFixedThreadPool(100);
ServerSocket server;
try {
server = new ServerSocket(5555);
System.out.println("Server gestartet");
while(true){
Socket client = server.accept();
executor.execute(new ClientHandler(client));
}
}catch (Exception e) {
e.printStackTrace();
}
}
Client:
try {
InetAddress addr = InetAddress.getByName("MY IP");
//Works
client = new Socket("localhost",Port);
//Does not work
client = new Socket(addr,Port);
//Does not work
client = new Socket("MY IP",Port);
System.out.println("Client gestartet");
//Streams
out = client.getOutputStream();
writer = new PrintWriter(out);
in = client.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
// ------------------------------------------------
String s = null;
while((s=reader.readLine())!=null){
render(s);
}
} catch (IOException e)
{
e.printStackTrace();
}
我忽略了ClientHandler,因为我确信不存在问题。
如果是防火墙问题,您将获得连接超时。如果收到"连接被拒绝",则服务器未运行。
不要按照此处其他地方的建议修改ServerSocket
构造。你做这件事的方式是正确的。