QTcpSocket::waitForBytesWritten always returns false



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

  • Lifetime Qt Champion

    Hi,

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



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



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



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


  • Lifetime Qt Champion

    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 ?



  • @SGaist Yes.



  • but why??



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


  • Moderators

    @Jeff-Esposito Which manual?


Log in to reply
 

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