【发布时间】:2012-03-20 20:39:15
【问题描述】:
好的,我需要编写一个能够检测客户端和服务器之间连接的客户端...一旦服务器关闭并重新启动,我的客户端需要能够将其连接回服务器..但是我我真的不知道该怎么做...如果可能的话有什么帮助吗?
public TEST(String serverIP, int serverPort){
Log("Connection to the Server....");
try{
socket = new Socket(serverIP, serverPort);
Log("Connected to the server : "+socket);
start();
} catch(UnknownHostException uhe){
System.out.println("Unknown Host: "+ uhe.getMessage());
} catch (IOException ioe){
System.out.println("IO Exception: "+ioe.getMessage());
}
String readline = "";
streamOutput.println("TRY test");
while(true){
try{
readline = streamInput.readLine();
System.out.println(readline);
} catch (IOException ioe){
System.out.println("Error in sending: "+ioe.getMessage());
return;
}
}
}
上面是我的客户端代码,它在编译后用于连接到服务器...
【问题讨论】:
-
那么服务器关闭连接时需要重新连接吗?
-
是的,我需要一些可以检测服务器是否处于活动状态的东西,如果不是,那么将尝试重新连接......