Java Server socket giving the wrong output when bytearray converted to string. C socket header format
int tccode 4
int len 4
char data[4088] 4088
C Struct format
Tc Code :1001 Size : 256
#ifdef __DTGPL1T__
struct DTGPL1T_S
{
// ENTRY ZONE
short por1cur;
short por2cur;
short por1ten;
short por2ten;
short p1lvim1;
short p1lvim2;
short p1lvim3;
short p2lvim1;
short p2lvim2;
short p2lvim3;
short brc1 ;
short entrylt; short enlopcar;
short notchlen;
short poruse;
short p1remain;
short p2remain;
short eloopcap;
short elooplen;
short untielen;
short entryspd;
short st8ten;
short brc2 ;
short scbtns;
short scbel ;
short scb1intr;
short scb2intr;
short scb3intr;
short st3ten;
short brc3 ;
short plettkts;
short deli1ten;
short d1loopcc;
short d1loopln;
short d1loopcp;
short brc4 ;
short br4ten;
short plcspd;
short triwidt;
short sthosingwidth ;
short trigap1;
short trigap2;
short trigap3;
short trigap4;
short trilap1;
short trilap2;
short trilap3;
short trilap4;
short aactwid;
short adevwid;
short chopw ;
short chopd ;
short tristat;
short trispd;
short trmtoplen;
short deli2ten;
short d2loopcc;
short d2loopcp;
short d2loopln;
short br5ten;
short brc5 ;
short wpd1sta;
short wpd2sta;
short wpd3sta;
short wpd3asta;
short wpd4sta;
short wpd5sta;
short chvsen[2];
short wbsen[2];
short etcsen;
short wpdpass[6];
short wpdlen[6];
short pl_cv_next_move;
short ccv1_lock_curr ;
short ccv2_lock_curr ;
short ewb1_lock_curr ;
short ewb2_lock_curr ;
short etc_lock_curr ;
short eskd1_lock_curr;
short eskd2_lock_curr;
short ecc1_lock_curr ;
short ecc2_lock_curr ;
short st_wid_low_err ;
short st_wid_upp_err ;
short trim_width_set;
short hadling_time;
short spare01[14] ;
short spare02[16] ;
//38 + 16 = 53
};
typedef struct DTGPL1T_S DTGPL1T_T;
typedef DTGPL1T_T *DTGPL1T_P;
#define DTGPL1T_LN sizeof(DTGPL1T_T)
#endif // Eof of __DTGPL1T__
And Java server code
DataInputStream in = new DataInputStream(clientSocket.getInputStream());
//DataInputStream in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
int tcCode = in.readInt();
int length = in.readInt();
if(tcCode == 1001) {
byte[] messageByte = new byte[length];
boolean end = false;
StringBuilder dataString = new StringBuilder(length);
int totalBytesRead = 0;
while(!end) {
int currentBytesRead = in.read(messageByte);
totalBytesRead = currentBytesRead + totalBytesRead;
if(totalBytesRead <= length) {
dataString
.append(new String(messageByte, 0, length - totalBytesRead + currentBytesRead));
} else {
dataString
.append(new String(messageByte, 0, length - totalBytesRead + currentBytesRead));
}
if(dataString.length()>=length) {
end = true;
}
}
System.out.println("Message1 "+dataString);
}
MessageBytes getting like [-1, -2, 0, 0, 0, 0, 0, 0, 0, 35, 0, 33, 0, 30, 0, 45, 0, 40, 0, 35, 0, 0, 0, 0, -1, -1, 0, 10, 0, 1, 0, 1, 0, 1, 5, -123, 0, 111, 3, 34, 0, 0, 0, 0, 0, 0, 2, -25, 0, 24, -1, -2, -1, -3, 0, 0, 0, 0, 0, 0, 43, 37, -1, -35, 0, 0, 0, 72, 10, 42, 0, 0, 43, 37, 0, 0, 49, 126, 112, -30, 0, 5, 0, 5, 0, 11, 0, 12, -3, 68, -3, 68, -6, -20, -6, -20, 27, 86, 0, 0, 0, 16, 0, 16, 0, 0, 0, 0, 0, -13, -1, -7, 0, 0, 16, 107, 0, -99, 0, 80, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, -20, 4, -73, -1, 74, -1] [Output screenshot] 1
Unlike in C, a Java String is not suitable for holding bytes. Converting bytes to a String decodes the bytes, which means some values may be altered or lost.
Even if no values were altered, your data is not going to represent characters, so printing it as text will display nonsense. For instance, many of your bytes are zero, which does not show at all in output, since character \
is a non-printing control character.
You should avoid Strings and StringBuilders entirely. Instead, store your bytes in the byte array only:
while (totalBytesRead < length) {
int currentBytesRead = in.read(messageByte,
totalBytesRead, length - totalBytesRead);
totalBytesRead = currentBytesRead + totalBytesRead;
}
Your data appears to consist entirely of short
values, so, assuming the C client is built to regard short
as a 16-bit value, and assuming the C compiler didn't pad your struct
members in order to word-align them, you can convert your bytes to a sequence of shorts using ByteBuffer and ShortBuffer :
ByteBuffer buffer = ByteBuffer.wrap(messageByte);
ShortBuffer shortBuffer = buffer.asShortBuffer();
To print them, retrieve them as an array and use Arrays.toString :
short[] values = new short[length / 2];
shortBuffer.get(values);
System.out.println(Arrays.toString(values));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.