I was trying to write a udp server who send an instance of a file to multiple clients.Now suppose any how I manage to know the address of those client statically(for the sake of simplicity) and now I want to send this packet to these addresses.So how exactly I need to fill the sockaddr structure to contain the address of these clients.I am taking an array of sockaddr structure(to contain client address) and trying to send at each one them at a time.Now problem is to fill the individual sockaddr structure to contain the client address. I was trying to do something like that
sa[1].sin_family = AF_INET;
sa[1].sin_addr.s_addr = htonl(INADDR_ANY);//should'nt I replace this INADDR_ANY with client ip??
sa[1].sin_port = htons(50002);
Correct me if this is not the correct way. All your help in this regard will be highly appr开发者_开发百科eciated.
With Thanks in advance,
Mawia
sin_addr should be set to the destination address.
if (! inet_aton("1.2.3.4", &sa[1].sin_addr)) {
// Give up all hope
}
// Everything is copacetic.
Looks like you're talking about multicast. It is a bit harder than trivial.
Take a look at this thread to discover how to subscribe to a multicast group (for the client side) and how to send multicast packets (for the server side). It is discussed using python, but only low-level wrappers around socket library are used, so it should be quite simple to translate examples to any language.
As mentioned in a different answer, you're talking about multicast, but on the public Internet, that requires ISP support.
There is such a thing as an application level multicast infrastructure. This writeup, dating back to 2000, describes one such method.
精彩评论