当前位置: 技术问答>java相关
socket抛出异常
来源: 互联网 发布时间:2015-11-16
本文导语: 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 = s...
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)
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();
}
{
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,时间到了以后没有响应的话,会抛出异常的,所以你只要捕获异常就可以了。
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。