setSoTimeout
public void setSoTimeout(int timeout)
throws SocketException
Enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a java.net.SocketTimeoutException is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.
Parameters:
timeout - the specified timeout, in milliseconds.
Throws:
SocketException - if there is an error in the underlying protocol, such as a TCP error.
Since:
JDK 1.1
See Also:
getSoTimeout()
用线程,并且设置Socket的超时(TimeOut)。如下例:
ConnServer con = new ConnServer(ip , port , frame);//这是一个连接服务器的操作,采用了线程和超时设置,可以做到连接时不会阻塞,并且在一段时间连接不上自动返回。
Thread t = new Thread(con);
t.start();
try
{
t.join(timeout);
}