我正在编写一个程序,将Linux上的单个JPEG图像从相机流式传输到本地运行的Firefox副本。使用wget和Chrome可以正常加载JPEG,但Firefox只加载了第一个64k(通过Firebug确认)。如果我将传输分解为64000字节块,并且在块之间增加了250ms的延迟,我终于让Firefox显示整个图像。这当然会延迟传输,我试图实际获得每秒至少2-5帧的吞吐量。
为什么Firefox需要在块之间延迟?为什么Firefox需要将图像拆分为<64k块开始?
Chrome或wget都不需要执行任何步骤。通常,图像是&lt; 128K。
以下是相关代码块:
int bytes = jpeg_size[out_buffer];
int start = 0;
buffer_active = true;
sprintf(buffer, "HTTP/1.0 200 OK\nVary: Accept-Encoding\nContent-Type: image/jpeg\nConnection: Close\n\n" );
n = write(newsockfd, buffer, strlen(buffer));
if (n < 0) error("ERROR writing to socket");
while (bytes > 0) {
int size = bytes > 64000 ? 64000 : bytes;
n = send(newsockfd, &(jpeg[out_buffer][start]), size, 0);
if (n < 0) error("ERROR writing to socket");
bytes -= n;
start += n;
if (bytes > 0) // Added for Firefox compatibility
usleep(250000);
}
buffer_active = false;
这是启动套接字的函数:
sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = new_portno;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
buffer_active = false;
current_buffer = -1;
die_status = false;
newsockfd = -1;
其次是:
newsockfd = accept(sockfd,
(struct sockaddr *) &cli_addr,
&clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,1024);
n = read(newsockfd,buffer,1024);