技术解析
接收数据在子进程中:
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);
g2u 方法是编码转换,把 gbk to utf-8 然后发现读出来的数据少了一部分,我是在 recv 返回 0 的时候,判断数据全部接收,可是发现结果有问题 显然,迅雷下载这几个字,没接收完(不知道哪里出了问题) 真正的文件列表