网络编程 - Java调用DataOuputStream的write方法写入数据第一个字节不知所踪
大家讲道理
大家讲道理 2017-04-18 10:36:57
0
4
910
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(4)
巴扎黑

Have you solved the TCP sticky problem?
Since it is TCP, we have to consider 粘包的问题。粘包If the problem is not solved, you will not be able to determine which bytes are the first 4 bytes of a packet.

In short, you receive a lot of bytes, and the source of these bytes may be as follows:

  • Send all the bytes of two or three packets together

  • The second half of the previous packet and the first half of the bytes of the next packet are sent together

  • In short, it’s a mixture of half packs or sticky packs

We use netty,tcp粘包的问题,这里有一个很好阐述和基于netty’s solution: https://my.oschina.net/andylu...

伊谢尔伦

What does it mean to read the input stream directly in Java without capturing the packet?

巴扎黑

This is a socket communication header problem. This is how I handled it in my previous project. The code snippet is as follows:

send(String url, int port, String content){
    Socket socket = null;
    OutputStream out = null;
    DataOutputStream dataOutputStream = null;
    try {
        socket = new Socket(url, port);
        out = socket.getOutputStream();
        dataOutputStream = new DataOutputStream(out);
        dataOutputStream.writeInt(content.getBytes().length);
        dataOutputStream.write(content.getBytes());
        dataOutputStream.flush();
        ...
    }
}

It is recommended to write a receiving socket for debugging. I have never used the packet capture tool. Or you can jointly debug with the other party. If the other party can receive and successfully parse it, it will be OK. There is no need to get into trouble with the packet capture.
Hope it can help you~

迷茫

Have you called flush?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template