本文整理汇总了C++中QUdpSocket::bytesAvailable方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::bytesAvailable方法的具体用法?C++ QUdpSocket::bytesAvailable怎么用?C++ QUdpSocket::bytesAvailable使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QUdpSocket
的用法示例。
在下文中一共展示了QUdpSocket::bytesAvailable方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getNTPTime
void TimeSync::getNTPTime()
{
QUdpSocket sock;
sock.connectToHost("0.pool.ntp.org",123);
if(!sock.waitForConnected(1000))return;
QByteArray data(48,char(0)); *(reinterpret_cast<qint32 *>(&data.data()[0]))=4194959577;
if(sock.write(data)<0||!sock.waitForReadyRead(3000)||sock.bytesAvailable()!=48)return;
data=sock.readAll();
quint32 seconds=qToBigEndian(*(reinterpret_cast<quint32 *>(&data.data()[40])));
quint32 fraction=qToBigEndian(*(reinterpret_cast<quint32 *>(&data.data()[44])));
quint32 newTime=QDateTime::fromMSecsSinceEpoch(seconds*1000ll+fraction*1000ll/0x100000000ll-2208988800000ll).toTime_t();
if(newTime<1451606400 || newTime>4000000000){
QThread::msleep(500);
emit startSync();
return;
}
qint32 tempTimeShift=qint64(newTime)-qint64(QDateTime::currentDateTime().toTime_t());
if(timeShift!=0)tempTimeShift=qint32((qint64(timeShift)+qint64(tempTimeShift))/2);
if(tempTimeShift>3600 || tempTimeShift<-3600){
static bool showMessage=true;
if(showMessage)emit warningMessage(julyTr("TIME_SYNC_ERROR","Your clock is not set. Please close the Qt Bitcoin Trader and set the clock. Changing time at Qt Bitcoin Trader enabled can cause errors and damage the keys."));
showMessage=false;
}
else
timeShift.fetchAndStoreOrdered(tempTimeShift);
getNTPTimeRetryCount++;
if(getNTPTimeRetryCount<3)emit startSync();
}
示例2: newData
void ClientSktUdp::newData()
{
QUdpSocket* s = qobject_cast<QUdpSocket*>(sender());
if (!s) return;
qint64 bufLen = s->bytesAvailable();
char* buf = TK::createBuffer(bufLen, MAXBUFFER);
if (!buf) return;
qint64 readLen = 0;
qint64 ioLen = s->read(buf, bufLen);
while (ioLen > 0)
{
readLen += ioLen;
ioLen = s->read(buf+readLen, bufLen-readLen);
}
if (ioLen >= 0)
{
recordRecv(readLen);
dump(buf, readLen, false);
}
TK::releaseBuffer(buf);
}
示例3: constructing
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
}
示例4: writeToNonExistingPeer
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));
}