簡體   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