TCP/IP Programming Manual
/* Port number */
portNum = atoi (argv[argNum++]); /* convert string to PORT # */
printf (" PortNum: %i\n", portNum);
/* Name of this host */
thishost = argv[argNum++];
if ((temp = gethostbyname (thishost)) != (struct hostent*)NULL) {
memmove ((char *)&in_addr_this.s_addr, (char *)temp->h_addr,
(size_t)temp->h_length);
}
else {
printf ("gethostbyname failed for %s, error = %d\n", thishost, h_errno);
exit (0);
}
thisaddr = in_addr_this.s_addr;
thisip = inet_ntoa (in_addr_this);
printf ("Multicast Interface IP: %s\n", thisip);
/* IP address of the multicast group to join */
multiip = argv[argNum++];
multiaddr0 = inet_addr (multiip); /* convert to binary format */
/* Multicast TTL */
ttlset = atoi(argv[argNum++]);
printf ("Multicast TTL: %i\n",ttlset);
/* Protocol is UDP */
udproto = getprotobyname ("UDP");
/* Create socket */
if ((fd1 = socket (AF_INET, SOCK_DGRAM, udproto->p_proto)) < 0){
perror ("Socket Failure");
exit (0);
}
/* Test Multicast I/F set and get */
printf ("SETting Multicast I/F to %s or 0x%lx \n", thisip, thisaddr);
if (setsockopt (fd1, IPPROTO_IP, IP_MULTICAST_IF,
(char *)&in_addr_this, sizeof(in_addr_this))) {
perror ("SET MULTI IF error");
exit (0);
}
if (getsockopt (fd1, IPPROTO_IP, IP_MULTICAST_IF,
(char *)&in_addr_gmulti, &getsize)) {
perror ("GET MULTI IF error");
exit (0);
}
printf ("GET Multicast I/F: %s, size: %d\n", inet_ntoa(in_addr_gmulti),
getsize);
/* Set multicast TTL */
ttlget = 0;
printf ("SETting TTL to %d\n",ttlset);
if (setsockopt (fd1, IPPROTO_IP, IP_MULTICAST_TTL,
(char *)&ttlset, sizeof(ttlset)))
perror("SET MULTI TTL error");
if (getsockopt (fd1, IPPROTO_IP, IP_MULTICAST_TTL,
Programs Using AF_INET Sockets 229