bigpotato
bigpotato

Reputation: 219

Use write() system call to send a UDP broadcast

I am doing some programming on Linux platform.

I want to make a UDP broadcast.

I set the socket option SO_BROADCAST, when I invoke the sendto() system call it perfectly sends the broadcast, but when I use bind() to bind the socket descriptor with the destination address and invoke the write() system call, it raises an error message:

Destination address required

Please give me some tips, thanks!

Upvotes: 0

Views: 1368

Answers (1)

nos
nos

Reputation: 229204

bind() doesn't set up the destination address, it sets up the local (source) address.

You need to use connect() to establish the destination address.

UDP is ofcourse connectionless, but calling connect() will allow you to associate the socket with a remote address - this allows you to use write() and send() on the socket. However, the socket will then only accept incoming messages coming from the address you specify in connect() as well - which might not be desired for broadcast traffic since there should never be messages coming from the broadcast address.

Upvotes: 2

Related Questions