views:

70

answers:

3

Hi

I am creating UDP socket for a UDP client and sending UDP packets with different port numbers and wait for the reply from the destination for certain amount of time.

My doubt is .. Is it possible to re-bind a UDP socket to multiple port numbers(even IP-address) to the same socket FD without closing the socket ?? (I cant use RAW sockets :()

EDIT1: I have tried to bind two different socket file descriptors with same IP-Address and Portnumber but I have mixed results .. (For both socket file descriptors I am setting SO_REUSEADDR option).

In Linux kernel 2.6.8

First Socket FD - binds successfully.

Second Socket FD: Returns error 98 saying Address already in use.

In Linux Kernel 2.6.24

First Socket FD: binds successfully

Second Socket FD: binds successfully

A: 

Try using select() on a group of open sockets.

Borealid
Hmm select() can be used only on mutiple sockets which are opened but not on the single SOCKET FD :(
codingfreak
@codingfreak: each listening socket requires its own FD. Open one socket per port/address pair on which you listen. Then `select` in a loop to find the next one to read.
Borealid
@codingfreak: It's perfectly ok to use `select()` on only one socket fd.
che
+1  A: 

My doubt is .. Is it possible to re-bind a UDP socket to multiple port numbers(even IP-address) to the same socket FD without closing the socket ??

It appears that POSIX has that now officially as unsupported, quote: The bind() function shall assign a local socket address address to a socket identified by descriptor socket that has no local socket address assigned.

In past I have heard that re-bind()ing was possible on some platforms, though personally I have never used that.

Is there any best solution other than this ???

Keep a cache of the open UDP sockets, use the sockets with poll() for the IO multiplexing and time-out handling.

Dummy00001
A: 

To achieve this you can use one UDP socket bonud to one port to receive the data, and other (bound to different port) to do the sending.

che
THis is not good solution in my case .. as I have to send and receive the data on the same socket ... seems select() really gonna help me out.
codingfreak