• server和client之间进行Socket通信,进行数据切片


    参考链接

    注意事项

    • 代码很low,主要看封装的Send函数所体现的切片思想即可

    server代码

    1. //udp服务端
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. #include
    12. #define SLICE_SIZE 5
    13. bool Send(int sock_fd, char* buffer, int length)
    14. {
    15. int once; //一次发送的返回值
    16. int total = 0; //目前发送了多少数据
    17. int thislen = 0; //本次要发送的数据长度
    18. do
    19. {
    20. if (length - total > SLICE_SIZE)
    21. thislen = SLICE_SIZE;
    22. else
    23. thislen = length - total;
    24. once = send(sock_fd, buffer + total, thislen, 0);
    25. if (once <= 0) //如果once < 0则表示发送失败,用break跳出循环,返回FALSE
    26. {
    27. break;
    28. }
    29. total += once;
    30. } while (total < length); //如果total < length说明数据还未完全发送,继续循环
    31. if (total == length) //当total == length时,说明已完全发送,返回TRUE
    32. return true;
    33. return false;
    34. }
    35. int main(int argc, char const *argv[])
    36. {
    37. //1.创建socket
    38. int cfd = socket(AF_INET,SOCK_DGRAM,0);
    39. if(cfd<0){
    40. perror("socket error");
    41. return -1;
    42. }
    43. //绑定
    44. struct sockaddr_in serv;
    45. struct sockaddr_in client;
    46. bzero(&serv,sizeof(serv));
    47. serv.sin_family = AF_INET;
    48. serv.sin_port = htons(7777);
    49. serv.sin_addr.s_addr = htonl(INADDR_ANY);
    50. bind(cfd,(struct sockaddr *)&serv,sizeof(serv));
    51. //3.循环读取读取客户端消息和给客户端回复消息
    52. int i;
    53. int n;
    54. socklen_t len;
    55. char buf[1024];
    56. std::string merge;
    57. while(1){
    58. //4.读取数据
    59. memset(buf,0x00,sizeof(buf));
    60. len = sizeof(client);
    61. n = recvfrom(cfd,buf,sizeof(buf),0,(struct sockaddr*)&client,&len);
    62. merge += buf;
    63. printf("serv端口号=[%d]:本次发送字节数=[%d],buf=[%s]\n",ntohs(client.sin_port),n,buf);
    64. // printf("serv端口号=[%d]:本次发送字节数=[%d],bufer=[%s]\n",ntohs(serv.sin_port),once,tmp);
    65. std::cout << "merge= " << merge << ";" << "merge size is " << merge.size() << std::endl;
    66. //5.给客户端回复消息
    67. sendto(cfd,buf,n,0,(struct sockaddr*)&client,len);
    68. }
    69. //关闭套接字
    70. close(cfd);
    71. return 0;
    72. }

    client代码

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #define SLICE_SIZE 5
    10. char* buffer = {"Helloworld\n"};
    11. char* tmp[1024] ={0};
    12. struct sockaddr_in serv;
    13. bool Send(int sock_fd, char* buffer, int length)
    14. {
    15. int once; //一次发送的返回值
    16. int total = 0; //目前发送了多少数据
    17. int thislen = 0; //本次要发送的数据长度
    18. do
    19. {
    20. if (length - total > SLICE_SIZE)
    21. thislen = SLICE_SIZE;
    22. else
    23. thislen = length - total;
    24. once = send(sock_fd, buffer + total, thislen, 0);
    25. // printf("serv端口号=[%d]:本次发送字节数=[%d],bufer=[%s]\n",ntohs(serv.sin_port),once,tmp);
    26. // sendto(cfd,buf,n,0,(struct sockaddr*)&serv,sizeof(serv));
    27. if (once <= 0) //如果once < 0则表示发送失败,用break跳出循环,返回FALSE
    28. {
    29. break;
    30. }
    31. total += once;
    32. } while (total < length); //如果total < length说明数据还未完全发送,继续循环
    33. if (total == length) //当total == length时,说明已完全发送,返回TRUE
    34. return true;
    35. return false;
    36. }
    37. int main(){
    38. //1.创建socket
    39. int cfd = socket(AF_INET,SOCK_DGRAM,0);
    40. if(cfd<0){
    41. perror("socket error");
    42. return -1;
    43. }
    44. int n ;
    45. serv.sin_family = AF_INET;
    46. serv.sin_port = htons(7777);
    47. inet_pton(AF_INET,"127.0.0.1",&serv.sin_addr.s_addr);
    48. struct sockaddr_in client;
    49. client.sin_family = AF_INET;
    50. client.sin_port = htons(3333);
    51. bind(cfd, (struct sockaddr*)&client, sizeof(client));
    52. connect(cfd, (struct sockaddr*)&serv, sizeof(serv));
    53. char buf[1024] = {'\0'};
    54. // while(1){
    55. //读取标准输入数据
    56. // memset(buf,0x00,sizeof(buf));
    57. // n = read(STDIN_FILENO,buf, sizeof(buf));
    58. // buf[n-1] = 0x00;
    59. // n -= 1;
    60. Send(cfd,buffer, strlen(buffer));
    61. //发送数据
    62. // sendto(cfd,buf,n,0,(struct sockaddr*)&serv,sizeof(serv));
    63. //读取数据
    64. memset(buf,0x00,sizeof(buf));
    65. n = recvfrom(cfd,buf,sizeof(buf),0,NULL,NULL);
    66. // printf("[%d]:n=[%d],buf=[%s]\n",ntohs(serv.sin_port),n,buf);
    67. // }
    68. //关闭套接字
    69. close(cfd);
    70. return 0;
    71. }

  • 相关阅读:
    2022年第一季度保险服务数字化跟踪分析
    [数据可视化] 词云(Word Cloud)
    python电影数据可视化分析系统的设计与实现【附源码】
    OpenAI开发系列(二):大语言模型发展史及Transformer架构详解
    定时执行专家 —— 定时循环发送UDP消息(例如:控制远程电脑的开机、关机、重启、打开和关闭程序等)
    uniapp使用nfc功能及详解
    Water 2.5.8 发布,一站式服务治理平台
    java并发编程学习二——synchronized
    2022最新Java后端面试题(带答案),重点都给画出来了!你不看?
    【蓝桥杯软件赛 零基础备赛20周】第3周——填空题
  • 原文地址:https://blog.csdn.net/CHYabc123456hh/article/details/126712161