本文整理汇总了C++中QUdpSocket::flush方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::flush方法的具体用法?C++ QUdpSocket::flush怎么用?C++ QUdpSocket::flush使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QUdpSocket
的用法示例。
在下文中一共展示了QUdpSocket::flush方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sendToEditor
bool EditorPipe::sendToEditor(QString command)
{
QUdpSocket socket;
// Attempt to connect to the LocalServer
socket.connectToHost(QHostAddress::LocalHost, 58487);
if(socket.waitForConnected(100))
{
qDebug() << "Connected";
QString str = QString(command);
QByteArray bytes;
bytes = str.toUtf8();
socket.write(bytes);
socket.waitForBytesWritten(10000);
socket.flush();
socket.disconnectFromHost();
qDebug() << "Bytes sent: " <<command;
return true;
}
else
{
qDebug() << "sendToEditor(QString command) fail to connect: " << socket.errorString();
return false;
}
}
示例2: udp_ping
int udp_ping(QStringList command) {
qDebug() << "udp_ping(" << command.join(" ") << ")" << endl;
qDebug() << "udp_ping(" << command.join(" ") << ")" << endl;
/**
* Check input
*/
QTextStream errorStream(stderr);
if(command.size() != 3 || command.at(0)!="ping" || command.at(1)!="udp" ) {
errorStream << "Error: udp_ping(" << command.join(" ") << ") is no valid call (ping udp <ip_address> <port> rzv|max|random|default)" << endl;
return 1;
}
/**
* <functionality>
*/
QByteArray byteArray;
/**
* CIP for "rzv"
*/
if(command.at(2)=="rzv") {
qDebug() << "rzv" << endl;
byteArray.append(QByteArray(42, '\0'));
}
/**
* Sent via TCP
*/
QUdpSocket *udpSocket;
udpSocket = new QUdpSocket();
QTextStream outStream(stdout);
QString out;
udpSocket->abort();
udpSocket->connectToHost("127.0.0.1", 22366);
qDebug() << "waitForConnected!";
if (udpSocket->waitForConnected(5000)) {
qDebug() << "Connected!";
}
else {
errorStream << "Error: udp_ping(" << command.join(" ") << "): No connection available!" << endl;
return 1;
}
qDebug() << QString("BytesWritten: %1").arg(udpSocket->write(byteArray, byteArray.length()));
udpSocket->flush();
int numRead = 0, numReadTotal = 0;
char buffer[MAXMSG];
forever {
numRead = udpSocket->read(buffer, MAXMSG);
qDebug() << "read buffer: " << numRead;
numReadTotal += numRead;
if (numRead <= 0 && !udpSocket->waitForReadyRead(30))
break;
}
qDebug() << numReadTotal << " bytes red";
if(numReadTotal==-1) {
errorStream << "Error: udp_ping(" << command.join(" ") << "): " << udpSocket->errorString() << endl;
return 1;
}
else {
for(int i=0; i < numReadTotal; i++) {
qDebug() << QString("receipt[%1]: %2\n").arg(i).arg(buffer[i], 8, 2, QLatin1Char('0')) << endl;;
}
QByteArray receipt(buffer);
qDebug() << "receipt.size(): " << receipt.size();
for(int i = 0; i < receipt.size();++i) {
qDebug() << QString("receipt[%1]: %2\n").arg(i).arg(receipt.at(i), 8, 2, QLatin1Char('0')) << endl;;
}
qDebug() << "buffer: " << buffer;
}
udpSocket->disconnectFromHost();
udpSocket->close();
outStream << out << endl;
return 0;
}