【发布时间】:2016-05-02 11:54:24
【问题描述】:
我想为MJPEGs 创建一个服务器,我找到了这个教程:http://www.bogotobogo.com/Qt/Qt5_QTcpServer_Client_Server.php。但是当我从 Chrome 之类的 Web 浏览器(Microsoft Telnet 工作正常)连接时,它显示连接重置。
创建服务器后,我想在我的浏览器上显示MJPEGs(就像 IP 摄像机一样),使用:How to Create a HTTP MJPEG Streaming Server With QTcp-Server Sockets?
这是我的 Server.cpp(稍作修改) -
#include "stdafx.h"
#include "TCPServer.h"
TcpServer::TcpServer(QObject *parent) :
QObject(parent)
{
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()),
this, SLOT(newConnection()));
if (!server->listen(QHostAddress::Any, 9999))
qDebug() << "Server could not start";
else
qDebug() << "Server started!";
}
void TcpServer::newConnection()
{
QTcpSocket *socket = server->nextPendingConnection();
QByteArray header = "HTTP/1.1 200 OK\r\n";
socket->write(header);
QByteArray ContentType = "Content-Type: text/html\r\n";
socket->write(ContentType);
QByteArray Body = "Test";
socket->write(Body);
socket->flush();
socket->close();
}
【问题讨论】:
-
您忘记了关于 HTTP 标准的一个非常重要的事情:标题和正文之间的空行。我建议您搜索一个专用的 HTTP 服务器库,这将简化很多其他手动工作。它甚至可能包含简单的路由,因此您可以使用不同的 URL 来给出不同的回复。