I need an app that sends an UDP packet to some network server and receives the response. The server replies to the same port number where request came from, so I first need
Call sendto without calling bind
first, the socket will be bound automatically (to a free port).
Another option is to specify port 0 to bind()
. That will allow you to bind to a specific IP address (in case you have multiple installed) while still binding to a random port. If you need to know which port was picked, you can use getsockname()
after the binding has been performed.
I must be missing something, why don't you use the udp socket to send back data? Start with sendto and then use recvfrom function to read incoming data also you get as a bonus the address from which the data was sent, right there for you to send a response back.