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::waitForBytesWritten always returns false

QTcpSocket::waitForBytesWritten always returns false

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 3.1k 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.
  • J Offline
    J Offline
    Jeff Esposito
    wrote on last edited by VRonin
    #1

    I am on Windows 10
    My QT version is 5.7.0
    c++

    This is all part of a big app, so I have just included some parts of it. Below is the code that calls waitForBytesWritten.

    //
    //	writes a request to the socket and reads the server's reply
    //
    bool ClientSocket::WriteRead(const jlib::CReq& req, jlib ::CRsp& rsp)
    {
    	bool connected = (mqSocket->state() == QTcpSocket::ConnectedState);
    	bool gotCount = false;
    	qint64 bufoff = 0;
    
    	if (!connected)
    	{
    		THROW_ERR(9999,
    			<< mIpAddr.toStdString()
    			<< ":"
    			<< mPort
    			<< " is not connected");
    	}
    	mqSocket->write((const char *)&req, req.mMsgLen);
    	while( mqSocket->bytesToWrite() > 0 )
    	{
    		if ( !mqSocket->waitForBytesWritten(2000) );
    		{
    			THROW_ERR(9999,
    				<< mIpAddr.toStdString()
    				<< ":"
    				<< mPort
    				<< " write failed with error "
    				<< mqSocket->error()
    				<< ": "
    				<< mqSocket->errorString().toStdString());
    		}
    	}
    	qDebug() << "Wrote request: \n" ;
    	std::string strReq = req.traceStr();
    	qDebug() << QString::fromStdString(strReq);
    	while (mqSocket->waitForReadyRead(kReadTimeout))
    	{
    		qint64 countRead = mqSocket->read(((char *)&rsp)+bufoff, sizeof(rsp));
    		bufoff += countRead;
    		bool hasEnufDataQueued = bufoff >= sizeof(rsp.mMsgLen)
    			&& bufoff >= rsp.mMsgLen;
    		if (bufoff > rsp.mMsgLen)
    		{
    			THROW_ERR(9999,
    			<< mIpAddr.toStdString()
    			<< ":"
    				<< mPort
    				<< " rsp too big, was "
    				<< bufoff
    				<< " s/b "
    				<< rsp.mMsgLen);
    		}
    		return true;
    	}
    	THROW_ERR(9999,
    		<< mIpAddr.toStdString()
    		<< ":"
    		<< mPort
    		<< " read failed with error "
    		<< mqSocket->error()
    		<< ": "
    		<< mqSocket->errorString().toStdString());
    	return false;
    }
    

    Note: There is some thread stuff going on also.

    This routine gets called from a "LogonWorker" thread, which is supposed to issue a logon request to the server in the background. Here is the stuff in my main, which sets up the logon thread. Maybe it has something to do with it.

    QtOub::QtOub(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	//
    	//	Set up the logon worker thread
    	//
    	qRegisterMetaType<oub::CLogonReq>("oub::CLogonReq");
    	qRegisterMetaType<oub::CLogonRsp>("oub::CLogonRsp");
    	mpLogonThread = new QThread(this);
    	mpLogonWorker = new LogonWorker;
    	mpLogonWorker->moveToThread(mpLogonThread);
    	connect(mpLogonWorker, SIGNAL(logonFinished(const oub::CLogonRsp&)),
    		this, SLOT(onLogonFinished(const oub::CLogonRsp&)));
    	connect(mpLogonWorker, SIGNAL(logonError(const QString&)),
    		this, SLOT(onLogonError(const QString&)));
    	connect(this, SIGNAL(startLogon(const oub::CLogonReq&)),
    		mpLogonWorker, SLOT(onStartLogon(const oub::CLogonReq&)));
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What's the size of your payload ? What's the speed of your network ?

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

      J 1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Why do you spawn a thread and use the synchronous API instead of just using the asynchronous one instead?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        J 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          What's the size of your payload ? What's the speed of your network ?

          J Offline
          J Offline
          Jeff Esposito
          wrote on last edited by
          #4

          The logon request is 96 bytes.

          I am still developing, so the server and client are running on same windows PC, and there really is no network.

          Also, the server actually sees the 96 bytes the client sent.

          1 Reply Last reply
          0
          • VRoninV VRonin

            Why do you spawn a thread and use the synchronous API instead of just using the asynchronous one instead?

            J Offline
            J Offline
            Jeff Esposito
            wrote on last edited by
            #5

            @VRonin I use "threads" in general rather than "async io" to reduce complexity.

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

              Just to be sure I understand correctly: you are reducing complexity by adding threads using a blocking mechanism for a background task to avoid using the asynchronous nature of QTcpSocket ?

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

              J 1 Reply Last reply
              2
              • SGaistS SGaist

                Just to be sure I understand correctly: you are reducing complexity by adding threads using a blocking mechanism for a background task to avoid using the asynchronous nature of QTcpSocket ?

                J Offline
                J Offline
                Jeff Esposito
                wrote on last edited by
                #7

                @SGaist Yes.

                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #8

                  but why??

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  1 Reply Last reply
                  1
                  • J Offline
                    J Offline
                    Jeff Esposito
                    wrote on last edited by
                    #9

                    Because the manual says its the preferred way to do it.

                    jsulmJ 1 Reply Last reply
                    0
                    • J Jeff Esposito

                      Because the manual says its the preferred way to do it.

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Jeff-Esposito Which manual?

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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