Multicast Sockets - Programming Tips
Here are a few tips on working with multicast sockets and UNIX (FreeBSD).
-
Sending socket.
In general, there's nothing special you need to do on the sending end. The
key is simply to send to a multicast IP (group) address. Tips:
- Use socket() with AF_INET and SOCK_DGRAM arguments as normal.
- Use bind() to associate this socket with a local address and port.
- Do not attempt to associate the socket with a multicast destination address
using connect().
- Use sendto() for sending data.
-
Receiving socket.
Receiving is nearly the same, but with one additional system call: setsockopt().
- Use socket() with AF_INET and SOCK_DGRAM arguments as normal.
- Use setsockopt() with the IP_ADD_MEMBERSHIP option. This tells the
system to receive packets on the network whose destination is the group
address (but not its own). Here's an example in C++:
struct sockaddr_in sockaddr_group;
struct hostent *group;
struct ip_mreq mreq;
bzero(&mreq,sizeof(struct ip_mreq));
// set group
if ((group = gethostbyname(ghost))==(struct hostent *)0) {
cerr << "gethostbyname error: fails for host " << host << endl;
exit(-1);
}
struct in_addr ia;
bcopy((void*)group->h_addr,
(void*)&ia,
group->h_length);
bcopy(&ia,
&mreq.imr_multiaddr.s_addr,
sizeof(struct in_addr));
// set interface
mreq.imr_interface.s_addr = htonl(INADDR_ANY);
// do membership call
if (setsockopt(sockfd,
IPPROTO_IP,
IP_ADD_MEMBERSHIP,
&mreq,
sizeof(struct ip_mreq))
== -1) {
cerr << "error: setsockopt(IP_ADD_MEMBERSHIP) fails with errno "
<< errno << endl;
exit(-1);
}
- Use bind() to associate this socket with a local address and port.
- Do not attempt to associate the socket with a multicast destination address
using connect().
- Use recvfrom() for receiving data.
-
Other multicast socket options. A list of additional options is below.
IP_MULTICAST_IF /* u_char; set/get IP multicast i/f */
IP_MULTICAST_TTL /* u_char; set/get IP multicast ttl */
IP_MULTICAST_LOOP /* u_char; set/get IP multicast loopback */
IP_ADD_MEMBERSHIP /* ip_mreq; add an IP group membership */
IP_DROP_MEMBERSHIP /* ip_mreq; drop an IP group membership */
Use IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP to switch multicast groups
"listened" for by the recieving socket. Use IP_MULTICAST_LOOP if you would like the
sender to also receive a copy of what is sent to the group. I don't believe
IP_MULTICAST_IF or IP_MULTICAST_TTL will be of interest for what we're doing.
-
Further reading.
- See the following include file for type definitions:
/usr/include/netinet/in.h
- For more about multicast socket programming, try UNIX Network Programming,
Second Edition, by W. Richard Stevens (Prentice Hall, 1998). In particular,
see Chapter 19 on "Multicasting".
阅读(1431) | 评论(0) | 转发(0) |