我正在编写一个HTTP服务器,在发送更大的文件时遇到了困难。如果我用netcat抓到它们,它的输出似乎是完美的。如果我使用浏览器或wget,有时只会得到完整的文件。WGET不断获得“由对等方重置连接”错误,请参见下面的输出。火狐说“连接被重置了”。
以下是向客户端发送数据的相关过程:
int handle_request(int sockfd, struct cached_file content) {
char buffer[1024]; // FIXME hardcoded arbitrary buffer size for header
unsigned int sent_bytes;
unsigned int total = 0;
unsigned int bytes_left = content.size;
printf("I have to send %u bytes of content.\n", content.size);
snprintf(buffer, 1024, "HTTP/1.1 %s\nContent-Type: %s\nContent-Length: %s\n\n", content.statuscode, content.contenttype, content.sizestring);
sent_bytes = send(sockfd, buffer, strlen(buffer), MSG_MORE);
printf("I wanted to send %u bytes of header, and I sent %u.\n", strlen(buffer), sent_bytes);
while (total < bytes_left) {
sent_bytes = send(sockfd, content.data+total, bytes_left, 0);
if (sent_bytes == -1) {
printf("send() returned -1\n");
break;
}
total += sent_bytes;
bytes_left -= sent_bytes;
}
printf("I sent %u bytes of content. I had %u left to send.\n", total, bytes_left);
if (sent_bytes == -1)
logprint("socket error!", errno);
}下面是wget试图抓取文件的输出:
wget --tries 1 http://localhost:8081/image.jpg
--2015-07-01 13:21:42-- http://localhost:8081/image.jpg
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8081... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:8081... connected.
HTTP request sent, awaiting response... 200 OK
Length: 700895 (684K) [image/jpeg]
Saving to: ‘image.jpg.10’
image.jpg.10 53%[===============================> ] 363.31K --.-KB/s in 0.001s
2015-07-01 13:21:42 (688 MB/s) - Read error at byte 372031/700895 (Connection reset by peer). Giving up.
wget --tries 1 http://localhost:8081/image.jpg
--2015-07-01 13:21:43-- http://localhost:8081/image.jpg
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8081... failed: Connection refused.
Connecting to localhost (localhost)|127.0.0.1|:8081... connected.
HTTP request sent, awaiting response... 200 OK
Length: 700895 (684K) [image/jpeg]
Saving to: ‘image.jpg.11’
image.jpg.11 6%[==> ] 42.69K --.-KB/s in 0s
2015-07-01 13:21:43 (500 MB/s) - Read error at byte 43711/700895 (Connection reset by peer). Giving up.调试http服务器的输出:
I have to send 700895 bytes of content.
I wanted to send 65 bytes of header, and I sent 65.
I sent 700895 bytes of content. I had 0 left to send.我很感激能再给我一双眼睛!为什么会发生这种事,我该怎么解决呢?
发布于 2015-07-01 18:18:26
我的猜测是,错误与您在这里没有显示的代码有关。
简单实现中常见的错误是不完全读取请求,而是只读取第一行或一些字节来确定请求的页面,然后发送响应,最后关闭。
由于关闭时仍有来自客户端的未读数据,这将导致对等方重置连接。在nc中,您没有看到这种效果,因为您用nc发送的请求可能比浏览器的请求短,因此在nc情况下会读取来自请求的所有数据,而在浏览器情况下则不会读取。
除此之外,即使浏览器接受响应,您的响应也是无效的。状态行(第一行)在状态代码之后停止,而不是添加原因短语,请参见http://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html#sec6.1。此外,您还使用\n而不是\r\n作为行分隔符。
发布于 2015-07-01 18:04:39
我不知道是否有必要,但是当我检查简单网站发送的头部时,值周围有引号:
Accept-Ranges:"bytes"
Cache-Control:"max-age=25920000"
Connection:"Keep-Alive"
Content-Length:"4777"
Content-Type:"image/gif"
Date:"Wed, 01 Jul 2015 17:55:52 GMT"
Expires:"Tue, 26 Apr 2016 17:55:52 GMT"
Keep-Alive:"timeout=30, max=100"
Last-Modified:"Mon, 28 Apr 1997 01:25:47 GMT"
Server:"Apache/1.3.28 (Unix) mod_gzip/1.3.19.1a mod_perl/1.28"内容类型是否可能不正确?
https://stackoverflow.com/questions/31168131
复制相似问题