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