A ftp client, has been written, but the data of receiving LIST commands with socket is incomplete.

receive data in child process:

char data_buffer[BUFFER_SIZE];
char *ptr = "";
int data_len = 0;
int pre_len = 0;
for (;;)
{
    bzero(data_buffer, BUFFER_SIZE);
    int length = recv(client_data_socket, data_buffer, BUFFER_SIZE, 0);
    if (length == 0)
    {
        close(client_data_socket);
        break;
    }
    else if (length < 0)
    {
        if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
        {
            continue;
        }
        close(client_data_socket);
        printf("get data failed\n");
        exit(1);
    }
    pre_len = data_len;
    data_len += length;
    char *tmp_ptr = (char *)calloc(data_len, sizeof(char));
    memcpy(tmp_ptr, ptr, pre_len);
    memcpy(tmp_ptr + pre_len, data_buffer, length);
    if (pre_len > 0) free(ptr);
    ptr = tmp_ptr;
}
char *tmp_ptr = (char *)calloc(data_len + 1, sizeof(char));
g2u(ptr, data_len, tmp_ptr, data_len + 1);
printf("%s\n", tmp_ptr);
free(ptr);
free(tmp_ptr);
exit(0);
The

g2u method transforms the gbk to utf-8 and then finds that part of the read data is missing. When recv returns 0, I judge that all the data is received, but I find that there is something wrong with the result


.
Gcc c
Mar.17,2021

< H1 > Test < / H1 >

printed the bytes received

..... 32 32 -47 -72 -64 -41 -49 -62 -44 -40 13 10 

32 is a space, 13 is\ r 10 is\ n
-47-72-64-41-49-62-44-40 I printed this paragraph with vc, which happens to be downloaded by Xunlei

. < H1 > conclusion < / H1 >

seems to be the problem of g2u function

< hr >

then I looked at it again and magnified the outbuf (changed to 2 times, and the value of outlen should also be changed), so that I could output

correctly.

seems to be the problem of multi-byte corresponding Chinese characters

GBK code, one Chinese character occupies two bytes UTF-8 code is a variable length code, usually Chinese characters occupy three bytes, and the Chinese characters after the extension of B area account for four bytes.
Menu