简体   繁体   English

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

[英]Error reading packets over TCP/IP

I'm making a development for connecting one client to a specific tipe of server over TCP/IP. 我正在开发通过TCP / IP将一个客户端连接到服务器的特定Tipe的开发。 I am creating a code that to send bytes over TCP/IP and receive bytes. 我正在创建一个代码,以通过TCP / IP发送字节并接收字节。

The problem is when I receive the packets in certain point it breaks. 问题是,当我在某些时候收到数据包时,它就坏了。

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());
}

Here is my output in console: 这是我在控制台中的输出:

在此处输入图片说明

One problem is when I try to get all the packet if you see on the image it breaks, and only get part. 一个问题是,如果您在图像上看到它破损而只能得到一部分的情况,则尝试获取所有数据包。

The second problem if I delete this lines: 第二个问题,如果我删除此行:

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

The getting packet stops in first P blank blank blank, part of packet. 获取数据包停止在数据包的第一个P空白空白中。

I test with BufferedReader reader = new BufferedReader(new InputStreamReader(input2)); 我测试BufferedReader reader = new BufferedReader(new InputStreamReader(input2)); but the reading packet is incorrect. 但是读取包不正确。

And I know that the packets are incorrect because I am checking it with Wireshark. 而且我知道数据包不正确,因为我正在用Wireshark检查它。

The important part of packet I need is that tells 13001 there is for mi id. 我需要的数据包的重要部分是告诉13001有mi id。

Thanks a lot to all. 非常感谢大家。

Update: 更新:

With my new code thanks to EJP, is working now, but one line is missin at last (I think). 借助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());         
}

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

If you see in the output is missin the large paquet where the ip from the last id is. 如果您在输出中看到的是Missin,则为大块,其中最后一个ID的IP为。 Is not a large problem. 不是很大的问题。 But if I need to end the while, is needed. 但是如果我需要结束片刻,那是需要的。

Thanks a lot to all. 非常感谢大家。

You are reading with readUTF() but you are not writing with writeUTF() . 您正在使用readUTF()进行阅读,但未使用writeUTF()书写。 Have a look at the Javadoc. 看看Javadoc。 There is no chance that readUTF() can read data written with write() . readUTF()不可能读取用write()数据。

You also need to decide as between Reader and Writer on the one hand and input/output streams on the other hand. 您还需要一方面在ReaderWriter之间选择,另一方面在输入/输出流之间进行选择。 Don't mix them. 不要混在一起。

I will point out some problems: DataOutputStream requires flush() to be certain the output is actually written. 我将指出一些问题:DataOutputStream要求使用flush()来确保实际写入输出。 The data you write does not seem to agree with the read methods and the results. 您写入的数据似乎与读取方法和结果不一致。 In UTF P is 0x50. 在UTF中,P为0x50。 -553882341120 is FF FF FF 7F 0A 0A 31 00. The proper way to go is that the write methods agree with the read methods, for example readLong should be matched with writeLong. -553882341120是FF FF FF 7F 0A 0A 3100。正确的处理方法是写方法与读方法一致,例如readLong应该与writeLong匹配。

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

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