質問

I am not sure if my question is right...@@

(complete source code: https://github.com/oNaiPs/droid-VNC-server)

I run the source code in the above link on Windows XP, Eclipse AVD(API level 14), and it failed at the step to start the sever.

While I am tracing the code in debug mode, I got an exception on this code

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;
    }
}

when it run to this line "clientSocket.receive(receivePacket);"

it goes to "catch (Exception e)"

The content of variable e is

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

Now I know its an exception cause by the sever didn't answer on time.(Thanks for Class Stacker). So I comment this line "clientSocket.setSoTimeout(100);" to waited a long time and find out it still stuck.

I don't know if there should be some device connected when running the AVD, or it can just run on the AVD only.

I look the variable "clientSocket", it's subcontent "address" got a null and the "port" got a -1 after this line "clientSocket.send(question);". Is this a correct result? (I read the explanation of the class on the android package website, but remain not understand. http://developer.android.com/reference/java/net/DatagramSocket.html)

役に立ちましたか?

解決

As discussed, you're not creating a separate Thread for this test. This "fake client" code runs in the server app, in the same Thread which should serve the PING request and provide the PONG response.

One quick solution would be to wrap this code inside a Runnable and start a separate Thread for it. A quick start example would be (throwing away the result for now,

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

But please study the details about concurrency, synchronized, volatile and using HandlerThread and AsyncTask rather than Thread on Android.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top