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
|
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> int main( int argc, char **argv) { struct sockaddr_in s_addr; struct sockaddr_in c_addr; int sock; socklen_t addr_len; int len; char buff[128]; /* 创建 socket , 关键在于这个 SOCK_DGRAM */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror ( "socket" ); exit ( errno ); } else printf ( "create socket.\n\r" ); memset (&s_addr, 0, sizeof ( struct sockaddr_in)); /* 设置地址和端口信息 */ s_addr.sin_family = AF_INET; if (argv[2]) s_addr.sin_port = htons( atoi (argv[2])); else s_addr.sin_port = htons(7838); if (argv[1]) s_addr.sin_addr.s_addr = inet_addr(argv[1]); else s_addr.sin_addr.s_addr = INADDR_ANY; /* 绑定地址和端口信息 */ if ((bind(sock, ( struct sockaddr *) &s_addr, sizeof (s_addr))) == -1) { perror ( "bind" ); exit ( errno ); } else printf ( "bind address to socket.\n\r" ); /* 循环接收数据 */ addr_len = sizeof (c_addr); while (1) { len = recvfrom(sock, buff, sizeof (buff) - 1, 0, ( struct sockaddr *) &c_addr, &addr_len); if (len < 0) { perror ( "recvfrom" ); exit ( errno ); } buff[len] = '\0' ; printf ( "收到来自%s:%d的消息:%s\n\r" , inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff); } return 0; } |
客户端源代码如下:
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
|
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> int main( int argc, char **argv) { struct sockaddr_in s_addr; int sock; int addr_len; int len; char buff[128]; /* 创建 socket , 关键在于这个 SOCK_DGRAM */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror ( "socket" ); exit ( errno ); } else printf ( "create socket.\n\r" ); /* 设置对方地址和端口信息 */ s_addr.sin_family = AF_INET; if (argv[2]) s_addr.sin_port = htons( atoi (argv[2])); else s_addr.sin_port = htons(7838); if (argv[1]) s_addr.sin_addr.s_addr = inet_addr(argv[1]); else { printf ( "消息必须有一个接收者!\n" ); exit (0); } /* 发送UDP消息 */ addr_len = sizeof (s_addr); strcpy (buff, "hello i'm here" ); len = sendto(sock, buff, strlen (buff), 0, ( struct sockaddr *) &s_addr, addr_len); if (len < 0) { printf ( "\n\rsend error.\n\r" ); return 3; } printf ( "send success.\n\r" ); return 0; } |
编译程序用下列命令:
1
2
|
gcc -Wall simple-udpserver.c -o server gcc -Wall simple-udpclient.c -o client |
运行程序用下列命令:
1
2
3
|
./server 127.0.0.1 7838 ./client 127.0.0.1 7838 |
UDP方式广播通讯
源代码变成下面的:
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
|
#include <stdio.h> #include <string.h> #include <sys/types.h> #include <netinet/in.h> #include <sys/socket.h> #include <errno.h> #include <stdlib.h> #include <arpa/inet.h> int main( int argc, char **argv) { struct sockaddr_in s_addr; int sock; int addr_len; int len; char buff[128]; int yes; /* 创建 socket */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror ( "socket" ); exit ( errno ); } else printf ( "create socket.\n\r" ); /* 设置通讯方式对广播,即本程序发送的一个消息,网络上所有主机均可以收到 */ yes = 1; setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &yes, sizeof (yes)); /* 唯一变化就是这一点了 */ /* 设置对方地址和端口信息 */ s_addr.sin_family = AF_INET; if (argv[2]) s_addr.sin_port = htons( atoi (argv[2])); else s_addr.sin_port = htons(7838); if (argv[1]) s_addr.sin_addr.s_addr = inet_addr(argv[1]); else { printf ( "消息必须有一个接收者!\n" ); exit (0); } /* 发送UDP消息 */ addr_len = sizeof (s_addr); strcpy (buff, "hello i'm here" ); len = sendto(sock, buff, strlen (buff), 0, ( struct sockaddr *) &s_addr, addr_len); if (len < 0) { printf ( "\n\rsend error.\n\r" ); return 3; } printf ( "send success.\n\r" ); return 0; } |
编译这个程序用下列命令:
1
|
gcc -Wall broadc-udpclient.c -o client |
运行程序用下列命令:
1
|
. /client 192.168.0.255 7838 |
就会往192.168.0网络内所有主机发消息。
其它主机如果运行了服务端:
1
|
. /server 自己的IP地址 7838 |
则都会收到上述客户端发的消息了。