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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化