繁体   English   中英

从 c 中的套接字读取大量数据

[英]Reading large amount of data from socket in c

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


int receiver(int soc_desc, char * buffer)
{
    char *arr = (char *)malloc(sizeof(char));
    unsigned int received , total_received;
    while (1)
    {
        memset(arr, 0, MAX); // clear the buffer
        if ( received = recv(soc_desc, arr , MAX, 0) < 0)
        {
            break;
        }
        else
        {
            total_received += received;
        }
    }
    printf("%s\n",arr);
    return received; 
}
// soc_desc is the socket descriptor 
// buffer is the buffer that will hold the final output 

我能想到的唯一方法是使用malloc来读取从服务器返回的数据块,但我很难弄清楚它,我需要在客户端时将char指针数组转换为单个 char完成从服务器接收数据

重新组装网络数据,尤其是来自 TCP 的数据,可能会很棘手。 以下代码未经测试,肯定不会考虑所有意外情况,但希望它是您需要做的事情的正确路径。

ssize_t receiver(int soc_desc, char * buffer)
{
    // Whats the buffer argument used for?
    // what you had before only allocated space for 1 char. That's not what you want
    // This allocates for MAX+1 chars (I'm doing +1 for a NUL terminator)
    char *arr = malloc(MAX+1);
    // if MAX is small enough, you could do
    // char arr[MAX+1];

    // 0 buffer. You could use calloc instead of malloc + memset
    memset(arr, 0, MAX+1);
    // initialize total_received to 0
    ssize_t received , total_received = 0;
    size_t spaceLeftInBuf = MAX;
    while (1)
    {
        // don't memset here, you'll erase the data you received last iteration

        // write data to arr+total_receieved. This way you won't overwrite what
        // you received the last iteration
        received = recv(soc_desc, arr+total_received, spaceLeftInBuf, 0);
        if (received < 0)
        {
            // there was an error
            perror("recv failed: ");
            // do something with the data already received? Ok, break and
            // print what we've got
            break;
        }
        else if (received == 0)
        {
            // socket closed gracefully, suppose we can break again and print
            // what we've got
            break;
        else
        {
            // update counters
            total_received += received;
            spaceLeftInBuf -= received;
            // is our buffer full? This may not be the right check, you need to
            // decide when to process the data
            // total_received better not ever be > MAX...
            if (total_received >= MAX)
            {
                // "process" the data by printing it
                printf("%s\n", arr);
                
                // reset
                total_received = 0;
                spaceLeftInBuf = MAX;
                // not particularly necessary to reset this to all 0s, but should
                // make sure printing goes smoothly if we break out of this loop
                memset(arr, 0, MAX);  // arr[MAX] should already be '\0' from above
            }
            
        }
    }
    printf("%s\n",arr);
    return received; 
}

请参阅我是否投射 malloc 的结果?

我找到了一种方法,但这没有经过充分测试,肯定会导致 memory 问题

    char *arr = malloc(sizeof(char));
    char tmp_buff[MAX];
    memset(arr,0,MAX);
    while (recv(soc_desc, tmp_buff , MAX, 0) > 0 )
    {
        strcat(arr , tmp_buff);
        printf("Size : %ld  arr : %s\n",strlen(tmp_buff),tmp_buff);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM