drvvvuyia15070493 2019-07-03 00:43
浏览 245
已采纳

在Golang中两个不同的TCP客户端之间中继数据

I'm writing a TCP server which simultaneously accepts multiple connections from mobile devices and some WiFi devices (IOT). The connections needs to be maintained once established, with the 30 seconds timeout if there is no heartbeat received. So it is something like the following:

// clientsMap map[string] conn

func someFunction() {
    conn, err := s.listener.Accept()
    // I store the conn in clientsMap
    // so I can access it, for brevity not 
    // shown here, then:
    go serve(connn)
}

func serve(conn net.Conn) {
    timeoutDuration := 30 * time.Second
    conn.SetReadDeadline(time.Now().Add(timeoutDuration))
    for {
        msgBuffer := make([]byte, 2048)
        msgBufferLen, err := conn.Read(msgBuffer)
        // do something with the stuff
    }
}

So there is one goroutine for each client. And each client, once connected to the server, is pending on the read. The server then processes the stuff read.

The problem is that I sometimes need to read things off one client, and then pass data to another (Between a mobile device and a WiFi device). I have stored the connections in clientsMap. So I can always access that. But since each client is handled by one goroutine, shall I be passing the data from one client to another by using a channel? But if the goroutine is blocked waiting for a pending read, how do I make it also wait for data from a channel? Or shall I just obtain the connection for the other party from the clientsMap and write to it?

  • 写回答

1条回答 默认 最新

  • dongshu9458 2019-07-03 10:42
    关注

    The documentation for net.Conn clearly states:

    Multiple goroutines may invoke methods on a Conn simultaneously.

    So yes, it is okay to simply Write to the connections. You should take care to issue a single Write call per message you want to send. If you call Write more than once you risk interleaving messages from different mobile devices. This implies calling Write directly and not via some other API (in other words don't wrap the connection). For instance, the following would not be safe:

    json.NewEncoder(conn).Encode(myValue) // use json.Marshal(myValue) instead
    io.Copy(conn, src)                    // use io.ReadAll(src) instead
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记