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

在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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?