Android SocketTimeOutException(id=830027971416)/原因错误异常(id=830029535888)

Android SocketTimeOutException(id=830027971416)/原因错误异常(id=830029535888),android,exception,vnc,Android,Exception,Vnc,我不确定我的问题是否正确@@ (完整的源代码:) 我在WindowsXP、EclipseAVD(API级别14)上运行上述链接中的源代码,但在启动服务器的步骤中失败 当我在调试模式下跟踪代码时,我在这段代码上遇到了一个异常 public static boolean isServerRunning() { try { byte[] receiveData = new byte[1024]; DatagramSocket clientSocket = new

我不确定我的问题是否正确@@

(完整的源代码:)

我在WindowsXP、EclipseAVD(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);”

它转到“catch(异常e)”

变量e的内容是

e:                       SocketTimeOutException (id=830027971416)
   bytes Transferred      0
  +cause                  ErrorException (id=830029535888)
   detailMessage          null
  +stackState             (id=830027970928)
   stackTrace             null
  +suppressedExceptions   ArrayList(id=830027971456)

现在我 知道这是由于服务器没有及时响应而导致的异常。(感谢类堆栈器)。所以我注释了这行“clientSocket.setSortimeout(100);”,等待了很长时间,发现它仍然卡住了

我不知道运行AVD时是否应该连接一些设备,或者它只能在AVD上运行


我查看变量“clientSocket”,它的subcent“address”在这一行“clientSocket.send(question);”后面有一个null,“port”有一个-1。这是正确的结果吗?(我在android软件包网站上阅读了该课程的解释,但仍然不理解。)

如前所述,您没有为该测试创建单独的线程。此“假客户端”代码在服务器应用程序中运行,在同一线程中,该线程应服务于PING请求并提供PONG响应

一个快速解决方案是将此代码包装在一个
Runnable
中,并为其启动一个单独的
线程。一个快速开始的例子是(暂时放弃结果

# start your concurrent request on-the-fly
Thread serverTest = new Thread(new Runnable() { public void run() { isServerRunning(); }});
serverTest.start();

但请研究并发性、
synchronized
volatile
以及在Android上使用
HandlerThread
AsyncTask
而不是
Thread
的详细信息。

将是e.getMessage()也许可以解释一下这个问题?100毫秒的超时时间不多。与客户端相比,您的服务器在哪里?对不起,我不明白这个问题。(我是乞丐,试着追踪源代码…@)这是由AVD运行的。问题是:你确定你的服务器在100毫秒内应答吗?因为你指定了100毫秒的超时。因此,如果你看到超时异常,这意味着服务器没有按时应答,或者答案没有到达你的安卓设备。你知道如何使用wireshark吗?我忘了告诉你信息。这个应用程序在t上运行他用手机作为服务器,电脑作为客户端。但它也可以选择反向连接。当我运行AVD时,我认为AVD就是服务器。