InetSocketAddress isa = new InetSocketAddress(InetAddress.getLocalHost() , 3333);
ServerSocket ss = new ServerSocket();
ss.bind(isa);
ss.setSoTimeout(10000);
Socket s = null;
try
{
while(true)
{
if (ss.isClosed()) break;
s = ss.accept();
pool.execute(new sss(s));//在线程池中操作
}
System.out.println(“close socket: ” + ss.isClosed());
}
catch(Exception e)
{
e.printStackTrace();
}
我运行这段程序后,等待10秒,就出现了下面错误,请问该怎么办?
java.net.SocketTimeoutException: Accept timed out
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:344)
at java.net.ServerSocket.implAccept(ServerSocket.java:441)
at java.net.ServerSocket.accept(ServerSocket.java:412)
at com.nearbox.server.ServerSocketWrapper.accept(ServerSocketWrapper.java:44)
at com.nearbox.server.AbstractServerSocket.accept(AbstractServerSocket.java:41)
at com.nearbox.server.sss.main(sss.java:63)
|
try
{
while(true)
{
if (ss.isClosed()) break;
try{
s = ss.accept();
pool.execute(new sss(s));//在线程池中操作
}catch(SocketTimeoutException e ) {
System.out.println(“Socket Accept Timeout”);
ss.close();
}
}
System.out.println(“close socket: ” + ss.isClosed());
}
catch(Exception e)
{
e.printStackTrace();
}
|
ss.setSoTimeout(10000);
这行注释掉再试一下
|
楼上说的对,JAVA的SOCKET,时间到了以后没有响应的话,会抛出异常的,所以你只要捕获异常就可以了。