qt 的udp发送和接收
udp要在.pro中加入
QT += network
udp接收
//QUdpSocket *udp1;
udp1 = new QUdpSocket(this);
udp1->bind(2024,QUdpSocket::ShareAddress);
connect(udp1,SIGNAL(readyRead()),this,SLOT(ReadyOut()));
void MainWindow::ReadyOut()
{
while(udp1->hasPendingDatagrams())
{
QByteArray b;
int len = udp1->pendingDatagramSize();
b.resize(len);
udp1->readDatagram(b.data(),len);
ui->label_2->setText(b.data());
}
}
udp发送:
QString str1 ="abc";
QString str=str1+QString::number(i);
QByteArray b=str.toUtf8();
udp1->writeDatagram(b,QHostAddress("127.0.0.1"),2025);
完整软件路径:
https://download.csdn.net/download/wyyy2088511/90053680