发送端
1. 创建套接字
QUdpSocket mSocket;
mSocket = new QUdpSocket();
2. 发送数据到指定的端口和地址
mSocket->writeDatagram();
接收端
1. 创建套接字
QUdpSocket mSocket;
mSocket = new QUdpSocket();
2. 绑定地址和端口号
connect(mSocket,SIGNAL(readyRead()),this,SLOT(read_data()));
3. 等待数据的到来
void UdpRecv::read_data()
{
QByteArray array;
QHostAddress address;
quint16 port;
array.resize(mSocket->bytesAvailable());
mSocket->readDatagram(array.data(),array.size(),&address,&port);
ui->listWidget->addItem(array);
}
4. 读取数据
readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)
|