实现UDP通信:
1,客户端只要声明一个QUdpSocket的实例,便开可开始发送数据。
类似:
udpSock->writeDatagram(msg.toLatin1(),msg.length(),QHostAddress::LocalHost,port);
2,服务端先要绑定端口,然后才能接收数据:
udpSock=new QUdpSocket(this);
udpSock->bind(port);
connect(udpSock,SIGNAL(readyRead()),this,SLOT(dataReceived()));
…
void Widget::dataReceived()
{
while(udpSock->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSock->pendingDatagramSize());
udpSock->readDatagram(datagram.data(),datagram.size());
QString msg=datagram.data();
ui->textEdit->insertPlainText(msg);
}
}