QTcpSocket can't read already written data from QTcpServer



  • I'm doing a threaded server that creates a thread for each incomming connection from a client. In the other side I have a client that only writes and receives data without threads. The problem is that when I discover a new client (using Server::incomingConnection(int) ) I start a thread with the socketDescriptor as parameter. This thread creates a socket to communicate with the client in the ServerThread::start() method and there written data is sent to the client as "Werlcome!" message.

    Data is sent and in the client a readyRead() signal is emit. I catch it and try to show incomming data. The problem is that when I call socket->canReadLine() in the client, it returns false.

    Brief description of structure: A Server (inherits QTcpServer) has a list of ServerThread. Each ServerThread (inherits QThread) is created and started in the Server::incommingConnection(int). Each ServerThread contains one Client (inherits QTcpSocket) where data is read or written.

    I post the code although it is a lot:

    SERVER

    Server.cpp
    @void Server::incomingConnection(int socketDescriptor)
    {
    ServerThread *thread = new ServerThread(socketDescriptor);
    threadList.append(thread);
    connect(thread, SIGNAL(receivedMsg(QString)), this, SLOT(msgFromThread(QString)));
    connect(thread, SIGNAL(closeThread(int)), this, SLOT(threadFinished(int)));
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
    }

    void Server::writeToClients(const QString &_msg)
    {
    for (int i = 0; i < threadList.size(); i++)
    threadList.at(i)->writeMsg(_msg);
    }
    @

    ServerThread.cpp
    @ServerThread::ServerThread(int socketDescriptor, QObject *parent) : QThread(parent)
    {
    socketId = socketDescriptor;
    client = new Client();
    client->moveToThread(this);
    }

    void ServerThread::run()
    {
    connect(client, SIGNAL(readyRead()), this, SLOT(readyRead()));
    connect(client, SIGNAL(disconnected()), this, SLOT(disconnectClient()));

    client->setSocketDescriptor(socketId);
    
    qDebug() << QString("[%1] New client from %2").arg(QDateTime::currentDateTime().toString("dd/MM/yyyy hh:mm:ss")).arg(client->peerAddress().toString());
    
    client->write("Connected to server!");
    client->waitForBytesWritten();
    
    while (client->isOpen())
        client->waitForReadyRead(1000);
    
    delete client;
    client = NULL;
    
    emit closeThread(socketId);
    

    }

    void ServerThread::readyRead()
    {
    QTcpSocket clSender = (QTcpSocket)sender();

    qDebug() << QString("Received missage from client [%1]").arg(clSender->peerAddress().toString());
    

    }

    void ServerThread::writeMsg(const QString &msg)
    {
    qDebug()<<client->write(QString("Connected to server!").toLatin1());
    client->waitForBytesWritten(15);
    }
    @

    CLIENT
    Client.cpp
    @GadaTester::GadaTester(const QString &nif, QWidget *parent) : ui(new Ui::GadaTester)
    {
    setAttribute(Qt::WA_DeleteOnClose);

    user = nif;
    ui->setupUi(this);
    
    socket = new QTcpSocket(this);
    
    connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(connectionClosedByServer()));
    
    connect(ui->pbConnect, SIGNAL(clicked()), this, SLOT(restoreConnection()));
    connect(ui->pbSend, SIGNAL(clicked()), this, SLOT(sendMsg()));
    connect(ui->pbStop, SIGNAL(clicked()), this, SLOT(closeSocket()));
    connect(ui->pbClose, SIGNAL(clicked()), this, SLOT(close()));
    
    addAction(ui->actionClose);
    

    }

    void GadaTester::restoreConnection()
    {
    qDebug()<<"Connecting to server...";
    socket->connectToHost(QHostAddress::LocalHost, PORT);
    }

    void GadaTester::connected()
    {
    socket->write(QString("Informacio del nom del socket").toLatin1());
    socket->waitForBytesWritten();

    ui->pbConnect->setEnabled(false);
    ui->pbStop->setEnabled(true);
    
    qDebug()<<QString("Connected to host %1:%2.\n").arg(socket->peerAddress().toString()).arg(socket->peerPort()));
    

    }

    void GadaTester::sendMsg()
    {
    socket->write("SOCKET_INFO;Missage 1");
    socket->waitForBytesWritten();
    }

    void GadaTester::readMsg()
    {
    qDebug()<<"Received answer.";

    QString line = "";
    
    while (socket->canReadLine()) //Always returns false
        line += socket->readLine().trimmed();
    
    qDebug()<<line;
    

    }
    @


  • Lifetime Qt Champion

    Hi,

    It seems that you suffer from the same problem as in your other thread.

    Add a "\n" to your string in sendMsg and you should be good.

    You might also be interested by bytesAvailable

    Hope it helps



  • Maybe QAbstractSocket::flush() will be useful.


Log in to reply
 

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