This article illustrates the solution to the problem of Golang network socket sticky package. Share to everyone for your reference, specific as follows:
See a lot of people ask this question, today wrote an example, hope to help people
First of all, what is the sticky bag: Baidu on the more popular saying is that the TCP protocol, the sender sent a number of packet data to receive the receiver to stick into a packet, from the receiving buffer to look at the last packet of data header immediately after the end of the packet data .
The solution is as follows:
Service side:
Copy Code code as follows:
Package Main
Import (
"Bytes"
"Encoding/binary"
"FMT"
"IO"
"NET"
)
Func Main () {
Listening port
ln, ERR: = Net. Listen ("TCP", ": 6000")
If Err!= nil {
Fmt. Printf ("Listen Error:%s\n", err)
Return
}
Listening loop
for {
Accept Client Links
Conn, err: = ln. Accept ()
If Err!= nil {
Fmt. Printf ("Accept Error:%s\n", err)
Continue
}
Working with client links
Go handleconnection (conn)
}
}
Func handleconnection (Conn net. Conn) {
Close link
Defer Conn. Close ()
Client
Fmt. Printf ("Client:%s\n", Conn.) REMOTEADDR ())
Message buffering
Msgbuf: = bytes. Newbuffer ([]byte, 0, 10240)
Data buffering
Databuf: = Make ([]byte, 4096)
Message length
Length: = 0
Message length UInt32
Ulength: = UInt32 (0)
Data loops
for {
Reading data
N, err: = conn. Read (DATABUF)
If err = = Io. EOF {
Fmt. Printf ("Client exit:%s\n", Conn.) REMOTEADDR ())
}
If Err!= nil {
Fmt. Printf ("Read Error:%s\n", err)
Return
}
Fmt. Println (Databuf[:n])
Data added to message buffering
N, err = Msgbuf. Write (Databuf[:n])
If Err!= nil {
Fmt. Printf ("Buffer write Error:%s\n", err)
Return
}
Message split Loops
for {
Message headers
If length = = 0 && msgbuf. Len () >= 4 {
Binary. Read (msgbuf, Binary. Littleendian, &ulength)
length = Int (ulength)
Check for super long messages
If length > 10240 {
Fmt. Printf ("Message too length:%d\n", length)
Return
}
}
Message body
If length > 0 && msgbuf. Len () >= Length {
Fmt. Printf ("Client messge:%s\n", String (MSGBUF). Next (length))
Length = 0
} else {
Break
}
}
}
}
Client:
Copy Code code as follows:
Package Main
Import (
"Bytes"
"Encoding/binary"
"FMT"
"NET"
"Time"
)
Func Main () {
Linked server
Conn, Err: = Net. Dial ("TCP", "127.0.0.1:6000")
If Err!= nil {
Fmt. Printf ("Dial Error:%s\n", err)
Return
}
Client Information
Fmt. Printf ("Client:%s\n", Conn.) LOCALADDR ())
Message buffering
Msgbuf: = bytes. Newbuffer ([]byte, 0, 1024)
Message content
Message: = []byte ("I am Utf-8")
Message length
Messagelen: = UInt32 (len (message))
Total message length
Mlen: = 4 + len (message)
Write 5 messages
For I: = 0; I < 10; i++ {
Binary. Write (msgbuf, Binary. Littleendian, Messagelen)
Msgbuf. Write (Message)
}
Single packet sending a message
Conn. Write (msgbuf. Next (Mlen))
Time. Sleep (time. Second)
Single packet send three messages
Conn. Write (msgbuf. Next (Mlen * 3))
Time. Sleep (time. Second)
Send an incomplete message header
Conn. Write (msgbuf. Next (2))
Time. Sleep (time. Second)
Send Message left part
Conn. Write (msgbuf. Next (mlen-2))
Time. Sleep (time. Second)
Send an incomplete message body
Conn. Write (msgbuf. Next (mlen-6))
Time. Sleep (time. Second)
Send Message left part
Conn. Write (msgbuf. Next (6))
Time. Sleep (time. Second)
Multi-segment Send
Conn. Write (msgbuf. Next (Mlen + 2))
Time. Sleep (time. Second)
Conn. Write (msgbuf. Next ( -2 + mlen-8))
Time. Sleep (time. Second)
Conn. Write (msgbuf. Next (8 + 1))
Time. Sleep (time. Second)
Conn. Write (msgbuf. Next ( -1 + Mlen + mlen))
Time. Sleep (time. Second)
Close link
Conn. Close ()
}
I hope this article will help you with your go language program.