-
Notifications
You must be signed in to change notification settings - Fork 0
/
10-6multicast_s.c
61 lines (51 loc) · 1.29 KB
/
10-6multicast_s.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define N 128
#define errlog(errmsg) do{perror(errmsg);\
printf("%s--%s--%d\n",\
__FILE__, __func__, __LINE__);\
exit(1);\
}while(0)
int main(int argc, const char *argv[])
{
int sockfd;
struct sockaddr_in groupcastaddr;
socklen_t addrlen = sizeof(groupcastaddr);
char buf[N] = {};
if(argc < 3)
{
fprintf(stderr, "Usage: %s ip port\n", argv[0]);
exit(1);
}
//第一步:创建套接字
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
errlog("fail to socket");
}
//第二步:填充组播网络信息结构体
//inet_addr:将点分十进制ip地址转化为网络字节序的整型数据
//htons:将主机字节序转化为网络字节序
//atoi:将数字型字符串转化为整型数据
groupcastaddr.sin_family = AF_INET;
//224.x.x.x - 239.x.x.x
groupcastaddr.sin_addr.s_addr = inet_addr(argv[1]);
groupcastaddr.sin_port = htons(atoi(argv[2]));
while(1)
{
fgets(buf, N, stdin);
buf[strlen(buf) - 1] = '\0';
if(sendto(sockfd, buf, N, 0,\
(struct sockaddr *)&groupcastaddr, addrlen) < 0)
{
errlog("fail to sendto");
}
}
close(sockfd);
return 0;
}