diff --git a/src/network/socket/qabstractsocket.cpp b/src/network/socket/qabstractsocket.cpp index d93150799c..5b1c5fa601 100644 --- a/src/network/socket/qabstractsocket.cpp +++ b/src/network/socket/qabstractsocket.cpp @@ -2456,13 +2456,15 @@ qint64 QAbstractSocket::readLineData(char *data, qint64 maxlen) qint64 QAbstractSocket::writeData(const char *data, qint64 size) { Q_D(QAbstractSocket); - if (d->state == QAbstractSocket::UnconnectedState) { + if (d->state == QAbstractSocket::UnconnectedState + || (!d->socketEngine && d->socketType != TcpSocket && !d->isBuffered)) { d->socketError = QAbstractSocket::UnknownSocketError; setErrorString(tr("Socket is not connected")); return -1; } - if (!d->isBuffered && d->socketType == TcpSocket && d->writeBuffer.isEmpty()) { + if (!d->isBuffered && d->socketType == TcpSocket + && d->socketEngine && d->writeBuffer.isEmpty()) { // This code is for the new Unbuffered QTcpSocket use case qint64 written = d->socketEngine->write(data, size); if (written < 0) { @@ -2473,8 +2475,7 @@ qint64 QAbstractSocket::writeData(const char *data, qint64 size) // Buffer what was not written yet char *ptr = d->writeBuffer.reserve(size - written); memcpy(ptr, data + written, size - written); - if (d->socketEngine) - d->socketEngine->setWriteNotificationEnabled(true); + d->socketEngine->setWriteNotificationEnabled(true); } return size; // size=actually written + what has been buffered } else if (!d->isBuffered && d->socketType != TcpSocket) { diff --git a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp index a4695955af..6f96e6d6f5 100644 --- a/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp +++ b/tests/auto/network/socket/qudpsocket/tst_qudpsocket.cpp @@ -117,6 +117,7 @@ private slots: void readyRead(); void readyReadForEmptyDatagram(); void asyncReadDatagram(); + void writeInHostLookupState(); protected slots: void empty_readyReadSlot(); @@ -1725,5 +1726,17 @@ void tst_QUdpSocket::asyncReadDatagram() delete m_asyncReceiver; } +void tst_QUdpSocket::writeInHostLookupState() +{ + QFETCH_GLOBAL(bool, setProxy); + if (setProxy) + return; + + QUdpSocket socket; + socket.connectToHost("nosuchserver.qt-project.org", 80); + QCOMPARE(socket.state(), QUdpSocket::HostLookupState); + QVERIFY(!socket.putChar('0')); +} + QTEST_MAIN(tst_QUdpSocket) #include "tst_qudpsocket.moc"