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);}
说明:
- 我都是在自己的机器上测试,所以服务器地址都是localHost。即
udpSocket->bind(QHostAddress::LocalHost, 7755);
- 给客户端回发信息
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);}}说明:
- 绑定本地地址和端口,以接收客户端发过来的信息
QHostAddress sender = udpSocket->localAddress();quint16 senderPort = udpSocket->localPort();udpSocket->bind(sender,senderPort);
- QUdpSocket 简单用法
- QUdpSocket实现简单通信
- 简单的通信(QUdpSocket、 QTcpSocket、QWebSocket)-----Qt网络
- QUdpSocket Class
- 初识QUdpsocket
- QUdpSocket简单的使用(局域网内发送给所有客户端消息与接收)
- QUdpSocket相关 (1)
- qt4 udp QUdpSocket -------wireshark
- QUdpSocket的问题
- Qt(3)QUdpsocket
- QUdpSocket进行网络传输
- QT QUdpSocket广播
- QUdpSocket使用整理
- QT QUdpSocket类
- QUdpSocket傳輸圖片QImage(QPixmap…
- QUdpSocket傳輸圖片QImage(QPixmap…
- 使用QUdpSocket进行UDP编程
- QUdpSocket和QTcpSocket的使用
- Git: Basics
- GAMBI2.4
- VTune工具使用心得
- hibernate ...many2one
- 网络编程——阻塞模式
- QUdpSocket 简单用法
- 为 TortoiseGit 和 TortoiseSVN 设置上网代理服务器
- 软件开发文档规范
- 自动化的货运系统(发明畅想)
- java内存区域
- vtune使用笔记
- CXF WebService整合Spring
- VTune安装
- 天气信息接口