Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtcpSocket—QIODevice.write() sometimes return -1



  • Dear all,

    I set up a QtcpSocket to test my TCP based communication as follows:

    QtcpSocket ipSocket = new QtcpScoket(this);
    ipSocket->connectToHost(HostName, dataPort);
    if(ipSocket->waitForConnected(5000ms))
    {
    	.........................
    }
    Else
    {
    	.......................
    }
    

    it works fine.

    then I want to write some message to the TCP with following codes:

    QIODevice m_ioDevice;
    if((this->m_ioDevice != 0) && (this->m_ioDevice->isWritable())
    {
    	if(This.m_ioDevice->write(msg) == msg.count())
    	{
    		.........................
    	}
    	Else
    	{
    		........................
    	}
    }
    

    it also works in most time. But sometimes the message can not be written to the IODevice, and the function This.m_ioDevice->write(msg) return -1.

    I am not sure:

    1. what does it mean when This.m_ioDevice->write(msg) returns -1 ?
    2. why my code works fine in most time, only sometime the problem occurs?
    3. how could I solve this problem?

    thanks a lot for any of your suggestion and reply

    best regards,
    hansflying



  • Welcome to devnet

    I have added editor markdown tags for your code block. This makes it easier to read. Please have a look to the "Editor markdown tags" at the end of this page.

    You can call error() and errorString(). From experience I guess that the connection broke down for whatever reason.



  • thank you koahnig a lot!!

    I will try your suggestion.

    But the problem occurs sometimes. Whatever we will check it.


Log in to reply