DataInputStreamsで少し問題があります。
だから私はローカルサーバーにデータを送っています、私が読んだバイトはこのフォーマットに従うことを知っています
文字列であることを指定する0x01
次にランダムなバイト数
末尾に0x000x00が続き、
サーバーからの読み取りに問題がありますが、
これが私の読書方法です
public static String convertFromServer(DataInputStream dis) throws IOException{
//Buffer to hold bytes being read in
ByteArrayOutputStream buf = new ByteArrayOutputStream();
if(dis.read() != -1){
//Check to see if first byte == 0x01
if(dis.read() == 0x01){
//Check if byte dosnt equal 0x00, i need it to check if it is actually 0x00 0x00
while(dis.read() != 0x00){
buf.write(dis.read());
}
}
if(dis.read() == 0x03){
while(dis.read() != 0x00){
buf.write(dis.read());
}
}
}
String messageRecevied = new String(buf.toByteArray());
return messageRecevied;
}
私が少し曖昧な場合は私に知らせてください。
私はデータを取り戻していますが、完全には正しくありません。基本的に、最初の要素は0x01で文字列を指定し、次に文字列をバイト単位で、最後の2つの要素は0x00と0x00で、バイト配列を介して送信します。 、その後、このデータはサーバーから私に返送されます、サーバーは間違いなくデータを受信します、ちょうど私がそれを読み返しているとき、それは正しくありません、文字は失われます
このコードは、データを0x01の形式でエンコードし、次にメッセージをバイト単位で、次に0x00,0x00の形式で書き込みます。
public static void writeStringToBuffer(ByteArrayOutputStream buf,String message){
buf.write(0x01);
byte[] b = message.getBytes();
for(int i =1; i<message.getBytes().length+1;i++ ){
buf.write(b[i-1]);
}
buf.write(0x00);
buf.write(0x00);
}