net: conn.Read();Contents that are concurrently written twice are read at a time. #42480
Labels
Comments
Ask this question in the forums first; see https://golang.org/wiki/Questions. You should only open an issue if you believe you've found a bug, and then carefully fill the issue template. You omitted at least two sections of the template. |
Show us a complete standalone test case. |
this is test case for go: https://github.com/justkeepsimple/socketRead_go |
@justkeepsimple there is a bug in your code. This is the fix, justkeepsimple/socketRead_go#3 |
thx!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: