conection to the simulator from java?

Apr 7, 2011 at 8:55 AM
Edited Apr 7, 2011 at 2:01 PM


please I'd like to use the simulator from a java progaram : I've used this program:

	Socket socket = new Socket("", 9998);

	InputStream in = socket.getInputStream();
	OutputStream out = socket.getOutputStream();
	BufferedOutputStream buffered = new BufferedOutputStream(out, 1024);

	String command = "0000NC"; 
        byte [] inBuffer = ("" + command).getBytes();
	inBuffer[0] = (byte) (command.length() / 256);
	inBuffer[1] = (byte) (command.length() % 256);
	System.out.println((int) inBuffer[1]);

but the probleme it don't work , i can get just a simple connection to the simulator, but no command is sent,


Apr 7, 2011 at 10:21 AM
Edited Apr 7, 2011 at 10:27 AM

i've solved the probleme by adding 2 spaces in :

byte [] inBuffer = ("  " + command).getBytes();

but I haven't undestand why we must do that?

best regards.


Apr 7, 2011 at 4:33 PM

The problem is with the original code. The declaration:

byte [] inBuffer = ("" + command).getBytes();

does not leave space for the length, so the inBuffer[0] and inBuffer[1] assignments override the command data. In essence, you wanted to send 0000NC which is six bytes. If you add the 2-byte software header, you'd need 2+6=8 bytes but your original code only includes six bytes. The simulator gets the packet you've send out, reads the command length and knows that it needs to find 6 bytes' worth of command but it only finds 4. So it sits still and expects the other 2 bytes, which never happens.