QUdpSocket实现简单通信
2017-07-28 16:15
489 查看
利用QUdpSocket实现简单的UDP通信功能,需要添加Network模块,.cpp文件代码:
#include "QtGuiApplication1.h" #include QtGuiApplication1::QtGuiApplication1(QWidget *parent) : QMainWindow(parent) { ui.setupUi(this); udpSocket = new QUdpSocket(this); udpSocket->bind(8000, QUdpSocket::DefaultForPlatform);//默认模式绑定8000端口 connect(udpSocket, SIGNAL(readyRead()), this, SLOT(OnReadyReadMsg())); } //接收数据槽函数 void QtGuiApplication1::OnReadyReadMsg() { while (udpSocket->hasPendingDatagrams())//缓冲区有数据等待被读取 { int size = udpSocket->pendingDatagramSize(); char* pBuffer = new char[size+1];//分配报文大小的内存 memset(pBuffer, 0, size + 1); udpSocket->readDatagram(pBuffer, size); /* 开始处理数据 */ } } //发送数据到本机8001端口 void QtGuiApplication1::SendMsg(const char* pData,int length) { QHostAddress address = QHostAddress("127.0.0.1");//本机地址 quint16 port = 8001;//发送端口 udpSocket->writeDatagram(pData, length,address, port);//将data中的数据发送 }
相关文章推荐
- JAVA系列课程讲座二:使用Socket通信实现简单聊天通信程序(UDP方式)
- linux网络编程之用socket实现简单客户端和服务端的通信(基于UDP)
- socket编程实现一个简单的多人通信(udp)
- Linux C UDP Socket实现客户与服务器简单通信
- Java 实现基于 UDP 的简单 socket 通信
- python socket实现简单的(TCP/UDP)服务器/客户端
- 用Socket实现TCP,UDP通信
- C语言实现socket简单通信实例
- socket 实例实现简单通信
- Android简单实现Socket通信,客户端连接服务器后,服务器向客户端发送文字数据
- socket的UDP通信简单例子
- C++ 简单的 Tcp 实现[socket] 客户端与客户端通信
- linux网络编程之用socket实现简单客户端和服务端的通信(基于TCP)
- 利用socket TCP协议实现客户端与服务端简单通信
- socket编程 -- 基于UDP协议的C/S通信模型及实现
- socket实现UDP通信
- 基于socket简单通信协议实现(c/c++)
- 如何实现基于UDP的socket的通信
- socket通信的简单实现
- LinuxC之socket通信实现简单的计算器