QTcpServer as HTML Server
-
wrote on 30 Apr 2024, 03:45 last edited by
I've been trying to port a basic Arduino HTML server that I have to QT 5 code and I'm having a bit of difficulty. With Arduino you simply write your HTML code to the client but when I do the same thing with a TCP client that I get by calling nextPendingConnection(), I can't view the HTML page. Is this the correct way of doing this? I understand QT 6 has some classes that are designed specifically for this but I'd rather do something more simple and generic.
-
wrote on 2 May 2024, 22:53 last edited by
The server page was not loading and the issue was I was using the server's newConnection() signal to immediately write the html page to the client. But you have to wine and dine the client first. I ended up connecting the newConnection() signal to a set up function:
void Server::handleConnection() { QTcpSocket* client = nextPendingConnection(); //client->setSocketOption(QAbstractSocket::KeepAliveOption, 0); QObject::connect(client,&QTcpSocket::readyRead,this,[=](){ handleRequest(client); }); }
You can then write the HTML content to the client in handleRequest()
-
wrote on 30 Apr 2024, 06:16 last edited by
"can't view the HTML page" is hard to evaluate without more information. Is the client (browser?) not receiving any response? Is the response incomplete or corrupted? Does the content-type, including encoding, correspond to the data received?
-
wrote on 2 May 2024, 22:53 last edited by
The server page was not loading and the issue was I was using the server's newConnection() signal to immediately write the html page to the client. But you have to wine and dine the client first. I ended up connecting the newConnection() signal to a set up function:
void Server::handleConnection() { QTcpSocket* client = nextPendingConnection(); //client->setSocketOption(QAbstractSocket::KeepAliveOption, 0); QObject::connect(client,&QTcpSocket::readyRead,this,[=](){ handleRequest(client); }); }
You can then write the HTML content to the client in handleRequest()
-
3/3