hypnomaki
hypnomaki

Reputation: 662

How to Create a Http Server with TCP Server Sockets in C++/QT

I want to create a Http Server to send an MJPEG Stream. In the first place i therefore want to create a Simple Version that just sends some html/text. I've already Managed to set up a TCP-Server but I don't have any clue about how to "act" like an http Server.

What I did: Created an TCP-Server. When a client Connects a TCP-Socket is created. Then I implemented a ReadyRead SLOT which gots executed when the Browser sends the "GET" Request to the Server.

GET / HTTP/1.1
Host: 127.0.0.1:8889
User-Agent: Mozilla/5.0...

Then I run following Code

QByteArray header = "HTTP/ 1.1 200 OK\r\n";
m_Client->write(header);

QByteArray ContentType = "Content-Type: text/html\r\n";
m_Client->write(ContentType);

QByteArray Body = "Test";
m_Client->write(Body);

m_Client->close();

But what I see in the Browser is

HTTP/ 1.1 200 OK
Content-Type: text/html
Test

So what am I doing wrong? I thought about receiving the Client GET Request, sending the Header, Mimes and the Content in Return and Then Closing the Connection.... Is this Method wrong or Just the way I coded it?

Upvotes: 1

Views: 2042

Answers (1)

Matteo Italia
Matteo Italia

Reputation: 126957

You have an extra space between the / and 1.1, and you are missing an empty line between the headers block and the response body.

Upvotes: 3

Related Questions