【发布时间】:2011-08-30 15:40:03
【问题描述】:
当客户端尝试连接到服务器时,我正在尝试设置超时,如果服务器关闭,连接将等待 10 秒,然后抛出超时异常。 在我的情况下,下面的代码无需等待就抛出 IOException,我真的不明白!
public boolean establishConnection()
{
System.out.println ("Connecting to " +
this.getServerHostname() + " au port " + this.getServerPort()+ " ...");
try {
SocketAddress sockaddr= new InetSocketAddress(_serverHostname, _serverPort);
_echoSocket = new Socket();
_echoSocket.connect(sockaddr,10000);
return _echoSocket.isConnected();
} catch (UnknownHostException e) {
System.err.println("Unknown Host: " + this.getServerHostname());
return false;
} catch (SocketTimeoutException e) {
System.err.println("Timeout");
return false;
} catch (IOException e) {
System.err.println("IOException : " +
this.getServerHostname() + ":" + this.getServerPort());
return false;
}
}
【问题讨论】:
-
你得到的例外是......?