欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

柚子快報(bào)激活碼778899分享:QT Udp廣播實(shí)現(xiàn)設(shè)備發(fā)現(xiàn)

柚子快報(bào)激活碼778899分享:QT Udp廣播實(shí)現(xiàn)設(shè)備發(fā)現(xiàn)

http://yzkb.51969.com/

測(cè)試環(huán)境

本文選用pc1作為客戶端,pc2,以及一臺(tái)虛擬機(jī)作為服務(wù)端。

pc1,pc2(客戶端): 虛擬機(jī)(服務(wù)端):

客戶端

原理:客戶端通過(guò)發(fā)送廣播消息信息到ip:255.255.255.255(QHostAddress::Broadcast),局域網(wǎng)內(nèi)的所有設(shè)備收到該消息回復(fù)客戶端即可??蛻舳送ㄟ^(guò)收到的回復(fù)統(tǒng)計(jì)當(dāng)前有哪些設(shè)備在線。獲取到本地的IP,getLocalIP函數(shù)獲取到過(guò)濾了虛擬機(jī)網(wǎng)卡以及本地回環(huán)網(wǎng)卡后的ip地址。

#include "udpclient.h"

#include

#include

#include

#include

udpClient::udpClient(QObject *parent) : QObject(parent)

{

QString localIp = getLocalIP();

udpSocket = new QUdpSocket;

udpSocket->bind(QHostAddress(localIp),2001);

connect(udpSocket,&QUdpSocket::readyRead,this,&udpClient::processData);

}

QString udpClient::getLocalIP() {

QList interfaces = QNetworkInterface::allInterfaces();

foreach (const QNetworkInterface &interface, interfaces) {

QList entries = interface.addressEntries();

qDebug()<<"name:"<

if(interface.humanReadableName().contains("Loopback") ||

interface.humanReadableName().contains("VMware Network Adapter"))

{

continue;

}

foreach (const QNetworkAddressEntry &entry, entries) {

if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {

qDebug() << "Local IP Address: " << entry.ip().toString()<< endl;

}

}

}

return QString();

}

udpClient::~udpClient()

{

if(udpSocket)

{

delete udpSocket;

}

}

void udpClient::sendBroadCast()

{

QByteArray datagram = "Device Discovery";

udpSocket->writeDatagram(datagram,QHostAddress::Broadcast,8888);

}

void udpClient::processData()

{

while(udpSocket->hasPendingDatagrams())

{

QByteArray datagram;

datagram.resize(udpSocket->pendingDatagramSize());

QHostAddress sender;

quint16 senderPort;

udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

qDebug() << "Received response from: " << sender.toString()<<"port:"<

}

}

服務(wù)端

#include "udpserver.h"

#include

udpServer::udpServer(QObject *parent) : QObject(parent)

{

udpSocket = new QUdpSocket(this);

udpSocket->bind(QHostAddress::Any, 8888);

connect(udpSocket, &QUdpSocket::readyRead, this, &udpServer::processPendingDatagrams);

}

void udpServer::processPendingDatagrams()

{

while (udpSocket->hasPendingDatagrams()) {

QByteArray datagram;

datagram.resize(udpSocket->pendingDatagramSize());

QHostAddress sender;

quint16 senderPort;

udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

std::cout << "Received discovery message: " << datagram.data() << std::endl;

QByteArray response = "Device Found";

udpSocket->writeDatagram(response, sender, senderPort);

}

}

輸出效果

優(yōu)化

對(duì)客戶端增加定時(shí)器,同時(shí)將客戶端對(duì)象移動(dòng)到一個(gè)線程中,這樣就可以定時(shí)輪詢?cè)O(shè)備發(fā)現(xiàn)了。

#include "udpclient.h"

#include

#include

#include

#include

#include

#include

udpClient::udpClient(QObject *parent) : QObject(parent)

{

qDebug()<<"thread id1:"<

}

void udpClient::createSocket()

{

qDebug()<<"thread id2:"<

QString localIp = getLocalIP();

udpSocket = new QUdpSocket;

udpSocket->bind(QHostAddress(localIp),2001);

connect(udpSocket,&QUdpSocket::readyRead,this,&udpClient::processData);

}

QString udpClient::getLocalIP() {

QList interfaces = QNetworkInterface::allInterfaces();

foreach (const QNetworkInterface &interface, interfaces) {

QList entries = interface.addressEntries();

qDebug()<<"name:"<

if(interface.humanReadableName().contains("Loopback") ||

interface.humanReadableName().contains("VMware Network Adapter"))

{

continue;

}

foreach (const QNetworkAddressEntry &entry, entries) {

if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol) {

qDebug() << "Local IP Address: " << entry.ip().toString()<< endl;

}

}

}

return QString();

}

udpClient::~udpClient()

{

if(udpSocket)

{

delete udpSocket;

}

}

void udpClient::sendBroadCast()

{

QByteArray datagram = "Device Discovery";

udpSocket->writeDatagram(datagram,QHostAddress::Broadcast,8888);

qDebug()<<"sendBroadCast,thread id:"<

}

void udpClient::processData()

{

while(udpSocket->hasPendingDatagrams())

{

QByteArray datagram;

datagram.resize(udpSocket->pendingDatagramSize());

QHostAddress sender;

quint16 senderPort;

udpSocket->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort);

result.push_back(IpInfo(sender.toString(),senderPort));

qDebug() << "Received response from: " << sender.toString()<<"port:"<

}

}

void tcpConnect(QString& ip, quint16 port)

{

}

#include "widget.h"

#include "ui_widget.h"

#include

#include

Widget::Widget(QWidget *parent)

: QWidget(parent)

, ui(new Ui::Widget)

{

ui->setupUi(this);

client = new udpClient;

connect(ui->pushButton,&QPushButton::clicked,client,&udpClient::sendBroadCast);

thread = new QThread;

connect(thread,&QThread::finished,client,&QObject::deleteLater);

connect(thread,&QThread::started,client,&udpClient::createSocket);

client->moveToThread(thread);

timer = new QTimer(this);

connect(timer,&QTimer::timeout,client,&udpClient::sendBroadCast);

timer->setInterval(500);

thread->start();

// QEventLoop loop;

// QTimer::singleShot(500,&loop,&QEventLoop::quit);

// loop.exec();

timer->start();

qDebug()<<"thread id:"<

}

Widget::~Widget()

{

delete ui;

thread->quit();

thread->wait();

delete thread;

thread=nullptr;

}

柚子快報(bào)激活碼778899分享:QT Udp廣播實(shí)現(xiàn)設(shè)備發(fā)現(xiàn)

http://yzkb.51969.com/

相關(guān)文章

評(píng)論可見(jiàn),查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。

轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://m.gantiao.com.cn/post/19544974.html

發(fā)布評(píng)論

您暫未設(shè)置收款碼

請(qǐng)?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問(wèn)

文章目錄