Qt networking server



  • 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 .



  • 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");
    }
    }
    @



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



  • 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 .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.