下面的文章内容,要给大家带来的就是简单的udp通信实例,有对这方面感兴趣的小伙伴吗?可以通过下面的文章来进行了解哦。
1、UDP client端(非阻塞)
#include <stdint.h> #include <string.h> #include <stdbool.h> #include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <stdlib.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <net/if.h> #include <fcntl.h> # define SendPort 12346# define TargetIp "192.168.1.1" void Perror(const char * s) { perror(s); exit(EXIT_FAILURE); } //设置非阻塞 static void setnonblocking(int sockfd) { int flag = fcntl(sockfd, F_GETFL, 0); if (flag < 0) { Perror("fcntl F_GETFL fail"); return; } if (fcntl(sockfd, F_SETFL, flag | O_NONBLOCK) < 0) { Perror("fcntl F_SETFL fail"); } } static int udp_send_msg() { struct sockaddr_in addr = {}; int sock; char buff[1024] = "hello server"; char recvbuff[1024] = { 0 }; if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(1); } setnonblocking(sock); //设置非阻塞 bzero( & addr, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_port = htons(SendPort); addr.sin_addr.s_addr = inet_addr(TargetIp); if (addr.sin_addr.s_addr == INADDR_NONE) { printf("Incorrect ip address!"); close(sock); exit(1); } int addr_len = sizeof(addr); int n; int i = 0; n = sendto(sock, buff, sizeof(buff), 0, (struct sockaddr * ) & addr, sizeof(addr)); if (n < 0) { perror("sendto"); close(sock); } printf("This is a UDP client send %d\n", n); memset(recvbuff, 0, sizeof(recvbuff)); while (true) { usleep(100000); //100ms读一次recbuff i = i + 1; n = recvfrom(sock, recvbuff, sizeof(recvbuff), 0, (struct sockaddr * ) & addr, & addr_len); if (i > 10) break; //一秒内未收到数据退出 if (n == -1) continue; else break; } recvbuff[n] = '\0'; printf("This is a UDP client 3 recv strlen :%d, %s\n", n, recvbuff); close(sock); return 0; } int main() { udp_send_msg(); return 0; }
2、UDP server端(非阻塞)
#include <stdint.h> #include <string.h> #include <stdbool.h> #include <pthread.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <unistd.h> #include <stdlib.h> #include <arpa/inet.h> #include <sys/ioctl.h> #include <net/if.h> #include <fcntl.h> # define RecvPort 12346 void Perror(const char * s) { perror(s); exit(EXIT_FAILURE); } //设置非阻塞 static void setnonblocking(int sockfd) { int flag = fcntl(sockfd, F_GETFL, 0); if (flag < 0) { Perror("fcntl F_GETFL fail"); return; } if (fcntl(sockfd, F_SETFL, flag | O_NONBLOCK) < 0) { Perror("fcntl F_SETFL fail"); } } static void * UDPServer_thread() { int sockfd = socket(AF_INET, SOCK_DGRAM, 0); if (0 > sockfd) { perror("sockfd"); return -1; } setnonblocking(sockfd); //准备地址 struct sockaddr_in addr = {}; addr.sin_family = AF_INET; //ipv4 addr.sin_port = htons(RecvPort); //端口号 addr.sin_addr.s_addr = htonl(INADDR_ANY); //我的ip地址 //绑定 int ret = bind(sockfd, (struct sockaddr_in * ) & addr, sizeof(addr)); if (0 > ret) { perror("bind"); return -1; } struct sockaddr_in src_addr = {}; socklen_t addr_len = sizeof(struct sockaddr_in); printf("********UDPServer start********\n"); while (1) { int n; char buff[1024] = { 0 }; char msg[1024] = "hello client"; //接收数据和来源的ip地址 n = recvfrom(sockfd, buff, sizeof(buff), 0, (struct sockaddr_in * ) & src_addr, & addr_len); if (n == -1) continue; buff[n] = '\0'; //memcpy(msg,&buf[10],n-10); printf("UDP server revc %d,:%s\n", n, buff); n = sendto(sockfd, msg, sizeof(msg), 0, (struct sockaddr * ) & src_addr, sizeof(src_addr)); if (n < 0) { perror("sendto"); close(sock); } printf("This is a UDP SERVER send %d\n", n); //message_handler(msg); } printf("********UDPServer end********\n"); close(sockfd); } void Start_UDPServer_Thread(void) { pthread_t thread; pthread_create( & thread, NULL, UDPServer_thread, NULL); } int main() { while (1) { Start_UDPServer_Thread(); } return 0; }
上面的例子你都清楚了吗?希望对你的编程之路可以有所帮助,更多相关实例,请继续关注奇Q工具网的java实例栏目来进行了解吧。
推荐阅读: