2018-04-13 22:28
浏览 373


I have a Golang TCP server, i.e. net.TCPConn, connected on a port which, in addition to a TCP stream, also has to receive UDP packets and respond with UDP packets. The incoming UDP packet pops out at the server (from a net.TCPConn.Read()) but I can't figure out how to send a UDP packet back again. All of the UDP write methods apply only to net.UDPConn. net.UDPConn.WriteMsgUDP() tantalisingly talks of whether it is applied to a connected or a non-connected socket, but I can't figure out how to derive net.UDPConn from net.TCPConn; I've tried casting net.TCPConn to net.UDPConn but that causes a panic.

What is the correct way to do this?

FYI, I do have a UDP listener open on the same port ('cos the client at the other end can chose to operate in completely connectionless mode) but since, when the socket is connected, the UDP packet arrives at the TCP server rather than the UDP server, I'd like to send the UDP response back down the same hole, rather than having to mix the two up in some unholy manner. Or is unholiness the answer?

EDIT: a word on the system design here: the purpose of this UDP packet is to test the connection on this socket (the server simply echoes it back). The socket is a [hopefully] established SSH port-forwarding tunnel, hence I don't want to use another socket as this wouldn't test what I'm trying to test (i.e. that both the socket and the SSH tunnel are open; it is a shortcoming of SSH port-forwarding tunnels that, since the application makes a connection to localhost, the socket will report connected immediately, even if the server isn't actually connected at the time). The SSH tunnel otherwise carries a stream of TCP traffic and I specifically want to use UDP for this as I don't want my UDP connection test to be stuck behind the queue of TCP traffic; timing is important in this application and the UDP packet carries timestamps to measure it. Sending a UDP packet on a connected socket is a valid sockets operation, Go must have a way to do it...?

图片转代码服务由CSDN问答提供 功能建议

我在端口上连接了Golang TCP服务器,即 net.TCPConn ,除了TCP数据流外,还必须接收UDP数据包并以UDP数据包进行响应。 传入的UDP数据包从服务器弹出(从 net.TCPConn.Read()),但是我不知道如何再次发送UDP数据包。 所有UDP写入方法仅适用于 net.UDPConn net.UDPConn.WriteMsgUDP()诱人地讨论了它是应用于已连接的套接字还是未连接的套接字,但是我不知道如何导出 net.UDPConn 来自 net.TCPConn ; 我已经尝试将 net.TCPConn 强制转换为 net.UDPConn ,但这会引起恐慌。

正确的方法是 这样吗?

仅供参考,我确实在同一端口上打开了UDP侦听器(因为另一端的客户端可以选择以完全无连接模式运行),但是自从 连接后,UDP数据包到达TCP服务器而不是UDP服务器,我想将UDP响应发送回同一孔,而不必以某种不合时宜的方式将两者混在一起。

编辑:这是系统设计上的一个字:此UDP数据包的目的是测试此套接字上的连接(服务器只是将其回显)。 该套接字是一个[希望]建立的SSH端口转发隧道,因此,我不想使用其他套接字,因为这不会测试我要测试的内容(即套接字和SSH隧道均已打开; SSH端口转发隧道的一个缺点是,由于应用程序建立了与 localhost 的连接,因此套接字会立即报告已连接,即使此时服务器实际上并未连接也是如此。 否则,SSH隧道将承载TCP通信流,我特别想为此使用UDP,因为我不希望UDP连接测试滞留在TCP通信队列中。 定时在此应用中很重要,并且UDP数据包带有时间戳以对其进行测量。 在已连接的套接字上发送UDP数据包是有效的套接字操作,Go必须具有 怎么做...?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douque9815 2018-04-14 16:41

    After a discussion on the Golang forums all has become clear.

    @JimB is quite right that you cannot send a UDP packet on a TCP port. The reason I thought this was possible is that the definition of sendto() says:

    If sendto() is used on a connection-mode (SOCK_STREAM, SOCK_SEQPACKET) socket, the arguments dest_addr and addrlen are ignored (and the error EISCONN may be returned when they are not NULL and 0), and the error ENOTCONN is returned when the socket was not actually connected.

    ...and when I called sendto() on my TCP connected port the data I sent did indeed turn up at my Golang net.TCPConn end-point. However, what is happening in this scenario is that, under the hood, sendto() has effectively become an alias for send() and, despite calling sendto(), the data sent is actually being transported in a TCP packet and not a UDP packet at all. This was proved by using netcat -u $host $port to send UDP traffic to the server and netcat $host $port to send TCP traffic to the server: the former did not produce any data at the net.TCPConn end-point while the latter did.

    The correct way to do this is for the client to open a TCP and a UDP socket to the server on the same port at the same time while the server, likewise, listens simultaneously for TCP and UDP "connections" (the UDP connection is not a connection at all of course, it is simply an association with a local port number) on the same port. The TCP server then handles my stream and the UDP server handles my UDP test/measurement packets, both down the same SSH port forwarding tunnel.

    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题