本文整理汇总了C++中QUdpSocket::close方法的典型用法代码示例。如果您正苦于以下问题:C++ QUdpSocket::close方法的具体用法?C++ QUdpSocket::close怎么用?C++ QUdpSocket::close使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QUdpSocket
的用法示例。
在下文中一共展示了QUdpSocket::close方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: process
void FetchThread::process(QString phost)
{
QUdpSocket *udpSocket ;
udpSocket= new QUdpSocket(0);
udpSocket->bind(QHostAddress::LocalHost, 9999);
udpSocket->waitForConnected(250);
QTcpSocket socket;
socket.connectToHost("localhost", 4949);
socket.waitForConnected(500);
while (socket.waitForReadyRead(250));
QString t_hello = socket.readAll();
qDebug() << "READ: " << t_hello;
socket.write(QString("list\n").toStdString().c_str() );
while (socket.waitForReadyRead(250));
QString buf1 = socket.readAll();
qDebug() << "READ: " << buf1;
QStringList list_probe = buf1.split(QRegExp("\\s+"));
for (int z=0; z< list_probe.size(); z++)
{
QString probe=list_probe.at(z);
QString cmd = QString("fetch ").append(probe).append("\n");
qDebug() << "cmd : " << cmd;
socket.write(cmd.toStdString().c_str() );
while (socket.waitForReadyRead(250));
QString buf2 = socket.readAll();
qDebug() << "Rep fetch :" << buf2 << "\n";
QRegularExpression re("(\\w+).(\\w+) ([0-9.]+)\\n");
QRegularExpressionMatchIterator i = re.globalMatch(buf2);
re.setPatternOptions(QRegularExpression::MultilineOption);
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString s_metric = match.captured(1);
QString s_value = match.captured(3);
QString s_mtr = "monit2influxdb,metric="+probe + "_" + s_metric + ",host=" + phost+ " value=" + s_value + " " + QString::number(1000000* QDateTime::currentMSecsSinceEpoch());
qDebug() << "metric: " << s_mtr.toLower();
udpSocket->writeDatagram(s_mtr.toStdString().c_str(), QHostAddress::LocalHost, 9999);
}
udpSocket->close();
}
}
示例2: run
/**
* @brief TransmitterTask::run
*/
void TransmitterTask::run()
{
QUdpSocket socket;
socket.bind(port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint);
socket.writeDatagram(data, address, port);
socket.waitForBytesWritten();
socket.close();
qDebug() << "Task:" << address << ':' << port << "<-" << data;
}
示例3: bindMode
void tst_QUdpSocket::bindMode()
{
QFETCH_GLOBAL(bool, setProxy);
if (setProxy) {
#ifdef TEST_QNETWORK_PROXY
QFETCH_GLOBAL(int, proxyType);
if (proxyType == QNetworkProxy::Socks5Proxy) {
QSKIP("With socks5 explicit port binding is not supported.", SkipAll);
}
#endif
}
QUdpSocket socket;
QVERIFY2(socket.bind(), socket.errorString().toLatin1().constData());
QUdpSocket socket2;
QVERIFY(!socket2.bind(socket.localPort()));
#ifdef Q_OS_UNIX
QVERIFY(!socket2.bind(socket.localPort(), QUdpSocket::ReuseAddressHint));
socket.close();
QVERIFY2(socket.bind(0, QUdpSocket::ShareAddress), socket.errorString().toLatin1().constData());
QVERIFY2(socket2.bind(socket.localPort()), socket2.errorString().toLatin1().constData());
socket2.close();
QVERIFY2(socket2.bind(socket.localPort(), QUdpSocket::ReuseAddressHint), socket2.errorString().toLatin1().constData());
#else
// Depending on the user's privileges, this or will succeed or
// fail. Admins are allowed to reuse the address, but nobody else.
if (!socket2.bind(socket.localPort(), QUdpSocket::ReuseAddressHint), socket2.errorString().toLatin1().constData())
qWarning("Failed to bind with QUdpSocket::ReuseAddressHint, user isn't an adminstrator?");
socket.close();
QVERIFY2(socket.bind(0, QUdpSocket::ShareAddress), socket.errorString().toLatin1().constData());
QVERIFY(!socket2.bind(socket.localPort()));
socket.close();
QVERIFY2(socket.bind(0, QUdpSocket::DontShareAddress), socket.errorString().toLatin1().constData());
QVERIFY(!socket2.bind(socket.localPort()));
QVERIFY(!socket2.bind(socket.localPort(), QUdpSocket::ReuseAddressHint));
#endif
}
示例4: loadURL
bool CQMedia::loadURL(const QString& url)
{
QString tmp = url;
tmp.remove(0,6);
QString ip = tmp.split(QChar(':'))[0];
int port = tmp.split(QChar(':'))[1].toInt();
QUdpSocket udp;
udp.bind(port,QUdpSocket::ShareAddress);
udp.joinMulticastGroup(QHostAddress(ip));
QEventLoop q;
QTimer::singleShot(1000,&q,SLOT(quit()));
connect(&udp,SIGNAL(readyRead()),&q,SLOT(quit()));
q.exec();
if(udp.pendingDatagramSize() == -1)
{
//qDebug("has no avalid datapkg...");
QTimer::singleShot(3500,this,SLOT(_emitTimeout()));
return false;
}
qDebug("has avalid datapkg...");
udp.leaveMulticastGroup(QHostAddress(ip));
udp.close();
this->url = url;
// SdpsrcParser sdpSrc(url);
// if(!sdpSrc.isValid())
// {
// qDebug()<<"SdpsrcParser invalid!!";
// return false;
// }
// sdpSrc.printf();
GstElement *udpsrc = gst_bin_get_by_name(GST_BIN(pipeline),"udpsrc");
GstCaps *caps = gst_caps_new_simple("application/x-rtp",NULL);
//QString uri = "udp://"+sdpSrc.broadcastIP+":"+QString::number(sdpSrc.v_port);
// QString uri = "udp://"+sdpSrc.broadcastIP+":"+QString::number(sdpSrc.v_port);
// QString uri = "udp://239.255.42.42:1234";
QString uri = url;
g_object_set(G_OBJECT(udpsrc),"uri",uri.toUtf8().data(),"caps",caps,NULL);
gst_object_unref(udpsrc);
return true;
}
示例5: close
void ServerPool::close(void)
{
QTcpServer *server;
while (!m_tcpServers.isEmpty())
{
server = m_tcpServers.takeLast();
server->disconnect();
server->close();
delete server;
}
QUdpSocket *socket;
while (!m_udpSockets.isEmpty())
{
socket = m_udpSockets.takeLast();
socket->disconnect();
socket->close();
delete socket;
}
}
示例6: broadcastToNetwork
//I'm in a new network, let's be polite and introduce myself
void LanLinkProvider::broadcastToNetwork()
{
if (!mServer->isListening()) {
//Not started
return;
}
Q_ASSERT(mTcpPort != 0);
qCDebug(KDECONNECT_CORE()) << "Broadcasting identity packet";
QHostAddress destAddress = mTestMode? QHostAddress::LocalHost : QHostAddress(QStringLiteral("255.255.255.255"));
NetworkPackage np(QLatin1String(""));
NetworkPackage::createIdentityPackage(&np);
np.set(QStringLiteral("tcpPort"), mTcpPort);
#ifdef Q_OS_WIN
//On Windows we need to broadcast from every local IP address to reach all networks
QUdpSocket sendSocket;
for (const QNetworkInterface &iface : QNetworkInterface::allInterfaces()) {
if ( (iface.flags() & QNetworkInterface::IsUp)
&& (iface.flags() & QNetworkInterface::IsRunning)
&& (iface.flags() & QNetworkInterface::CanBroadcast)) {
for (const QNetworkAddressEntry &ifaceAddress : iface.addressEntries()) {
QHostAddress sourceAddress = ifaceAddress.ip();
if (sourceAddress.protocol() == QAbstractSocket::IPv4Protocol && sourceAddress != QHostAddress::LocalHost) {
qCDebug(KDECONNECT_CORE()) << "Broadcasting as" << sourceAddress;
sendSocket.bind(sourceAddress, PORT);
sendSocket.writeDatagram(np.serialize(), destAddress, PORT);
sendSocket.close();
}
}
}
}
#else
mUdpSocket.writeDatagram(np.serialize(), destAddress, PORT);
#endif
}
示例7: close
void ServerPool::close(void)
{
QTcpServer *server;
while (!m_tcpServers.isEmpty())
{
server = m_tcpServers.takeLast();
server->disconnect();
server->close();
server->deleteLater();
}
QUdpSocket *socket;
while (!m_udpSockets.isEmpty())
{
socket = m_udpSockets.takeLast();
socket->disconnect();
socket->close();
socket->deleteLater();
}
m_listening = false;
}
示例8: findHPSDRDevices
int Discoverer::findHPSDRDevices() {
int devicesFound = 0;
m_findDatagram.resize(63);
m_findDatagram[0] = (char)0xEF;
m_findDatagram[1] = (char)0xFE;
m_findDatagram[2] = (char)0x02;
for (int i = 3; i < 63; i++)
m_findDatagram[i] = (char)0x00;
QUdpSocket socket;
CHECKED_CONNECT(
&socket,
SIGNAL(error(QAbstractSocket::SocketError)),
this,
SLOT(displayDiscoverySocketError(QAbstractSocket::SocketError)));
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "using " << qPrintable(QHostAddress(set->getHPSDRDeviceLocalAddr()).toString()) << " for discovery.";
io->networkIOMutex.unlock();
// clear comboBox entries in the network dialogue
set->clearNetworkIOComboBoxEntry();
#if defined(Q_OS_WIN32)
if (socket.bind(
QHostAddress(set->getHPSDRDeviceLocalAddr()), 0,
QUdpSocket::ReuseAddressHint | QUdpSocket::ShareAddress))
//QUdpSocket::ReuseAddressHint))
{
set->setMetisPort(this, socket.localPort());
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "discovery_socket bound successfully to port " << socket.localPort();
io->networkIOMutex.unlock();
}
else {
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "discovery_socket bind failed.";
io->networkIOMutex.unlock();
socket.close();
return 0;
}
#elif defined(Q_OS_LINUX)
if (socket.bind(
QHostAddress(set->getHPSDRDeviceLocalAddr()),
QUdpSocket::DefaultForPlatform))
{
CHECKED_CONNECT(
&socket,
SIGNAL(error(QAbstractSocket::SocketError)),
this,
SLOT(displayDiscoverySocketError(QAbstractSocket::SocketError)));
set->setMetisPort(this, socket.localPort());
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "discovery_socket bound successfully to port " << socket.localPort();
io->networkIOMutex.unlock();
}
else {
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "discovery_socket bind failed.";
io->networkIOMutex.unlock();
socket.close();
return 0;
}
#endif
if (socket.writeDatagram(m_findDatagram, QHostAddress::Broadcast, DEVICE_PORT) == 63) {
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "discovery data sent.";
io->networkIOMutex.unlock();
}
else {
io->networkIOMutex.lock();
DISCOVERER_DEBUG << "discovery data not sent.";
io->networkIOMutex.unlock();
}
// wait a little
//SleeperThread::msleep(30);
SleeperThread::msleep(500);
while (socket.hasPendingDatagrams()) {
TNetworkDevicecard mc;
quint16 port;
m_deviceDatagram.resize(socket.pendingDatagramSize());
socket.readDatagram(m_deviceDatagram.data(), m_deviceDatagram.size(), &mc.ip_address, &port);
//.........这里部分代码省略.........
示例9: main
//.........这里部分代码省略.........
if(tcp) {
QTcpSocket sock;
if(ipv6) {
sock.bind(QHostAddress::AnyIPv6, bind);
} else {
sock.bind(QHostAddress::AnyIPv4, bind);
}
sock.connectToHost(addy, port);
sock.waitForConnected(1000);
if(sock.state() == QAbstractSocket::ConnectedState)
{
OUTIF() << "TCP (" <<sock.localPort() <<")://" << address << ":" << port << " " << dataString;
bytesWriten = sock.write(sendData);
sock.waitForBytesWritten(1000);
//OUTIF() << "Sent:" << Packet::byteArrayToHex(sendData);
if(wait) {
sock.waitForReadyRead(wait);
recvData = sock.readAll();
bytesRead = recvData.size();
QString hexString = Packet::byteArrayToHex(recvData);
if(quiet) {
o << "\n" << hexString;
} else {
o << "\nResponse Time:" << QDateTime::currentDateTime().toString(DATETIMEFORMAT);
o << "\nResponse HEX:" << hexString;
o << "\nResponse ASCII:" << Packet::hexToASCII(hexString);
}
}
sock.disconnectFromHost();
sock.waitForDisconnected(1000);
sock.close();
OUTPUT();
return bytesWriten;
} else {
OUTIF() << "Error: Failed to connect to " << address;
OUTPUT();
return -1;
}
} else {
QUdpSocket sock;
if(ipv6) {
if(!sock.bind(QHostAddress::AnyIPv6, bind)) {
OUTIF() << "Error: Could not bind to " << bind;
OUTPUT();
return -1;
}
} else {
if(!sock.bind(QHostAddress::AnyIPv4, bind)) {
OUTIF() << "Error: Could not bind to " << bind;
OUTPUT();
return -1;
}
}
示例10: 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;
}