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. QTcpSocket can't read already written data from QTcpServer

QTcpSocket can't read already written data from QTcpServer

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.2k 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.
  • francescmmF Offline
    francescmmF Offline
    francescmm
    wrote on last edited by
    #1

    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;
    

    }
    @

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        Maybe QAbstractSocket::flush() will be useful.

        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