Question

I am implementing a socket in Java for communicating with VMware-Workstation (Version: 8.0.0 build-471780).

I create the socket with: Socket socket = new Socket("localhost",8832);, since I'm running the virtual machine locally and 8832 is the port that VMware listens for debugging by defalut. The connection is not being refused or throwing any kind of exceptions. However, I'm in trouble to establish a communication between my process and the VMware stub.

These methods are not effectively sending or receiving anything from the stub:

public void sendStr(String str) throws IOException {
        out.write(str.getBytes());
}
public String getStr() throws IOException, ClassNotFoundException {//blocking call
    byte[] byteArray = new byte[40];
    in.read(byteArray);
    return new String(byteArray);
}

The second method never ends, since it's a blocking call.

When i terminate the Java process, my virtual machine notices the lost of debugger connection, so I'm able to say that the connection was succesfully made.

Can someone help me here? I don't know what to do anymore.

Was it helpful?

Solution

Can you try running the program in the same system, without using the Virtual Machine?

Its not save to ignore the return value of read as it may not read the full length. Your application could hang if you expect to receive the same number of "messages" as you send. This is because a Socket is a stream of bytes and you can have any number of sent strings combine or split.

A safer approach is to use DataOutput/InputStreams

final DataOutputStream out = 

public void sendStr(String str) throws IOException {
    out.writeUTF(str);
}

final DataInputStream in = 

public String getStr() throws IOException {//blocking call
    return in.readUTF(); // will return exactly on string.
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top