void ClientSktUdp::error() { QUdpSocket* s = qobject_cast<QUdpSocket*>(sender()); show(QString("UDP socket error %1, %2").arg(s->error()).arg(s->errorString())); unplug(); }
void tst_QUdpSocket::writeToNonExistingPeer() { QSKIP("Connected-mode UDP sockets and their behaviour are erratic", SkipAll); QFETCH(QHostAddress, peerAddress); quint16 peerPort = 34534; qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); QUdpSocket sConnected; QSignalSpy sConnectedReadyReadSpy(&sConnected, SIGNAL(readyRead())); QSignalSpy sConnectedErrorSpy(&sConnected, SIGNAL(error(QAbstractSocket::SocketError))); sConnected.connectToHost(peerAddress, peerPort, QIODevice::ReadWrite); // the first write succeeds... QCOMPARE(sConnected.write("", 1), qint64(1)); // the second one should fail! QTest::qSleep(1000); // do not process events QCOMPARE(sConnected.write("", 1), qint64(-1)); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); // the third one will succeed... QCOMPARE(sConnected.write("", 1), qint64(1)); QTestEventLoop::instance().enterLoop(1); QCOMPARE(sConnectedReadyReadSpy.count(), 0); QCOMPARE(sConnectedErrorSpy.count(), 1); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); // we should now get a read error QCOMPARE(sConnected.write("", 1), qint64(1)); QTest::qSleep(1000); // do not process events char buf[2]; QVERIFY(!sConnected.hasPendingDatagrams()); QCOMPARE(sConnected.bytesAvailable(), Q_INT64_C(0)); QCOMPARE(sConnected.pendingDatagramSize(), Q_INT64_C(-1)); QCOMPARE(sConnected.readDatagram(buf, 2), Q_INT64_C(-1)); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); QCOMPARE(sConnected.write("", 1), qint64(1)); QTest::qSleep(1000); // do not process events QCOMPARE(sConnected.read(buf, 2), Q_INT64_C(0)); QCOMPARE(int(sConnected.error()), int(QUdpSocket::ConnectionRefusedError)); // we should still be connected QCOMPARE(int(sConnected.state()), int(QUdpSocket::ConnectedState)); }
void tst_QUdpSocket::constructing() { QUdpSocket socket; QVERIFY(socket.isSequential()); QVERIFY(!socket.isOpen()); QVERIFY(socket.socketType() == QUdpSocket::UdpSocket); QCOMPARE((int) socket.bytesAvailable(), 0); QCOMPARE(socket.canReadLine(), false); QCOMPARE(socket.readLine(), QByteArray()); QCOMPARE(socket.socketDescriptor(), -1); QCOMPARE(socket.error(), QUdpSocket::UnknownSocketError); QCOMPARE(socket.errorString(), QString("Unknown error")); // Check the state of the socket api }
void tst_QUdpSocket::ipv6Loop() { QFETCH(QByteArray, peterMessage); QFETCH(QByteArray, paulMessage); QFETCH(bool, success); QUdpSocket peter; QUdpSocket paul; quint16 peterPort = 28124; quint16 paulPort = 28123; if (!peter.bind(QHostAddress::LocalHostIPv6, peterPort)) { QCOMPARE(peter.error(), QUdpSocket::UnsupportedSocketOperationError); } else { QVERIFY(paul.bind(QHostAddress::LocalHostIPv6, paulPort)); QCOMPARE(peter.writeDatagram(peterMessage.data(), peterMessage.length(), QHostAddress("::1"), paulPort), qint64(peterMessage.length())); QCOMPARE(paul.writeDatagram(paulMessage.data(), paulMessage.length(), QHostAddress("::1"), peterPort), qint64(paulMessage.length())); char peterBuffer[16*1024]; char paulBuffer[16*1024]; #if !defined(Q_OS_WINCE) QVERIFY(peter.waitForReadyRead(5000)); QVERIFY(paul.waitForReadyRead(5000)); #else QVERIFY(peter.waitForReadyRead(15000)); QVERIFY(paul.waitForReadyRead(15000)); #endif if (success) { QCOMPARE(peter.readDatagram(peterBuffer, sizeof(peterBuffer)), qint64(paulMessage.length())); QCOMPARE(paul.readDatagram(paulBuffer, sizeof(peterBuffer)), qint64(peterMessage.length())); } else { QVERIFY(peter.readDatagram(peterBuffer, sizeof(peterBuffer)) != paulMessage.length()); QVERIFY(paul.readDatagram(paulBuffer, sizeof(peterBuffer)) != peterMessage.length()); } QCOMPARE(QByteArray(peterBuffer, paulMessage.length()), paulMessage); QCOMPARE(QByteArray(paulBuffer, peterMessage.length()), peterMessage); } }
void tst_QUdpSocket::writeDatagram() { QUdpSocket server; QVERIFY2(server.bind(), server.errorString().toLatin1().constData()); QHostAddress serverAddress = QHostAddress::LocalHost; if (!(server.localAddress() == QHostAddress::Any)) serverAddress = server.localAddress(); QUdpSocket client; qRegisterMetaType<qint64>("qint64"); qRegisterMetaType<QAbstractSocket::SocketError>("QAbstractSocket::SocketError"); for(int i=0;; i++) { QSignalSpy errorspy(&client, SIGNAL(error(QAbstractSocket::SocketError))); QSignalSpy bytesspy(&client, SIGNAL(bytesWritten(qint64))); qint64 written = client.writeDatagram(QByteArray(i * 1024, 'w'), serverAddress, server.localPort()); if (written != i * 1024) { #if defined (Q_OS_HPUX) QSKIP("HP-UX 11.11 on hai (PA-RISC 64) truncates too long datagrams.", SkipSingle); #endif QCOMPARE(bytesspy.count(), 0); QCOMPARE(errorspy.count(), 1); QCOMPARE(*static_cast<const int *>(errorspy.at(0).at(0).constData()), int(QUdpSocket::DatagramTooLargeError)); QCOMPARE(client.error(), QUdpSocket::DatagramTooLargeError); break; } QVERIFY(bytesspy.count() == 1); QCOMPARE(*static_cast<const qint64 *>(bytesspy.at(0).at(0).constData()), qint64(i * 1024)); QCOMPARE(errorspy.count(), 0); if (!server.waitForReadyRead(5000)) QSKIP(QString("UDP packet lost at size %1, unable to complete the test.").arg(i * 1024).toLatin1().data(), SkipSingle); QCOMPARE(server.pendingDatagramSize(), qint64(i * 1024)); QCOMPARE(server.readDatagram(0, 0), qint64(0)); } }