我不确定我的问题是否正确...@@
(完整源代码:https ://github.com/oNaiPs/droid-VNC-server )
我在 Windows XP、Eclipse AVD(API 级别 14)上运行上述链接中的源代码,但在启动服务器的步骤中失败。
当我在调试模式下跟踪代码时,我在这段代码中遇到了异常
public static boolean isServerRunning() {
try {
byte[] receiveData = new byte[1024];
DatagramSocket clientSocket = new DatagramSocket();
InetAddress addr = InetAddress.getLocalHost();
clientSocket.setSoTimeout(100);
String toSend = "~PING|";
byte[] buffer = toSend.getBytes();
DatagramPacket question = new DatagramPacket(buffer, buffer.length,
addr, 13132);
clientSocket.send(question);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
clientSocket.receive(receivePacket);
String receivedString = new String(receivePacket.getData());
receivedString = receivedString.substring(0, receivePacket
.getLength());
return receivedString.equals("~PONG|");
} catch (Exception e) {
return false;
}
}
当它运行到这一行时“clientSocket.receive(receivePacket);”
它转到“捕获(异常e)”
变量e的内容是
e: SocketTimeOutException (id=830027971416)
bytes Transferred 0
+cause ErrorException (id=830029535888)
detailMessage null
+stackState (id=830027970928)
stackTrace null
+suppressedExceptions ArrayList(id=830027971456)
现在我知道这是服务器没有按时回答的异常原因。(感谢Class Stacker)。所以我评论这一行“clientSocket.setSoTimeout(100);” 等了很久才发现还是卡住了。
我不知道在运行 AVD 时是否应该连接一些设备,或者它只能在 AVD 上运行。
我查看变量“clientSocket”,它的子内容“地址”为空,“端口”在“clientSocket.send(question);”这一行之后为 -1。这是一个正确的结果吗?(我在android包网站上阅读了类的解释,但仍然不明白。http://developer.android.com/reference/java/net/DatagramSocket.html)