Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTcpServer as HTML Server
Forum Updated to NodeBB v4.3 + New Features

QTcpServer as HTML Server

Scheduled Pinned Locked Moved Solved General and Desktop
qtcpserverhtmlqt5.14.2web server
3 Posts 2 Posters 335 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rtavakko
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rtavakko
      wrote on last edited by
      #3

      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()

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #2

        "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?

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        1
        • R Offline
          R Offline
          rtavakko
          wrote on last edited by
          #3

          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()

          1 Reply Last reply
          0
          • R rtavakko has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved