conection to the simulator from java?

Apr 7, 2011 at 7:55 AM
Edited Apr 7, 2011 at 1:01 PM

Hello,

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

	
	Socket socket = new Socket("127.0.0.1", 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]);
	buffered.write(inBuffer);
	buffered.flush();

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

thanks.

Apr 7, 2011 at 9:21 AM
Edited Apr 7, 2011 at 9: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.

 

Coordinator
Apr 7, 2011 at 3: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.