net: conn.Read();Contents that are concurrently written twice are read at a time. #42480
Labels
FrozenDueToAge
WaitingForInfo
Issue is not actionable because of missing required information, which needs to be provided.
What version of Go are you using (
go version
)?go1.14.6
What operating system and processor architecture are you using (
go env
)?linux/amd64
What did you do?
i used two gorutines to write data from serve to client concurrently, but the data are read at a time.
write code one:
...
hb, err := json.Marshal(HeartBeatMsg)
if err != nil {
panic(errstd.JsonSerializerErr)
}
for {
time.Sleep(time.Second * 2)
SendMsgLock.Lock()
_, errTcp := sc.conn.Write(hb)
SendMsgLock.Unlock()
if errTcp != nil {
panic(errstd.CommunicationErr)
}
}
...
write code two:
...
req := &Message{MsgNo: no, Data: data}
bytes, err := json.Marshal(req)
if err != nil {
panic(errstd.JsonSerializerErr)
}
SendMsgLock.Lock()
_, errTcp := sc.conn.Write(bytes)
SendMsgLock.Unlock()
if errTcp != nil {
panic(errstd.CommunicationErr)
}
...
read code:
...
rBuf := make([]byte, ReadBufferSize)
read, err := sc.conn.Read(rBuf)
if err != nil {
sc.conn.Close()
break
}
content := &Message{}
errDes := json.Unmarshal(rBuf[0:read], content)
if errDes != nil {
fmt.Println(string(rBuf[0:read]))
panic(errstd.JsonDeserializerErr)
}
...
error:
string(rBuf[0:read]):
{"MsgNo":6,"Data":"eyJJbmRleCI6MTkzdFJlcSI6ZmFsc2V9"}{"MsgNo":0,"Data":null}
i hope that i can receive the data '{"MsgNo":6,"Data":"eyJJbmRleCI6MTkzdFJlcSI6ZmFsc2V9"}' after calling conn.Read(rBuf),
and receive the data '{"MsgNo":0,"Data":null}' after the next calling conn.Read(rBuf),
The text was updated successfully, but these errors were encountered: