I am using TCP for creating a stable connection to configure (cli) a data generator. The generated data is real time and there is no need to keep it if it is lost. Also, there's lots of it. So I simply want to stream it out on UDP. I am using C sockets for this.
In my experience: an UDP client first needs to send something to the UDP server, so that the server knows which port it uses. This also makes sure the firewall will accept and forward incoming data on this port to the UDP client.
This immediately means you need to implement some form of bidirectional communication with error checking and retrying to make sure it works as UDP gives no guarantees. Also there is some kind of need for checking if the client is still there if you don't want to keep sending data to nowhere endlessly. Please correct me if I'm wrong with this. I don't need all of this, I simply want the data to be sent out to the client as long as it is there.
Most of these things is already available in TCP: letting the server know you're there is a connection and the server knows you left if the connection closes.
So, is it possible to initiate the UDP data transfer through TCP (and how do you do this)? Can the UDP client open the UDP port so the firewall accepts it and know what port it is so it can send a TCP command like: 'please send me your data on UDP port XXXX'? When the TCP connection closes, the server can then assume the UDP stream can be stopped too.
an UDP client first needs to send something to the UDP server, so that the server knows which port it uses.
That information can easily be conveyed over the TCP connection.
This also makes sure the firewall will accept and forward incoming data on this port to the UDP client.
That much is true. Typically, unless the client notifies the firewall directly to open a UDP port, then the firewall admin should open a port first and then tell the client which port it can use.
Also there is some kind of need for checking if the client is still there if you don't want to keep sending data to nowhere endlessly.
You can use the presence of the TCP connection for that purpose. If the TCP connection stays alive, you can assume the UDP receiver is still running, too. The client can notify the server via the TCP connection when to stop sending UDP data. And if the TCP connection dies, the server can assume the UDP receiver is gone, too. As long as the client wants to receive UDP, it should leave the TCP connection open.
is it possible to initiate the UDP data transfer through TCP (and how do you do this)?
TCP and UDP sockets are completely separate from each other. But nothing stops you from creating the UDP socket and then conveying its information over the TCP connection.
Can the UDP client open the UDP port so the firewall accepts it
Yes, though it may or may not require manual intervention, depending on your setup.
and know what port it is so it can send a TCP command like: 'please send me your data on UDP port XXXX'?
Yes, either by knowing the UDP port ahead of time and binding the UDP socket to it, or by binding the UDP socket to port 0 to let the OS choose a random port dynamically and then using getsockname()
to find out which port the OS chose.
When the TCP connection closes, the server can then assume the UDP stream can be stopped too.
Yes.