本文共 3890 字,大约阅读时间需要 12 分钟。
UDP主要使用sendto()
和recvfrom()
recvfrom()
函数原型如下:
#include#include ssize_t recv(int sockfd, void *buf, size_t len, int flags);ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen);if (recvfrom(sockfd_s, buf, BUFFERSIZE, 0, (struct sockaddr*)&client_addr, &addrlen) == -1) { perror("recvfrom"); continue; //UDP无连接传输,偶尔传输失败一次继续执行 }
特别注意:
sendto()
函数原型如下:
#include#include ssize_t send(int sockfd, const void *buf, size_t len, int flags);ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen);if (sendto(sockfd_c, buf, strlen(buf)+1, 0, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("sendto"); continue; }
特别注意:
#include#include #include #include #include #include #include #include #include #include #include #include #include #include #define SERVER_IP "172.17.44.154"#define SERVER_PORT 5002#define BUFFERSIZE 100int main(int argc, const char *argv[]){ int sockfd_s; struct sockaddr_in server_addr, client_addr; char buf[BUFFERSIZE]; socklen_t addrlen; /* 创建套接字 */ if ((sockfd_s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } /* 绑定服务器套接字 */ bzero(&server_addr, sizeof(server_addr)); server_addr.sin_port = htons(SERVER_PORT); server_addr.sin_family = AF_INET; if (inet_pton(AF_INET, SERVER_IP, (void*)&server_addr.sin_addr.s_addr) != 1) { perror("inet_pton"); exit(-1); } if (bind(sockfd_s, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0) { perror("bind"); exit(-1); } while (1) { bzero(buf, BUFFERSIZE); if (recvfrom(sockfd_s, buf, BUFFERSIZE, 0, (struct sockaddr*)&client_addr, &addrlen) == -1) { perror("recvfrom"); continue; //UDP无连接传输,偶尔传输失败一次继续执行 } else { //成功收到 if (strcmp(buf, "quit") == 0) { printf("the client %s/%d is quit\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); exit(0); } printf("recv from %s/%d data:%s\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), buf); sendto(sockfd_s, buf, strlen(buf)+1, 0, (struct sockaddr*)&client_addr, (socklen_t)sizeof(client_addr)); //retroreflection } } return 0;}
#include#include #include #include #include #include #include #include #include #include #include #include #include #include #define SERVER_IP "172.17.44.154"#define SERVER_PORT 5002#define BUFFERSIZE 100int main(int argc, const char *argv[]){ int sockfd_c; struct sockaddr_in server_addr; char buf[BUFFERSIZE]; //512 socklen_t addrlen; /* 创建套接字 */ if ((sockfd_c = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); exit(-1); } bzero(&server_addr, sizeof(server_addr)); server_addr.sin_port = htons(SERVER_PORT); server_addr.sin_family = AF_INET; if (-1 == inet_pton(AF_INET, SERVER_IP, (void*)&server_addr.sin_addr.s_addr)) { perror("inet_pton"); exit(-1); } while (1) { bzero(buf, BUFFERSIZE); printf("input:"); fgets(buf, BUFFERSIZE-1, stdin); if (sendto(sockfd_c, buf, strlen(buf)+1, 0, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("sendto"); continue; } if (strcmp(buf, "quit\n") == 0) { printf("the client will quit\n"); exit(0); } bzero(buf, BUFFERSIZE); // bzero(&server_addr, sizeof(server_addr)); if (-1 == recvfrom(sockfd_c, buf, BUFFERSIZE - 1, 0, (struct sockaddr*)&server_addr, &addrlen)) { perror("recvfrom"); continue; } else { printf("recv from %s/%d data:%s\n", inet_ntoa(server_addr.sin_addr), ntohs(server_addr.sin_port), buf); } } return 0;}
转载地址:http://zxwzi.baihongyu.com/