C中从套接字阅读大量数据

qojgxg4l  于 2023-10-16  发布在  其他
关注(0)|答案(2)|浏览(111)

我有一个python tcp服务器,它接受连接并生成一个长度在(0,1M)字符之间的随机字符串,另一方面,我有一个c客户端,它需要监听该套接字并读取字符串,并将其转换为与服务器返回的字符串长度相同的单个字符。

  1. int receiver(int soc_desc, char * buffer)
  2. {
  3. char *arr = (char *)malloc(sizeof(char));
  4. unsigned int received , total_received;
  5. while (1)
  6. {
  7. memset(arr, 0, MAX); // clear the buffer
  8. if ( received = recv(soc_desc, arr , MAX, 0) < 0)
  9. {
  10. break;
  11. }
  12. else
  13. {
  14. total_received += received;
  15. }
  16. }
  17. printf("%s\n",arr);
  18. return received;
  19. }
  20. // soc_desc is the socket descriptor
  21. // buffer is the buffer that will hold the final output

我能想到的唯一方法是使用malloc来读取从服务器返回的数据块,但我在尝试解决这个问题时遇到了困难,当客户端完成从服务器接收数据时,我需要将char指针数组转换为单个字符

e1xvtsh3

e1xvtsh31#

我找到了一种方法来做到这一点,但这是没有足够的测试,肯定会导致内存问题

  1. char *arr = malloc(sizeof(char));
  2. char tmp_buff[MAX];
  3. memset(arr,0,MAX);
  4. while (recv(soc_desc, tmp_buff , MAX, 0) > 0 )
  5. {
  6. strcat(arr , tmp_buff);
  7. printf("Size : %ld arr : %s\n",strlen(tmp_buff),tmp_buff);
  8. }
92vpleto

92vpleto2#

重组网络数据,特别是来自TCP的数据,可能会变得很棘手。下面的代码是未经测试的,当然不能解释所有的意外情况,但希望是沿着正确的道路,你需要做的。

  1. ssize_t receiver(int soc_desc, char * buffer)
  2. {
  3. // Whats the buffer argument used for?
  4. // what you had before only allocated space for 1 char. That's not what you want
  5. // This allocates for MAX+1 chars (I'm doing +1 for a NUL terminator)
  6. char *arr = malloc(MAX+1);
  7. // if MAX is small enough, you could do
  8. // char arr[MAX+1];
  9. // 0 buffer. You could use calloc instead of malloc + memset
  10. memset(arr, 0, MAX+1);
  11. // initialize total_received to 0
  12. ssize_t received , total_received = 0;
  13. size_t spaceLeftInBuf = MAX;
  14. while (1)
  15. {
  16. // don't memset here, you'll erase the data you received last iteration
  17. // write data to arr+total_receieved. This way you won't overwrite what
  18. // you received the last iteration
  19. received = recv(soc_desc, arr+total_received, spaceLeftInBuf, 0);
  20. if (received < 0)
  21. {
  22. // there was an error
  23. perror("recv failed: ");
  24. // do something with the data already received? Ok, break and
  25. // print what we've got
  26. break;
  27. }
  28. else if (received == 0)
  29. {
  30. // socket closed gracefully, suppose we can break again and print
  31. // what we've got
  32. break;
  33. }
  34. else
  35. {
  36. // update counters
  37. total_received += received;
  38. spaceLeftInBuf -= received;
  39. // is our buffer full? This may not be the right check, you need to
  40. // decide when to process the data
  41. // total_received better not ever be > MAX...
  42. if (total_received >= MAX)
  43. {
  44. // "process" the data by printing it
  45. printf("%s\n", arr);
  46. // reset
  47. total_received = 0;
  48. spaceLeftInBuf = MAX;
  49. // not particularly necessary to reset this to all 0s, but should
  50. // make sure printing goes smoothly if we break out of this loop
  51. memset(arr, 0, MAX); // arr[MAX] should already be '\0' from above
  52. }
  53. }
  54. }
  55. printf("%s\n",arr);
  56. free(arr);
  57. return received;
  58. }

参见Do I cast the result of malloc?

展开查看全部

相关问题