在GoLang UDP服务器中获取分段数据包

I need to implement a udp listener in go to read incoming packets from a port and parse them as json. The problem is I'm getting fragmented packets which breaks parsing as they're incomplete json.

const BufferSize = 15214400
func udpListener(ip []byte, port int) {
  addr := net.UDPAddr{IP: ip, Port: port, Zone: ""}
  ServerConn, err := net.ListenUDP("udp", &addr)
  if err != nil {
    log.Panicln(err.Error())
  }
  ServerConn.SetReadBuffer(BufferSize)
  defer ServerConn.Close()
  buffer := make([]byte, BufferSize)
  for {
    buffLen, senderAddress, err := ServerConn.ReadFromUDP(buffer)
    if err != nil {
      log.Errorln("Error occurred while read data from UDP: " + err.Error())
      continue
    }
    go handleUdpDatagram(buffLen, buffer, senderAddress.String())
  }
}

func handleUdpDatagram(buffLen int, buffer []byte, senderAddress string) {
  var jsonData map[string]interface{}
  if err := json.Unmarshal(buffer[0:buffLen], &jsonData); err != nil {
    log.Errorln("Error occurred while parsing data: " + data)
    return
  }
}

The packets chopped at a random number of bytes and sent in next read. The size of chopped packets is not constant, I'm getting other packets intact with bigger sizes.

Sample packet:

{"_id":"5c7c366e29c0094fcb03306e","index":0,"guid":"04fbc039-7fb4-4b3f-9722-60a0617b5469","isActive":true,"balance":"$3,431.05","picture":"http://placehold.it/32x32","age":25,"eyeColor":"green","name":"Reynolds Rodgers","gender":"male","company":"BITENDREX","email":"reynoldsrodgers@bitendrex.com","phone":"+1 (905) 547-2873","address":"778 Mermaid Avenue, Draper, Northern Mariana Islands, 4983","about":"Qui ad velit ut irure eu veniam laborum nulla fugiat magna nostrud irure in laboris. Laborum commodo enim Lorem anim id. Ex cupidatat do ad labore cillum cillum aute cillum. Incididunt velit duis nostrud deserunt anim. Aliqua aute commodo magna in deserunt reprehenderit consectetur aliquip ea proident. Ullamco deserunt qui aliqua ad mollit commodo enim pariatur enim labore cillum. Eu deserunt quis adipisicing nisi sint magna proident voluptate fugiat eiusmod ut nostrud exercitation sit.
","registered":"2018-11-14T06:24:10 -06:-30","latitude":-70.393932,"longitude":-95.507978,"tags":["esse","voluptate","eu","enim","ullamco","aute","velit"],"friends":[{"id":0,"name":"Wendi Graves"},{"id":1,"name":"Hogan Bean"},{"id":2,"name":"Garrison Bennett"}],"greeting":"Hello, Reynolds Rodgers! You have 2 unread messages.","favoriteFruit":"banana"}

When I send this packet, the listener receives it in 2 fragments

time="2019-03-04T01:51:47+05:30" level=error msg="Error occurred while parsing data: im\",\"ullamco\",\"aute\",\"velit\"],\"friends\":[{\"id\":0,\"name\":\"Wendi Graves\"},{\"id\":1,\"name\":\"Hogan Bean\"},{\"id\":2,\"name\":\"Garrison Bennett\"}],\"greeting\":\"Hello, Reynolds Rodgers! You have 2 unread messages.\",\"favoriteFruit\":\"banana\"}
\"male\",\"company\":\"BITENDREX\",\"email\":\"reynoldsrodgers@bitendrex.com\",\"phone\":\"+1 (905) 547-2873\",\"address\":\"778 Mermaid Avenue, Draper, Northern Mariana Islands, 4983\",\"about\":\"Qui ad velit ut irure eu veniam laborum nulla fugiat magna nostrud irure in laboris. Laborum commodo enim Lorem anim id. Ex cupidatat do ad labore cillum cillum aute cillum. Incididunt velit duis nostrud deserunt anim. Aliqua aute commodo magna in deserunt reprehenderit consectetur aliquip ea proident. Ullamco deserunt qui aliqua ad mollit commodo enim pariatur enim labore cillum. Eu deserunt quis adipisicing nisi sint magna proident voluptate fugiat eiusmod ut nostrud exercitation sit.
\",\"registered\":\"2018-11-14T06:24:10 -06:-30\",\"latitude\":-70.393932,\"longitude\":-95.507978,\"tags\":[\"esse\",\"voluptate\",\"eu\",\"en error: invalid character 'i' looking for beginning of value"
time="2019-03-04T01:51:47+05:30" level=error msg="Error occurred while parsing data: im\",\"ullamco\",\"aute\",\"velit\"],\"friends\":[{\"id\":0,\"name\":\"Wendi Graves\"},{\"id\":1,\"name\":\"Hogan Bean\"},{\"id\":2,\"name\":\"Garrison Bennett\"}],\"greeting\":\"Hello, Reynolds Rodgers! You have 2 unread messages.\",\"favoriteFruit\":\"banana\"}
 error: invalid character 'i' looking for beginning of value"
donglu1472
donglu1472 数据报不能在套接字接口上进行分段,因为实际的分段数据包是由OS重组的。您正在多个goroutine之间共享相同的缓冲区。使用竞赛检测器运行代码。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问