QTcpServer as HTML Server
-
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.
-
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()
-
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()
-