1

Currently I am working on a Java client that sends binary data commands to a device panel. I am having a strange problem that if I am sending one command per socket connection (one TCP session) all commands work well. But when I send multiple commands in one connection (same TCP session), the first command is executed on the device panel perfectly but in the second command it gets two extra bytes with values 0x01 and 0x00. It's strange, and I have been trying for the last couple of days but could not find the answer.

My code:

SSLSocketFactory sslsocketfactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
sslsocket = (SSLSocket) sslsocketfactory.createSocket(deviceIP, port);
//Output Streams
OutputStream outputStream = sslsocket.getOutputStream();

outputStream.write(command1);

outputStream.write(command2);

Note that command1 and command2 are byte arrays:

  • Value in command1 = 01,01,01
  • Value in command2 = 01,34,45,34,56

Log of the device panel is:

Received command: 01 01 01
Received command: 01 00 01 03 45 34 56

What could be the problem? Your suggestions will be highly appreciated.

8
  • Can you test it with plain TCP (without SSL)? Then you can check tcpdump and find out whether command is properly send. Maybe this is receiving side that is failing. Commented Nov 15, 2012 at 20:33
  • No i cant because device has only SSL base port opened on device, I have checked the content of bytes before writing on the stream it look perfect but when i check logs of device there two bytes appeared in the log. Commented Nov 15, 2012 at 20:37
  • How this device knows the length of the command which is sent? Maybe you missed something in protocol? Is this always 01 00 that is added? Have you tried changing this first command? What happens then? Commented Nov 15, 2012 at 20:45
  • Device protocal 01 indicates the start of command and second byte indicates the length of data. so if you analyze first command it is 01(initialization of command) second 01(length of data which is 01 in this case) and 01 ( because single byte of data). Commented Nov 15, 2012 at 20:50
  • I think that you should make some tests to make sure which side of this communication is failing (your Java app or this device). BTW do you flush your output stream after each command? Are you sure nothing else writes to this stream in between? Commented Nov 15, 2012 at 21:02

2 Answers 2

2

I have same problem with Java 6 application with SSLSocket, server received an extra byte between two writes. Extra bytes are a CBC protection for your SSL connection. If you don't want have extra bytes add a java property -Djsse.enableCBCProtection=false to disable CBC protection.

Sign up to request clarification or add additional context in comments.

1 Comment

An application program would never get to see the extra SSL bytes.
0

Java and your OS don't add extra bytes. Otherwise half the Internet wouldn't work. Clearly either the device is malfunctioning or you are mis-observing it.

2 Comments

Well when I replace java sockets with chilkat library implementation my program work fines. but only change that changing from chilkat socket to java socket make my program behave like this.
@zaffargachal When you replaced buggy code with working code your program works fine. Not surprising.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.