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. Qt networking server

Qt networking server

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.9k Views 1 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.
  • G Offline
    G Offline
    great88
    wrote on last edited by
    #1

    I am trying to make a server application .
    I looked at the threadedfortuneserver example that comes with QT 5 .
    How do you keep a connection open until the client decides to disconnect ? I would like to
    maintain a list of all open connections/sockets .

    At the end of fortunethread.cpp , the code is :
    tcpSocket.write(block);
    tcpSocket.disconnectFromHost();
    tcpSocket.waitForDisconnected();

    So the socket is disconnected . If I comment out the last 2 lines , nothing is written to the client for some reason ?
    How do I keep the connection open ? Do I use the socketDescriptor somehow ? I would try that but it seems to me
    that the socket connection is gone .

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      Here is the simplest example to read and write on the same socket:

      server.h
      @
      #include <QObject>
      #include <QTcpServer>
      #include <QTcpSocket>

      class ExTCPServer : public QObject
      {
      Q_OBJECT
      public:
      explicit ExTCPServer(QObject *parent = 0);

      private slots:
      void new_connection();
      void ready_read();

      private:
      QTcpServer * server;
      QTcpSocket * sock;
      };
      @

      server.cpp
      @
      #include "server.h"
      #include <QDebug>

      ExTCPServer::ExTCPServer(QObject *parent) :
      QObject(parent)
      {
      this->server = new QTcpServer(this);
      connect(this->server, SIGNAL(newConnection()), this, SLOT(new_connection()));
      if(!this->server->listen(QHostAddress::Any, 4444))
      {
      qCritical() << "ERROR: " << this->server->errorString();
      }
      }

      void ExTCPServer::new_connection()
      {
      if(!this->server->hasPendingConnections())return;
      this->sock = this->server->nextPendingConnection();
      connect(this->sock, SIGNAL(readyRead()),this, SLOT(ready_read()));
      if(this->sock->bytesAvailable() > 0)
      {
      qDebug() << FUNCTION;
      qDebug() << this->sock->readAll();
      this->sock->write("I need more!");
      }
      }

      void ExTCPServer::ready_read()
      {
      if(this->sock->bytesAvailable() > 0)
      {
      qDebug() << this->sock->readAll();
      this->sock->write("I need more!");
      }
      }
      @

      client.h
      @
      #include <QObject>
      #include <QTcpSocket>

      class ExTCPClient : public QObject
      {
      Q_OBJECT
      public:
      explicit ExTCPClient(QObject *parent = 0);

      private slots:
      void connected();
      void ready_read();

      private:
      QTcpSocket * sock;
      };
      @

      client.cpp
      @
      #include "client.h"
      #include <QHostAddress>
      #include <QDebug>

      ExTCPClient::ExTCPClient(QObject *parent) :
      QObject(parent)
      {
      this->sock = new QTcpSocket(this);
      connect(this->sock, SIGNAL(connected()), this, SLOT(connected()));
      connect(this->sock, SIGNAL(readyRead()), this, SLOT(ready_read()));
      this->sock->connectToHost(QHostAddress::Any, 4444);
      }

      void ExTCPClient::connected()
      {
      qDebug() << this->sock->write("read this!");
      }

      void ExTCPClient::ready_read()
      {
      if(this->sock->bytesAvailable() > 0)
      {
      qDebug() << this->sock->readAll();
      this->sock->write("more data");
      }
      }
      @

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        great88
        wrote on last edited by
        #3

        OK , thanks for that . It seems straight forward enough . For the threadedfortuneserver example , why doesn't tcpSocket.write(block); write back to the client right away ? If I comment out the following lines :
        tcpSocket.disconnectFromHost();
        tcpSocket.waitForDisconnected();

        then nothing happens . It seems those two lines are needed to do the write ?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          great88
          wrote on last edited by
          #4

          OK , I got the threadedfortuneserver code to work by
          replacing
          tcpSocket.write(block);
          tcpSocket.disconnectFromHost();
          tcpSocket.waitForDisconnected();

          with
          tcpSocket.write(block);
          tcpSocket.waitForBytesWritten();

          not sure I totally understand but interesting . If anyone has comments , please do write .

          1 Reply Last reply
          0

          • Login

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