博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Linux UDP服务器编程
阅读量:3949 次
发布时间:2019-05-24

本文共 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无连接传输,偶尔传输失败一次继续执行 }

特别注意:

  • sockfd是本地套接字的描述符,在服务器端,就是服务器套接字;在客户端,就是客户端套接字
  • buf是接受数据的缓冲区,len是缓冲区的大小
  • flags一般为0
  • 后面两个参数是用来获取:数据发送端的IP地址和端口号

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; }

特别注意:

  • sockfd是本地套接字的描述符,在发送的时候一般要绑定本地套接字与众所周知的IP地址和端口
  • 参数中的套接字地址就是要发送的目的

示例

server.c

#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;}

client.c

#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/

你可能感兴趣的文章
高效率的危害
查看>>
寻找边缘性创新
查看>>
让创意瞄准市场
查看>>
高效经理人应具有的八个重要习惯
查看>>
优秀的领导者能读懂人才
查看>>
大智若愚也是领导力
查看>>
android如何编译MTK的模拟器
查看>>
android如何添加AP中要使用的第三方JAR文件
查看>>
利用sudo命令为Ubuntu分配管理权限
查看>>
Ubuntu下几个重要apt-get命令用法与加速UBUNTU
查看>>
Ubuntu中网页各种插件安装命令
查看>>
使用tar命令备份Ubuntu系统
查看>>
ubuntu flash 文字乱码解决方案
查看>>
在ubuntu中运行exe文件
查看>>
ubuntu安装命令
查看>>
和上司沟通必备8个黄金句
查看>>
联系查看两张卡的未接电话记录
查看>>
Python 3 之多线程研究
查看>>
APP第三方登录实现步骤
查看>>
KVO & KVC 比较 - KVC
查看>>