-
dynamic binding
hi, i need help with my homework. i have a sender and a receiver (both uses UDP). my teacher wants us to make the receiver 'dynamically' bind to a port and then 'announce' that port so that the sender would know where to send (we're supposed to use bind() and getsockname() to achieve this). my questions: what exactly is meant by dynamic binding? and how do i use bind() and getsockname() for 'dynamic binding.' i read the man pages for these functions but none mentions 'dynamic binding'
thanks in advance.
-
IIRC, If your port is set to 0 a dynamic one will be bound, it can then be retrieved again with getsockname()
-
By specifying the address INADDR_ANY and the port 0, rather than an explicit address and/or port, you can instruct the socket provider to automatically select a source address and port. I believe this is what your teacher is referring to when he/she talks of "dynamic binding".
From the sounds of your description I expect the teacher wants something like the following:
1. Call bind() to "dynamically bind" to a port/address.
2. Call getsockname to see which port address the socket has been bound to.
3. Announce that information.
I'm prepared to give your instructor the benefit of the doubt for requesting you use bind(), but this can be done simply without bind().
1. Use sendto() to send a one byte packet to the peer. This causes an implicit dynamic bind.
2. Use recvfrom() on the peer. This will return the remote address/port.
On a side note, the nix documentation is often very brief. I see the man page for bind() does not mention the use of INADDR_ANY, etc. You can sometimes pick up extra tips on the msdn page for a socket function, although some aspects do differ.