[英]ServerSocket.accept() throwing SocketTimeoutException with null message
I am trying to get an instance of Socket from ServerSocket Object. 我正在尝试从ServerSocket对象获取Socket的实例。 But, it always prints 但是,它总是打印
SocketTimeoutException = null
There is my code: 有我的代码:
try {
// We listen for new connections
Log.d("ShairPort", "Service >> In TRY ");
try {
servSock = new ServerSocket(port);
} catch (IOException e) {
Log.d("ShairPort", "Service >> In TRY # IO Exception = " +e.getMessage());
servSock = new ServerSocket();
}catch(Exception e) {
Log.d("ShairPort", "Service >> In TRY # E xception = " +e.getMessage());
}
// DNS Emitter (Bonjour)
byte[] hwAddr = getHardwareAdress();
emitter = new BonjourEmitter(name, getStringHardwareAdress(hwAddr), port);
//Setting Timeout
servSock.setSoTimeout(10000);
Log.d("ShairPort", "Service >> stopThread = " +stopThread);
while (!stopThread) {
try {
//********** This is throwing Exception ***************//
Socket socket = servSock.accept();
servSock.setReuseAddress(true);
Log.d("ShairPort", "Service >> got connection from " + socket.toString());
new RTSPResponder(hwAddr, socket).start();
} catch(SocketTimeoutException e) {
e.printStackTrace();
Log.d("ShairPort", "Service >> SocketTimeoutException = " + e.getMessage());
//********* here I am getting exception **************//
}catch(Exception e) {
Log.d("ShairPort", "Service >> Exception = " + e.getMessage());
servSock.close();
}
}
} catch (IOException e) {
Log.d("ShairPort", "Service >> TRY # CATCH IOException = " + e.getMessage());
throw new RuntimeException(e);
}
Please let me know if I am doing anything wrong, so that I can resolve this issue. 如果我做错了任何事情,请告诉我,以便我解决此问题。
02-06 18:14:08.300: W/System.err(2245): java.net.SocketTimeoutException
02-06 18:14:08.300: W/System.err(2245): at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:108)
02-06 18:14:08.300: W/System.err(2245): at java.net.ServerSocket.implAccept(ServerSocket.java:203)
02-06 18:14:08.310: W/System.err(2245): at java.net.ServerSocket.accept(ServerSocket.java:128)
02-06 18:14:08.310: W/System.err(2245): at vavi.apps.shairport.LaunchThread.run(LaunchThread.java:99)
02-06 18:14:08.310: W/System.err(2245): Caused by: libcore.io.ErrnoException: accept failed: EAGAIN (Try again)
02-06 18:14:08.320: W/System.err(2245): at libcore.io.Posix.accept(Native Method)
02-06 18:14:08.320: W/System.err(2245): at libcore.io.BlockGuardOs.accept(BlockGuardOs.java:55)
02-06 18:14:08.320: W/System.err(2245): at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:98)
02-06 18:14:08.320: W/System.err(2245): ... 3 more
You're setting a ten second timeout on the server socket. 您正在服务器套接字上设置十秒超时。
The accept()
method will therefore throw a SocketTimeoutException
if no connection arrives within ten seconds. 因此,如果十秒钟内没有连接到达, accept()
方法将抛出SocketTimeoutException
。
If you don't want that behaviour, don't set the timeout, or raise it. 如果您不希望这种行为,请不要设置超时时间,也不要提高超时时间。
I have no idea why you're surprised at getting a timeout when you're setting it yourself. 我不知道为什么您自己设置超时时会感到惊讶。
NB It is utterly pointless to call setReuseAddress()
on the server socket after it is bound, let alone every time you accept a new connection. 注意:绑定后在服务器套接字上调用setReuseAddress()
是毫无意义的,更不用说每次接受新连接时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.