Qt网络与通信-UDP
2016-04-10 19:12
357 查看
实现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);
}
}
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);
}
}
相关文章推荐
- HttpClient设置HTTP请求头Header
- apache的HttpComponents项目的介绍和简单使用
- 第五届华中区程序设计邀请赛 网络赛Calculation E 【状压dp】
- TCP协议的学习(三)TCP协议三次握手及攻击
- JavaWeb总结(二)—HttpServletResponse对象
- 【安卓网络请求开源框架Volley源码解析系列】初识Volley及其基本用法
- 【安卓网络请求开源框架Volley源码解析系列】初识Volley及其基本用法
- 复习笔记——计算机网络
- 机器学习:贝叶斯_3:贝叶斯网络
- HTTP 1.1与HTTP 1.0的比较
- 加载网络图片
- Java网络编程小结 URLConnection协议处理器
- robotframework使用RequestsLibrary进行http接口测试
- iis_kangle_https压力测试
- Linux C语言 网络编程(二) 服务器模型
- iOS学习笔记14-网络(三)WebView
- 第五届华中区程序设计邀请赛 网络赛 Calculation A 【前缀和】
- ZOJ3229 Shoot the Bullet(有源汇流量有上下界网络的最大流)
- RTSP协议、RTMP协议、HTTP协议的区别
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)