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. tcp listen and receive text
QtWS25 Last Chance

tcp listen and receive text

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 511 Views
  • 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.
  • pauleddP Offline
    pauleddP Offline
    pauledd
    wrote on last edited by
    #1

    Hi there,
    I have written a small Android app that sends an url link to a specified ip/port in my lan. The url is followed by a newline (\n).

    So far that works. If I run:

    ncat -k -l 192.168.1.100 6666
    

    on my Desktop Pc I can receive every url that I sent over the app.

    Now I want a Qt application that does the same instead of ncat.

    I wrote this little code but everytime I send the url the "newConnection" function gets called but appearently I dont get any bytes:

    MyServer::MyServer(QObject *parent)
    {
       server = new QTcpServer(this);
       connect(server,&QTcpServer::newConnection,this,&MyServer::newConnection);
    
       if(!server->listen(QHostAddress::Any,6666))
       {
           qDebug() << "Server could not start!";
       }
       else
       {
           qDebug() << "Server started!";
       }
    }
    
    void MyServer::newConnection()
    {
        QTcpSocket *socket = server->nextPendingConnection();
        if(socket->bytesAvailable())
        {
            qDebug() << socket->readAll();
        }
    }
    

    I also tried with:

    qDebug() << socket->bytesAvailable()
    

    and that gives me back always 0.
    Can someone give me a hint what I am doing wrong?

    VRoninV 1 Reply Last reply
    0
    • pauleddP pauledd

      Hi there,
      I have written a small Android app that sends an url link to a specified ip/port in my lan. The url is followed by a newline (\n).

      So far that works. If I run:

      ncat -k -l 192.168.1.100 6666
      

      on my Desktop Pc I can receive every url that I sent over the app.

      Now I want a Qt application that does the same instead of ncat.

      I wrote this little code but everytime I send the url the "newConnection" function gets called but appearently I dont get any bytes:

      MyServer::MyServer(QObject *parent)
      {
         server = new QTcpServer(this);
         connect(server,&QTcpServer::newConnection,this,&MyServer::newConnection);
      
         if(!server->listen(QHostAddress::Any,6666))
         {
             qDebug() << "Server could not start!";
         }
         else
         {
             qDebug() << "Server started!";
         }
      }
      
      void MyServer::newConnection()
      {
          QTcpSocket *socket = server->nextPendingConnection();
          if(socket->bytesAvailable())
          {
              qDebug() << socket->readAll();
          }
      }
      

      I also tried with:

      qDebug() << socket->bytesAvailable()
      

      and that gives me back always 0.
      Can someone give me a hint what I am doing wrong?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      You have to wait for readyRead before reading.

      void MyServer::newConnection()
      {
          QTcpSocket *socket = server->nextPendingConnection();
      QObject::connect(socket,&QTcpSocket::readyRead,this,[socket]()->void{
          if(socket->bytesAvailable())
          {
              qDebug() << socket->readAll();
          }
      });
      }
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      5
      • pauleddP Offline
        pauleddP Offline
        pauledd
        wrote on last edited by
        #3

        great! works. Thanks a lot.

        1 Reply Last reply
        1

        • Login

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