【Linux】UDP通信

[复制链接]
发表于 2024-12-22 01:48:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
udp使用的是数据报传输。可以一对一,一对多举行传输,用于快速,实时性高的场景
服务器端:
使用步骤:
1.创建socket
2.bind绑定可吸取的客户端
3.while{
recv吸取数据
send发送数据
}
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>
  9. int main()
  10. {
  11.     // 创建socket
  12.     int cfd = socket(AF_INET, SOCK_DGRAM, 0);
  13.     if (cfd < 0)
  14.     {
  15.         perror("socket error");
  16.         return -1;
  17.     }
  18.     // 绑定
  19.     struct sockaddr_in serv;
  20.     struct sockaddr_in clientsock;
  21.     bzero(&serv, sizeof(serv));
  22.     serv.sin_family = AF_INET;
  23.     serv.sin_port = htons(9999);
  24.     serv.sin_addr.s_addr = htonl(INADDR_ANY);
  25.     bind(cfd, (struct sockaddr *)&serv, sizeof(serv));
  26.     int i;
  27.     int n;
  28.     socklen_t len;
  29.     char buf[1024];
  30.     while (1)
  31.     {
  32.         // 读取数据
  33.         memset(buf, 0x00, sizeof(buf));
  34.         len = sizeof(clientsock);
  35.         n = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&clientsock, &len);
  36.         // 将大写转换为小写
  37.         for (i = 0; i < n; i++)
  38.         {
  39.             buf[i] = toupper(buf[i]);
  40.         }
  41.         char sIP[16];
  42.         memset(sIP, 0x00, sizeof(sIP));
  43.         printf("ip:[%s] port:[%d] n:[%d], buf:[%s]\n", inet_ntop(AF_INET, &clientsock.sin_addr.s_addr, sIP, sizeof(sIP)), ntohs(clientsock.sin_port), n, buf);
  44.         // 发送数据
  45.         sendto(cfd, buf, n, 0, (struct sockaddr *)&clientsock, len);
  46.     }
  47.     // 关闭套接字
  48.     close(cfd);
  49.     return 0;
  50. }
复制代码
客户端:
使用步骤:
1.创建socket
2.while{
send发送数据
担当数据
}
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <ctype.h>
  6. #include <sys/types.h>
  7. #include <arpa/inet.h>
  8. #include <netinet/in.h>
  9. int main()
  10. {
  11.         //创建socket
  12.         int cfd = socket(AF_INET, SOCK_DGRAM, 0);
  13.         if(cfd<0)
  14.         {
  15.                 perror("socket error");
  16.                 return -1;
  17.         }
  18.         int n;
  19.         char buf[1024];
  20.         struct sockaddr_in serv;
  21.         serv.sin_family = AF_INET;
  22.         serv.sin_port = htons(9999);
  23.         inet_pton(AF_INET, "192.168.3.99", &serv.sin_addr.s_addr);
  24.         while(1)
  25.         {
  26.                 //读标准输入数据
  27.                 memset(buf, 0x00, sizeof(buf));
  28.                 n = read(STDIN_FILENO, buf, sizeof(buf));
  29.                 //发送数据
  30.                 sendto(cfd, buf, n, 0, (struct sockaddr *)&serv, sizeof(serv));
  31.                 //读取数据
  32.                 memset(buf, 0x00, sizeof(buf));
  33.                 n = recvfrom(cfd, buf, sizeof(buf), 0, NULL, NULL);
  34.                 printf("n:[%d], buf:[%s]\n", n, buf);
  35.         }
  36.         //关闭套接字
  37.         close(cfd);
  38.         return 0;
  39. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

×
登录参与点评抽奖,加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表