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 (I explain why below). 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 on the socket. 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 ham-fistedly type asserting net.TCPConn
to net.UDPConn
but, unsurprisingly, that causes a panic.
What is the correct way to achieve my aim?
Why I want to do this: the purpose of this UDP packet is to test the connection on this socket (the server simply has to echo 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, hence the need for this test). 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…?