0

(I'm new to network programming, and I am working on C in Linux) I followed Beej's guide for a simple UDP listener-talker and I know how to create a socket and send that to a destination (with calls to getaddrinfo() and socket() using SOCK_DGRAM) See http://beej.us/guide/bgnet/output/html/multipage/clientserver.html#datagram.

In my distributed app, I will need to send a message to multiple peers (reliable multicast). My question is: do I need to create a socket for each of the peers? I'm worried about scalability. Or should I create the socket, use it and destroy it (close it) after each message?

In summary, is there a good way to send a UDP packet to multiple destinations, periodically? Thanks for the help!

superlumi
  • 3
  • 2
  • 1
    I'm concerned by your use of UDP and reliable in the same message :) UDP is not reliable - any part of the communication chain is allowed to drop the packet. – DrC Jun 29 '13 at 21:20

1 Answers1

1

For UDP, you need but one local socket. You can send a packet to any destination you like from that one, single socket.

Also, you don't need to destroy and recreate the socket after each message. Just keep the socket open, and keep sending messages.

Ziffusion
  • 8,779
  • 4
  • 29
  • 57
  • Well, that's great... but now I am lost :) I don't know how to do that... The code that I have for opening a socket uses getaddrinfo(), where the ip and port and some "hints" are specified, mainly ai_family = AF_UNSPEC and ai_socktype = SOCK_DGRAM. Then a call to socket(p->ai_family, p->ai_socktype, p->ai_protocol) until it returns something different than -1. So, how do I change the IP/port of the destination in order to reuse the socket? Thanks for the help! – superlumi Jun 29 '13 at 21:27
  • That part of the code is just setting up the socket. The part where you provide the destination address for the packet comes later, in sendto (see http://linux.die.net/man/2/sendto). The parameter dest_address in the API is where you setup the destination address. You can set that to whatever, and send the packet there. – Ziffusion Jun 29 '13 at 22:13
  • This really helps! Thank you Ziffusion! – superlumi Jun 29 '13 at 22:17