Results 1 to 4 of 4

Thread: Blocking QTcpSocket for read/write operations

  1. #1
    Join Date
    Dec 2009
    Location
    Romania, Iasi
    Posts
    18
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Blocking QTcpSocket for read/write operations

    Hello guys,

    I am trying to implement a blocking QTcpSocket for both reading and writing operations. The application to which I am connecting to is written in Java and for very unknown reasons the non-blocking approach doesn't work. With non-blocking approach the highest state reached is QAbstractSocket::ConnectingState and NOT Connected, although the Java client shows it got a new connection.
    So this code:

    tcpSocket = new QTcpSocket(this);
    QObject::connect(tcpSocket, SIGNAL(connected()), SLOT(onConnect()), Qt:irectConnection);
    QObject::connect(tcpSocket, SIGNAL(hostFound()), SLOT(hostFound()), Qt:irectConnection);
    QObject::connect(tcpSocket, SIGNAL(readyRead()), SLOT(readFromSocket()), Qt:irectConnection);
    QObject::connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
    SLOT(displayError(QAbstractSocket::SocketError)), Qt:irectConnection);
    does not work.

    By the other side, for the same unkown reasons if I'm using this code:

    const int Timeout = 2 * 1000;
    if (!tcpSocket->waitForConnected(Timeout)) {
    displayError(tcpSocket->error());
    }
    I enter the QAbstractSocket::ConnectedState ... very strage! Non-blocking approach doesn't work and the blocking on works.

    The problem with the blocking approach is that I have to put the code which waits for the data into a separate thread (not to freeze the app), like this:

    void EQtClientSocket::run()
    {
    tcpSocket = new QTcpSocket;

    if (!connectionRequested) {
    mutex.lock();
    cond.wait(&mutex);
    mutex.unlock();
    }

    while(!quit) {

    // already connected?
    if(tcpSocket->state() == QAbstractSocket::ConnectedState) {
    errno = EISCONN;
    getWrapper()->error(NO_VALID_ID, ALREADY_CONNECTED.code(), ALREADY_CONNECTED.msg());
    }

    // starting to connect to server
    tcpSocket->abort();
    tcpSocket->connectToHost(host, port);

    const int Timeout = 2 * 1000;
    if (!tcpSocket->waitForConnected(Timeout)) {
    displayError(tcpSocket->error());
    }
    else {
    socketState = QAbstractSocket::ConnectedState;
    onConnect();
    emit connected();
    qDebug() << "connection ok";
    }

    // set client id
    while (tcpSocket->bytesAvailable() < (int)sizeof(quint16)) {
    if (tcpSocket->waitForReadyRead()) {
    onReceive();
    qDebug() << "New data available";
    }
    }

    mutex.lock();
    cond.wait(&mutex);
    mutex.unlock();
    }
    }
    however I need to write something to the socket, which I can't do it from a different thread which created the socket (every QObject should be in the same thread), but I can't write something from the same thread since tcpSocket->waitForReadyRead() would block the thread in most of the time and will wait for the data until it starts again.

    I'm stuck. I can't implement a read&write QTcpSocket with the blocking approach.

    Please help

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Blocking QTcpSocket for read/write operations

    Maybe you have a problem with your event loop? Post a compilable example and maybe we can help.

  3. #3
    Join Date
    Dec 2009
    Location
    Romania, Iasi
    Posts
    18
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Blocking QTcpSocket for read/write operations

    Quote Originally Posted by squidge View Post
    Maybe you have a problem with your event loop? Post a compilable example and maybe we can help.
    Thanks squidge, as per your suggestions I did some more digs and found the problem related to the event loop. This solved the problem.

  4. #4
    Join Date
    Jun 2020
    Posts
    9
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Blocking QTcpSocket for read/write operations

    Please tell me how you solved the problem of starting the event processing cycle (in fact, QCoreApplication::exec()) in Java GUI-app. I have an unsuccessful attempt to start a simple asynchronous socket client - it just does not work in JavaFX. I am trying to make a synchronous client with blocking sockets, but they also require signals and slots and therefore an event loop.

Similar Threads

  1. Replies: 2
    Last Post: 2nd November 2010, 05:15
  2. QTcpSocket Write & \0 \x00
    By NewGuy5800 in forum Newbie
    Replies: 1
    Last Post: 25th April 2010, 10:23
  3. read and write on qtcpsocket
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 09:42
  4. Non-blocking stream read
    By akiross in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2009, 11:07
  5. QTcpSocket waiting for write to be read
    By spraff in forum Qt Programming
    Replies: 1
    Last Post: 23rd December 2008, 19:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.