所以我编写了一个多线程的Web服务器,这里是程序的一个功能。此功能将输出文件描述符(fd
),内容类型,指向要提供的数据的指针(*buf
)和数据大小(numbytes
)。它始终卡在5775字节!我试过使用write()
而不是send()
,但无济于事!我试图一次发送整个buf
,甚至试图将它转换成块,但是wget
显示它在5775字节处得到!下面是代码:无法通过TCP连接发送整个文件! (UNIX C)
int return_result(int fd, char *content_type, char *buf, int numbytes)
{
char out_buf[BUF_SIZE], numb[6];
int buf_len, total = 0, buf_size;
long int i = 0;
sprintf(numb, "%d", numbytes);
strcpy(out_buf, "HTTP/1.1 200 OK \nContent-Type: ");
strcat(out_buf, content_type);
strcat(out_buf, "\nContent-Length: ");
strcat(out_buf, numb);
strcat(out_buf, "\nConnection: Close\n \n");
printf("\nSending HTTP Header\n %d bytes sent!",
send(fd, out_buf, strlen(out_buf), 0));
char *start = NULL, *str = NULL, *temp = NULL;
start = buf;
printf("\n Start Pointer Val = %ld", &start);
while (start != NULL) {
printf("\n While Loop");
if (i + 2048 * sizeof(char) < numbytes) {
printf("\n If 1");
str = (char *)malloc(sizeof(char) * 2048);
memcpy(str, start, sizeof(char) * 2048);
i = i + 2048 * sizeof(char);
buf_size = send(fd, str, 2048, 0);
free(str);
printf("\n Sent %d bytes total : %d", buf_size, total =
total + buf_size);
temp = start + sizeof(char) * 2048;
start = temp;
} else {
i = numbytes - i * sizeof(char);
if (i > 0) {
printf("\n If 2");
printf("\n Value of i %d", i);
str = (char *)malloc(sizeof(char) * i);
memcpy(str, start, sizeof(char) * i);
printf("Total bytes finally sent:%d", total =
total + send(fd, str, i, 0));
if (total == numbytes) {
printf("\nTransfer Complete!");
}
free(str);
}
start = NULL;
}
}
printf("out of loop!");
return 0;
}
你实际上得到的“总字节数最后送:...”信息匹配,你期待什么?如果是这样,它可能是一个缓冲/冲洗问题。 – 2011-12-16 01:13:24
是的,事实上我发送()从服务器端的所有字节,但wget只收到5775! – Zombie 2011-12-16 01:16:18