Source Code of Server Program
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct in_addr localInterface;
struct sockaddr_in group_socket_address;
int socket_file_descriptor;
char buffer[1024] = "Multicast test message lol!";
int data_length = sizeof(buffer);
int main(int argc, char *argv[]) {
socket_file_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_file_descriptor < 0) {
perror("Opening datagram socket error");
exit(1);
} else
printf("Opening the datagram socket...OK.\n");
memset((char *) &group_socket_address, 0, sizeof(group_socket_address));
group_socket_address.sin_family = AF_INET;
group_socket_address.sin_addr.s_addr = inet_addr("226.1.1.1");
group_socket_address.sin_port = htons(4321);
localInterface.s_addr = inet_addr("203.106.93.94");
if (setsockopt(socket_file_descriptor, IPPROTO_IP, IP_MULTICAST_IF, (char *) &localInterface,
sizeof(localInterface)) < 0)
{
perror("Setting local interface error");
exit(1);
}
else
printf("Setting the local interface...OK\n");
if (sendto(socket_file_descriptor, buffer, data_length, 0, (struct sockaddr*) &group_socket_address,
sizeof(group_socket_address)) < 0)
perror("Sending datagram message error");
else
printf("Sending datagram message...OK\n");
return 0;
}
Source Code of Client Program
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
struct sockaddr_in local_socket_address;
struct ip_mreq group;
int socket_file_descriptor;
int data_length;
char buffer[1024];
int main(int argc, char *argv[])
{
socket_file_descriptor = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_file_descriptor < 0)
{
perror("Opening datagram socket error");
exit(1);
}
else
{
printf("Opening datagram socket....OK.\n");
int reuse = 1;
if (setsockopt(socket_file_descriptor, SOL_SOCKET, SO_REUSEADDR, (char *) &reuse,sizeof(reuse)) < 0)
{
perror("Setting SO_REUSEADDR error");
close(socket_file_descriptor);
exit(1);
}
else
printf("Setting SO_REUSEADDR...OK.\n");
}
memset((char *) &local_socket_address, 0, sizeof(local_socket_address));
local_socket_address.sin_family = AF_INET;
local_socket_address.sin_port = htons(4321);
local_socket_address.sin_addr.s_addr = INADDR_ANY;
if (bind(socket_file_descriptor, (struct sockaddr*) &local_socket_address, sizeof(local_socket_address)))
{
perror("Binding datagram socket error");
close(socket_file_descriptor);
exit(1);
}
else
printf("Binding datagram socket...OK.\n");
/* Join the multicast group 226.1.1.1 on the local 203.106.93.94 */
/* interface. Note that this IP_ADD_MEMBERSHIP option must be */
/* called for each local interface over which the multicast */
/* datagrams are to be received. */
group.imr_multiaddr.s_addr = inet_addr("226.1.1.1");
group.imr_interface.s_addr = inet_addr("203.106.93.94");
if (setsockopt(socket_file_descriptor, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &group, sizeof(group)) < 0)
{
perror("Adding multicast group error");
close(socket_file_descriptor);
exit(1);
}
else
printf("Adding multicast group...OK.\n");
/* Read from the socket. */
data_length = sizeof(buffer);
if (read(socket_file_descriptor, buffer, data_length) < 0)
{
perror("Reading datagram message error");
close(socket_file_descriptor);
exit(1);
}
else
{
printf("Reading datagram message...OK.\n");
printf("The message from multicast server is: \"%s\"\n", buffer);
}
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.