距离详解Linux下的UDP方式通讯

时间:2021-05-19

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];if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {perror("socket");exit(errno);} elseprintf("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]));elses_addr.sin_port = htons(7838);if (argv[1])s_addr.sin_addr.s_addr = inet_addr(argv[1]);elses_addr.sin_addr.s_addr = INADDR_ANY;if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) {perror("bind");exit(errno);} elseprintf("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;}

客户端源代码如下:

#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];if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {perror("socket");exit(errno);} elseprintf("create socket.\n\r");s_addr.sin_family = AF_INET;if (argv[2])s_addr.sin_port = htons(atoi(argv[2]));elses_addr.sin_port = htons(7838);if (argv[1])s_addr.sin_addr.s_addr = inet_addr(argv[1]);else {printf("消息必须有一个接收者!\n");exit(0);}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;}

编译程序用下列命令:

gcc -Wall simple-udpserver.c -o servergcc -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;if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {perror("socket");exit(errno);} elseprintf("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]));elses_addr.sin_port = htons(7838);if (argv[1])s_addr.sin_addr.s_addr = inet_addr(argv[1]);else {printf("消息必须有一个接收者!\n");exit(0);}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;}

编译这个程序用下列命令:

gcc -Wall broadc-udpclient.c -o client


运行程序用下列命令:

./client 192.168.0.255 7838


就会往192.168.0网络内所有主机发消息。

其它主机如果运行了服务端:

./server 自己的IP地址 7838


则都会收到上述客户端发的消息了。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章