所以我編寫了一個多線程的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