繁体   English   中英

通过TCP / IP读取数据包时出错

[英]Error reading packets over TCP/IP

我正在开发通过TCP / IP将一个客户端连接到服务器的特定Tipe的开发。 我正在创建一个代码,以通过TCP / IP发送字节并接收字节。

问题是,当我在某些时候收到数据包时,它就坏了。

String input;
Traductor traductor = new Traductor();
PrintWriter writer = new PrintWriter(this.socket.getOutputStream(), true);
// Write a message to server       
byte[] theBytesInmediate = new byte[]{0x11, 0x00, 0x14, 0x50, 0x00, 0x01,  0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x7F, (byte) 0x0A, 0x02, (byte)0x2A, 0x00, 0x02, 0x02, 0x31, 0x00};
DataOutputStream salida = new DataOutputStream(this.socket.getOutputStream());
salida.write(theBytesInmediate);
System.out.println("Packet sent"); 
InputStream input2 = socket.getInputStream();

String tmp ="";
while(true){
     DataInputStream entrada = new 
     DataInputStream(this.socket.getInputStream());
     System.out.println("bytes: " + entrada.readByte());
     try{
        tmp =tmp +  entrada.readUTF();
        System.out.println("readUTF: " + tmp);
     }catch(IOException e){
                System.out.println(e);
     }      
     System.out.println("read: " + entrada.read());
     System.out.println("readChar: " + entrada.readChar());
     System.out.println("readLong: " + entrada.readLong());
}

这是我在控制台中的输出:

在此处输入图片说明

一个问题是,如果您在图像上看到它破损而只能得到一部分的情况,则尝试获取所有数据包。

第二个问题,如果我删除此行:

System.out.println("read: " + entrada.read());
System.out.println("readChar: " + entrada.readChar());
System.out.println("readLong: " + entrada.readLong());

获取数据包停止在数据包的第一个P空白空白中。

我测试BufferedReader reader = new BufferedReader(new InputStreamReader(input2)); 但是读取包不正确。

而且我知道数据包不正确,因为我正在用Wireshark检查它。

我需要的数据包的重要部分是告诉13001有mi id。

非常感谢大家。

更新:

借助EJP,有了我的新代码,它现在可以工作了,但是最后一行缺失了(我认为)。

PrintWriter writer = new PrintWriter(this.socket.getOutputStream(), true);  
byte[] theBytesInmediate = new byte[]{0x11, 0x00, 0x14, 0x50, 0x00, 0x01,  0x04, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x7F, (byte) 0x0A, 0x02, (byte)0x2A, 0x00, 0x02, 0x02, 0x31, 0x00};
DataOutputStream salida = new DataOutputStream(this.socket.getOutputStream());
String str = new String(theBytesInmediate, StandardCharsets.UTF_8);

writer.println(str);
//writer.flush();
System.out.println("Packet sent"); 
InputStream input2 = this.socket.getInputStream();

BufferedReader reader = new BufferedReader(new InputStreamReader(input2));
String tmp ="";
while(true)
{
   System.out.println("read: " + reader.readLine().toString());
   System.out.println("read: " +reader.read());         
}

输出: 在此处输入图片说明

如果您在输出中看到的是Missin,则为大块,其中最后一个ID的IP为。 不是很大的问题。 但是如果我需要结束片刻,那是需要的。

非常感谢大家。

您正在使用readUTF()进行阅读,但未使用writeUTF()书写。 看看Javadoc。 readUTF()不可能读取用write()数据。

您还需要一方面在ReaderWriter之间选择,另一方面在输入/输出流之间进行选择。 不要混在一起。

我将指出一些问题:DataOutputStream要求使用flush()来确保实际写入输出。 您写入的数据似乎与读取方法和结果不一致。 在UTF中,P为0x50。 -553882341120是FF FF FF 7F 0A 0A 3100。正确的处理方法是写方法与读方法一致,例如readLong应该与writeLong匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM