Golang服务器,如何接收TCP JSON数据包?

huangapple go评论111阅读模式
英文:

Golang server, how to receive TCP JSON packet?

问题

我是你的中文翻译助手,以下是翻译好的内容:

我对Golang还不熟悉,正在使用这里的“Server”代码作为起点:http://www.golang-book.com/13/index.htm#section7

我尝试使用JSON而不是Gob解码(因为我需要用C#编写客户端),并且我将JSON TCP数据的客户端数据发送到与下面代码分开的脚本中。

我卡在实际接收JSON TCP数据并将其存储在变量中以进行解码的部分。看起来我可以使用json.Unmarshal进行解码,但我找不到任何使用json.Unmarshal解码TCP数据的示例。我只能找到使用json.Unmarshal解码JSON字符串的示例。

我的代码如下:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. )
  7. type coordinate struct {
  8. X float64 `json:"x"`
  9. Y float64 `json:"y"`
  10. Z float64 `json:"z"`
  11. }
  12. func server() {
  13. // 监听端口
  14. ln, err := net.Listen("tcp", ":9999")
  15. if err != nil {
  16. fmt.Println(err)
  17. return
  18. }
  19. for {
  20. // 接受连接
  21. c, err := ln.Accept()
  22. if err != nil {
  23. fmt.Println(err)
  24. continue
  25. }
  26. // 处理连接
  27. go handleServerConnection(c)
  28. }
  29. }
  30. func handleServerConnection(c net.Conn) {
  31. // 接收消息
  32. var msg coordinate
  33. // 卡在下面这行。我应该将rawJSON变量设置为什么值?
  34. err := json.Unmarshal([]byte(rawJSON), &msg)
  35. if err != nil {
  36. fmt.Println(err)
  37. } else {
  38. fmt.Println("Received", msg)
  39. }
  40. c.Close()
  41. }
  42. func main() {
  43. go server()
  44. // 让服务器goroutine一直运行
  45. var input string
  46. fmt.Scanln(&input)
  47. }

希望对你有帮助!

英文:

I'm new to Golang and am using the "Server" code here as a starting point: http://www.golang-book.com/13/index.htm#section7

I've attempted to use JSON instead of Gob decoding (since I am required to write the client in C#), and I'm sending the JSON TCP data client data in a separate script from the code below.

I'm stuck on the part where I'm actually receiving the JSON TCP data and storing it in a variable for it to be decoded. It looks like I can decode it with json.Unmarshal, but I can't find any examples where json.Unmarshal is being used to decode TCP data. I can only find examples where json.Unmarshal is being used to decode JSON strings.

My code is below:

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net"
  6. )
  7. type coordinate struct {
  8. X float64 `json:"x"`
  9. Y float64 `json:"y"`
  10. Z float64 `json:"z"`
  11. }
  12. func server() {
  13. // listen on a port
  14. ln, err := net.Listen("tcp", ":9999")
  15. if err != nil {
  16. fmt.Println(err)
  17. return
  18. }
  19. for {
  20. // accept a connection
  21. c, err := ln.Accept()
  22. if err != nil {
  23. fmt.Println(err)
  24. continue
  25. }
  26. // handle the connection
  27. go handleServerConnection(c)
  28. }
  29. }
  30. func handleServerConnection(c net.Conn) {
  31. // receive the message
  32. var msg coordinate

Stuck on the line below. What could I set the rawJSON variable equal to?

  1. err := json.Unmarshal([]byte(rawJSON), &msg)
  2. if err != nil {
  3. fmt.Println(err)
  4. } else {
  5. fmt.Println("Received", msg)
  6. }
  7. c.Close()
  8. }
  9. func main() {
  10. go server()
  11. //let the server goroutine run forever
  12. var input string
  13. fmt.Scanln(&input)
  14. }

答案1

得分: 13

你可以直接将json.Decoder修补到连接上:

  1. func handleServerConnection(c net.Conn) {
  2. // 创建一个直接从套接字读取的解码器
  3. d := json.NewDecoder(c)
  4. var msg coordinate
  5. err := d.Decode(&msg)
  6. fmt.Println(msg, err)
  7. c.Close()
  8. }

这段代码的作用是处理服务器连接,并从连接中读取JSON数据并解码为coordinate类型的变量msg。然后,它打印出msg和可能的错误,并关闭连接。

英文:

You can patch a json.Decoder directly to the connection:

  1. func handleServerConnection(c net.Conn) {
  2. // we create a decoder that reads directly from the socket
  3. d := json.NewDecoder(c)
  4. var msg coordinate
  5. err := d.Decode(&msg)
  6. fmt.Println(msg, err)
  7. c.Close()
  8. }

huangapple
  • 本文由 发表于 2015年5月12日 19:56:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/30190159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定