dousu5608 2018-04-13 22:28
浏览 413
已采纳

在TCP连接的端口上发送UDP数据包[关闭]

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...?

  • 写回答

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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?