# UDP通信
# server.c
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> int main() { int lfd = socket(AF_INET, SOCK_DGRAM, 0); char *ip_buf = "192.168.248.128"; int ip; inet_pton(AF_INET, ip_buf, &ip); struct sockaddr_in saddr; saddr.sin_addr.s_addr = ip; saddr.sin_family = AF_INET; saddr.sin_port = htons(9999); bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr)); while(1) { char recvBuf[1024]; struct sockaddr_in client_addr; int client_addr_len = sizeof(client_addr); // 第五个参数为传输参数,用于获取发送数据客户端的信息 int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr *) &client_addr , &client_addr_len); if(len == -1) { perror("recvfrom"); exit(-1); } else if(len == 0) { printf("client close······"); exit(-1); } else { printf("recv info : %s\n", recvBuf); } char client_ipBuf[1024]; inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ipBuf, sizeof(client_ipBuf)); printf("client ip : %s, port : %d\n", client_ipBuf, ntohs(client_addr.sin_port)); // 给客户端再回个数据 sendto(lfd, recvBuf, strlen(recvBuf) + 1, 0, (struct sockaddr *) &client_addr, sizeof(client_addr)); } close(lfd); return 0; }
# client.c
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> int main() { int lfd = socket(AF_INET, SOCK_DGRAM, 0); char *ip_buf = "192.168.248.128"; int ip; inet_pton(AF_INET, ip_buf, &ip); struct sockaddr_in saddr; saddr.sin_addr.s_addr = ip; saddr.sin_family = AF_INET; saddr.sin_port = htons(9999); int num = 0; while(1) { char sendBuf[1024]; sprintf(sendBuf, "hello, i am client %d\n", num++); sendto(lfd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *) &saddr, sizeof(saddr)); char recvBuf[1024]; // 服务端的地址已经指定,不需要再获取了,置NULL即可 int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, NULL, NULL); if(len == -1) { perror("recvfrom"); exit(-1); } else if(len == 0) { printf("server close······"); exit(-1); } else { printf("recv info : %s\n", recvBuf); } sleep(1); } close(lfd); return 0; }
# 广播
# server.c
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> int main() { int lfd = socket(AF_INET, SOCK_DGRAM, 0); // 设置广播属性 int opt = 1; setsockopt(lfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)); // 创建一个广播地址 char *ip_buf = "192.168.248.255"; int ip; inet_pton(AF_INET, ip_buf, &ip); struct sockaddr_in saddr; saddr.sin_addr.s_addr = ip; saddr.sin_family = AF_INET; saddr.sin_port = htons(9999); int num = 0; while(1) { char sendBuf[1024]; sprintf(sendBuf, "client...... %d\n", num++); sendto(lfd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *) &saddr, sizeof(saddr)); printf("广播数据:%s\n", sendBuf); sleep(1); } close(lfd); return 0; }
# client.c
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> int main() { int lfd = socket(AF_INET, SOCK_DGRAM, 0); char *ip_buf = "192.168.248.128"; int ip; inet_pton(AF_INET, ip_buf, &ip); struct sockaddr_in saddr; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_family = AF_INET; saddr.sin_port = htons(9999); bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr)); // 谁先接收谁bind while(1) { char recvBuf[1024]; struct sockaddr_in client_addr; int client_addr_len = sizeof(client_addr); // 第五个参数为传输参数,用于获取发送数据客户端的信息 int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, NULL , NULL); if(len == -1) { perror("recvfrom"); exit(-1); } else if(len == 0) { printf("client close······"); exit(-1); } else { printf("recv info : %s\n", recvBuf); } // char client_ipBuf[1024]; // inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ipBuf, sizeof(client_ipBuf)); // printf("client ip : %s, port : %d\n", client_ipBuf, ntohs(client_addr.sin_port)); } close(lfd); return 0; }
# 组播
# server.c
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> int main() { int lfd = socket(AF_INET, SOCK_DGRAM, 0); // 设置多播地址 struct in_addr opt; inet_pton(AF_IENT, "239.0.0.10", &opt.s_addr); setsockopt(lfd, IPPROTO_IP, IP_MULTICAST_IF, &opt, sizeof(opt)); // 创建一个多播地址 char *ip_buf = "239.0.0.10"; int ip; inet_pton(AF_INET, ip_buf, &ip); struct sockaddr_in saddr; saddr.sin_addr.s_addr = ip; saddr.sin_family = AF_INET; saddr.sin_port = htons(9999); int num = 0; while(1) { char sendBuf[1024]; sprintf(sendBuf, "client...... %d\n", num++); sendto(lfd, sendBuf, strlen(sendBuf) + 1, 0, (struct sockaddr *) &saddr, sizeof(saddr)); printf("广播数据:%s\n", sendBuf); sleep(1); } close(lfd); return 0; }
# client.c
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> int main() { int lfd = socket(AF_INET, SOCK_DGRAM, 0); char *ip_buf = "192.168.248.128"; int ip; inet_pton(AF_INET, ip_buf, &ip); struct sockaddr_in saddr; saddr.sin_addr.s_addr = INADDR_ANY; saddr.sin_family = AF_INET; saddr.sin_port = htons(9999); bind(lfd, (struct sockaddr *) &saddr, sizeof(saddr)); // 谁先接收谁bind // 加入多播组 struct ip_mreq opt; inet_pton(AF_INET, "239.0.0.10", &opt.imr_multiaddr); opt.imr_interface = INADDR_ANY; setsockopt(lfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, ) while(1) { char recvBuf[1024]; struct sockaddr_in client_addr; int client_addr_len = sizeof(client_addr); // 第五个参数为传输参数,用于获取发送数据客户端的信息 int len = recvfrom(lfd, recvBuf, sizeof(recvBuf), 0, NULL , NULL); if(len == -1) { perror("recvfrom"); exit(-1); } else if(len == 0) { printf("client close······"); exit(-1); } else { printf("recv info : %s\n", recvBuf); } // char client_ipBuf[1024]; // inet_ntop(AF_INET, &client_addr.sin_addr.s_addr, client_ipBuf, sizeof(client_ipBuf)); // printf("client ip : %s, port : %d\n", client_ipBuf, ntohs(client_addr.sin_port)); } close(lfd); return 0; }
标签:UDP,组播,addr,int,ip,广播,client,saddr,include From: https://www.cnblogs.com/WTSRUVF/p/17402658.html