导航

Linux下多播的客户端和服务端程序

Posted on 2017-02-09 14:22  red_w  阅读(141)  评论(0)    收藏  举报

借见网上的一些程序,整理后放在这里,便于以后查找

客户端程序:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<unistd.h>
 5 #include<fcntl.h>
 6 #include<stdarg.h>
 7 #include<string.h>
 8 #include<netdb.h>
 9 #include<errno.h>
10 #include<sys/stat.h>
11 #include<pthread.h>
12 #include<syslog.h>
13 #include<signal.h>
14 #include<ifaddrs.h>
15 #include<netinet/in.h>
16 #include<arpa/inet.h>
17 
18 char *host_name = "224.0.0.1";
19 int port = 6789;
20 int main(int argc, char **argv)
21 {
22     int socketfd;
23     struct sockaddr_in sin;
24     struct ip_mreq command;
25     int loop = 1, iter = 0, sin_len;
26     struct hostent *server_host_name;
27     char message[256];
28 
29     if ((server_host_name = gethostbyname(host_name)) == 0)
30     {
31         perror("gethostbyname");
32         exit(EXIT_FAILURE);
33     }
34 
35     socketfd = socket(AF_INET, SOCK_DGRAM, 0);
36     if (-1 == socketfd)
37     {
38         perror("Opening socket");
39         exit(EXIT_FAILURE);
40     }
41 
42     memset(&sin, 0, sizeof(sin));
43     sin.sin_family = AF_INET;
44     sin.sin_addr.s_addr = htonl(INADDR_ANY);
45     sin.sin_port = htons(port);
46 
47     loop = 1;
48     if (setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
49     {
50         perror("setsockopt: SO_REUSEADDR");
51         exit(EXIT_FAILURE);
52     }
53 
54     if (bind(socketfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) 
55     {
56         perror("bind");
57         exit(EXIT_FAILURE);
58     }
59 
60     loop = 1;
61     if (setsockopt(socketfd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0)
62     {
63         perror("setsockopt: IP_MULTICAST_LOOP");
64         exit(EXIT_FAILURE);
65     }
66 
67     command.imr_multiaddr.s_addr = inet_addr("224.0.0.1");
68     command.imr_interface.s_addr = htonl(INADDR_ANY);
69     if (command.imr_multiaddr.s_addr == -1)
70     {
71         perror("224.0.0.1 not a legal multicast address.");
72         exit(EXIT_FAILURE);
73     }
74 
75     if (setsockopt(socketfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0)
76     {
77         perror("setsockopt: IP ADD_MEMBERSHIP");
78     }
79 
80     while(iter < 8)
81     {
82         sin_len = sizeof(sin);
83         if (recvfrom(socketfd, message, 256, 0, (struct sockaddr *)&sin, &sin_len) == -1)
84         {
85             perror("recvfrom");
86         }
87 
88         printf("Response #%-2d from server: %s\n", iter++, message);
89         sleep(2);
90     }
91 
92     if (setsockopt(socketfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &command, sizeof(command)) < 0)
93     {
94         perror("setsockopt: IP_DROP_MEMBERSHIP");
95     }
96 
97     close(socketfd);
98     exit(EXIT_SUCCESS);
99 }

服务端程序:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 #include<unistd.h>
 5 #include<fcntl.h>
 6 #include<stdarg.h>
 7 #include<string.h>
 8 #include<netdb.h>
 9 #include<errno.h>
10 #include<sys/stat.h>
11 #include<pthread.h>
12 #include<syslog.h>
13 #include<signal.h>
14 #include<ifaddrs.h>
15 #include<netinet/in.h>
16 #include<arpa/inet.h>
17 
18 int port = 6789;
19 
20 int main(int argc, char **argv)
21 {
22     int socketfd;
23     struct sockaddr_in address;
24 
25     socketfd = socket(AF_INET, SOCK_DGRAM, 0);
26     if (-1 == socketfd)
27     {
28         perror("Opening socket");
29         exit(EXIT_FAILURE);
30     }
31 
32     memset(&address, 0, sizeof(address));
33     address.sin_family = AF_INET;
34     address.sin_addr.s_addr = inet_addr("224.0.0.1");
35     address.sin_port = htons(port);
36 
37     //starting Mcast transmit...
38     while(1)
39     {
40         if (sendto(socketfd, "test from broadcast", sizeof("test from broadcast"), 0,(struct sockaddr *)&address, sizeof(address)) < 0)
41         {
42             perror("sendto");
43             exit(EXIT_FAILURE);
44         }
45         sleep(1);
46     }
47     exit(EXIT_SUCCESS);
48 }