QUdpSocket 简单用法

来源:互联网 发布:ubuntu grub 修复 编辑:程序博客网 时间:2024/06/05 09:34

     UDP是无连接的不可靠的传输协议,可以用在可靠传输不是很重要的情况下使用。 QUdpSocket是QAbstractSocket 的子类,它们都继承了QIODevice。所以可以用QUdpSocket进行发送接收数据。它和QTcpSocket最大的区别也就是,发送数据之前不需要建立连接。以下简单例子,演示了用QUdpSocket如何实现客户端和服务端的通信。

服务端代码:

class UDPServer:public QObject{Q_OBJECTpublic:UDPServer(QObject *parent = NULL);~UDPServer();private slots:void readPendingDatagrams();private:QUdpSocket           *udpSocket;};

UDPServer::UDPServer(QObject *parent /* = NULL */):QObject(parent){udpSocket = new QUdpSocket(this);udpSocket->bind(QHostAddress::LocalHost, 7755);cout<<"Server is Running......"<<endl;connect(udpSocket, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));}UDPServer::~UDPServer(){}void UDPServer::readPendingDatagrams(){QHostAddress sender;quint16 senderPort;while (udpSocket->hasPendingDatagrams()) {QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());udpSocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);string strMes(datagram);std::cout<<strMes<<endl;}QString text = "hello ...";QByteArray datagram = text.toLocal8Bit();udpSocket->writeDatagram(datagram.data(),datagram.size(),sender, senderPort);}


说明:

  1.     我都是在自己的机器上测试,所以服务器地址都是localHost。即
    udpSocket->bind(QHostAddress::LocalHost, 7755);
  2. 给客户端回发信息
    udpSocket->writeDatagram(datagram.data(),datagram.size(),sender, senderPort);

客户端代码:

class UdpClient : public QWidget{Q_OBJECTpublic:UdpClient(QWidget *parent = 0, Qt::WFlags flags = 0);~UdpClient();private slots:void sendMessageSlot();void readPendingDatagrams();private:QUdpSocket     *udpSocket;QLineEdit      *m_pLEdit;QPushButton    *m_pSendMesBtn;QLabel         *m_pMessage;//Ui::UdpClientClass ui;};

UdpClient::UdpClient(QWidget *parent, Qt::WFlags flags): QWidget(parent, flags){m_pLEdit = new QLineEdit(this);m_pSendMesBtn = new QPushButton(tr("Sending"),this);udpSocket = new QUdpSocket(this);m_pMessage = new QLabel(this);QHostAddress sender = udpSocket->localAddress();quint16 senderPort = udpSocket->localPort();udpSocket->bind(sender,senderPort);m_pLEdit->setGeometry(5,5,100,20);m_pSendMesBtn->setGeometry(110,5,50,20);m_pMessage->setGeometry(5,30,150,20);m_pLEdit->setStyleSheet("QLineEdit{color:red}");connect(m_pSendMesBtn, SIGNAL(clicked()),this, SLOT(sendMessageSlot()));connect(udpSocket, SIGNAL(readyRead()),this, SLOT(readPendingDatagrams()));//ui.setupUi(this);}UdpClient::~UdpClient(){}void UdpClient::sendMessageSlot(){QString text = m_pLEdit->text();QByteArray datagram = text.toLocal8Bit();udpSocket->writeDatagram(datagram.data(),datagram.size(),QHostAddress::LocalHost, 7755);}void UdpClient::readPendingDatagrams(){while (udpSocket->hasPendingDatagrams()) {QByteArray datagram;datagram.resize(udpSocket->pendingDatagramSize());QHostAddress sender;quint16 senderPort;udpSocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);QString text = QString(datagram);m_pMessage->setText(text);}}
说明:

  1.  绑定本地地址和端口,以接收客户端发过来的信息
    QHostAddress sender = udpSocket->localAddress();quint16 senderPort = udpSocket->localPort();udpSocket->bind(sender,senderPort);
演示结果图:


原创粉丝点击