QUdpSocket 简单用法
2013-05-29 00:01
447 查看
UDP是无连接的不可靠的传输协议,可以用在可靠传输不是很重要的情况下使用。 QUdpSocket是QAbstractSocket 的子类,它们都继承了QIODevice。所以可以用QUdpSocket进行发送接收数据。它和QTcpSocket最大的区别也就是,发送数据之前不需要建立连接。以下简单例子,演示了用QUdpSocket如何实现客户端和服务端的通信。
服务端代码:
说明:
我都是在自己的机器上测试,所以服务器地址都是localHost。即
给客户端回发信息
客户端代码:
绑定本地地址和端口,以接收客户端发过来的信息
演示结果图:
服务端代码:
class UDPServer:public QObject { Q_OBJECT public: 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_OBJECT public: 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);
演示结果图:
相关文章推荐
- 封装一个简单的Windows UDP socket 网络类
- Java&&Android Socket简单用法
- Linux socket编程示例(最简单的TCP和UDP两个例子)
- java中ServerSocket和Socket的简单用法
- CAsysncSocket 之--udp工具--AsyncUdpSocket 用法
- Socket简单用法
- 基于socket的TCP和UDP通讯的简单建立
- TCP与UDP的简单发送与实现(socket)
- android-->Socket简单用法
- UDP Socket基本思路小程序(2) 多线程简单聊天程序
- java网络编程一:Socket用法,简单模拟一对一聊天
- Java 网络编程 简单Socket(UDP)
- Linux C UDP Socket实现客户与服务器简单通信
- socket UDP简单通讯
- 【android】Socket简单用法
- 【android】Socket简单用法
- socket编程:简单UDP服务器/客户端编程
- Socket TCP_UDP简单实例
- linux简单的TCP与UDP的socket程序以及机器大小端的判断程序
- android-->Socket简单用法