UDP方式点对点通讯
#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.nr"); 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.nr"); /* 循环接收数据 */ 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] = ''; printf("收到来自%s:%d的消息:%snr", inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff); } return 0; }
客户端源代码如下:
#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.nr"); /* 设置对方地址和端口信息 */ 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("nrsend error.nr"); return 3; } printf("send success.nr"); return 0; }
编译程序用下列命令:
gcc -Wall simple-udpserver.c -o server gcc -Wall simple-udpclient.c -o client
运行程序用下列命令:
./server 127.0.0.1 7838 ./client 127.0.0.1 7838
UDP方式广播通讯
源代码变成下面的:
#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.nr"); /* 设置通讯方式对广播,即本程序发送的一个消息,网络上所有主机均可以收到 */ 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("nrsend error.nr"); return 3; } printf("send success.nr"); return 0; }
编译这个程序用下列命令:
gcc -Wall broadc-udpclient.c -o client
运行程序用下列命令:
./client 192.168.0.255 7838
就会往192.168.0网络内所有主机发消息。
其它主机如果运行了服务端:
./server 自己的IP地址 7838
则都会收到上述客户端发的消息了。