Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deal with TCP unpacking / gluing with go language

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "go language how to deal with TCP unpacking / gluing", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "go language how to deal with TCP unpacking / gluing" bar!

Part 1

Recently, I was learning the rpc that comes with go, and after watching it, I thought about implementing a codec myself, that is, serialization and deserialization of custom messages. Serialization and deserialization of messages involves two steps:

1. Read data from and write data to the network

2. Deserialize and serialize existing objects into binary data according to the binary data obtained. In this process, you need to deal with the unpacking and sticking of TCP.

The unpacking / sticking of TCP is also a relatively basic problem in network programming, and the specific meaning and solution of the problem are no longer described in detail. Although programmers who implement application-layer logic may not need to care about this at all, as a middleware development, but also for the purpose of learning the go language, or a little practice.

Part 2

The solution of TCP unpacking and sticking package: when reading the data, the binary data read can be divided in the correct position. Here we use head+body directly, that is, when sending data, we append the size of the whole data to the data first, like this:

+ +

Size (2 bytes) | body (size bytes)

+ +

Here, the size of the packet is used as the head

Note: in the example given here, the specific number of byte;size occupied by size can be determined according to the actual situation.

Part 3

The specific implementation of server:

Func doConn (conn net.Conn) {var (buffer = bytes.NewBuffer (make ([] byte, 0, BUF_SIZE)) / / buffer is used to cache read data readBytes = make ([] byte, BUF_SIZE) / / readBytes is used to receive data read each time Add readBytes to buffer after each read is completed isHead = true / / to identify the current state: is the size part being processed or the body part bodyLen = 0 / / indicates the length of the body) for {/ / read the data readByteNum first Err: = conn.Read (readBytes) if err! = nil {log.Fatal (err) return} buffer.Write (readBytes [0: readByteNum]) / / put the read data into buffer / / then process the data for {if isHead {if buffer.Len () > = HEAD_ SIZE {isHead = false head: = make ([] byte HEAD_SIZE) _, err = buffer.Read (head) if err! = nil {log.Fatal (err) return} bodyLen = int (binary.BigEndian.Uint16 (head))} else {break }} if! isHead {if buffer.Len () > = bodyLen {body: = make ([] byte, bodyLen) _ Err = buffer.Read (body [: bodyLen]) if err! = nil {log.Fatal (err) return} fmt.Println ("received body:" + string (body [: bodyLen])) isHead = true } else {break } func HandleTcp () {listener, err: = net.Listen ("tcp", ": 1234") if err! = nil {log.Fatal (err) return} log.Println ("start listening on 1234") for {conn, err: = listener.Accept () if err! = nil {log.Fatal (err) return} go doConn (conn)}}

The specific implementation of client:

Func SendStringwithTcp (arg string) error {conn, err: = net.Dial ("tcp", ": 1234") if err! = nil {log.Fatal (err) return err} head: = make ([] byte, server.HEAD_SIZE) content: = [] byte (arg) headSize: = len (content) binary.BigEndian.PutUint16 (head, uint16 (headSize)) / / write the head section first Then write the body section _, err = conn.Write (head) if err! = nil {log.Fatal (err) return err} _, err = conn.Write (content) if err! = nil {log.Fatal (err) return err} return nil} Thank you for reading. This is the content of "how go language handles TCP unpacking / gluing". After the study of this article, I believe that you have a deeper understanding of how the go language deals with the problem of TCP unpacking / gluing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report