codingfreak
codingfreak

Reputation: 4595

Client UDP Socket binding

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

Upvotes: 0

Views: 3017

Answers (3)

che
che

Reputation: 12263

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.

Upvotes: 0

Dummy00001
Dummy00001

Reputation: 17420

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.

Upvotes: 1

Borealid
Borealid

Reputation: 98459

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

Upvotes: 0

Related Questions