QUdpSocket Class
QUdpSocket类提供一个UDP套接字。
| Header: | #include <QUdpSocket> |
| qmake: | QT += network |
| Inherits: | QAbstractSocket. |
注意:这个类的所有函数都是不可重入的(reentrant)。
公有函数:
|
QUdpSocket(QObject * parent = 0) |
|
| virtual | ~QUdpSocket() |
| bool | hasPendingDatagrams() const |
| bool |
joinMulticastGroup(const QHostAddress & groupAddress) |
| bool |
joinMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & iface) |
| bool |
leaveMulticastGroup(const QHostAddress & groupAddress) |
| bool |
leaveMulticastGroup(const QHostAddress & groupAddress, const QNetworkInterface & iface) |
| QNetworkInterface | multicastInterface() const |
| qint64 | pendingDatagramSize() const |
| qint64 |
readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0) |
| void |
setMulticastInterface(const QNetworkInterface & iface) |
| qint64 |
writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port) |
| qint64 |
writeDatagram(const QByteArray & datagram, const QHostAddress & host, quint16 port) |
37公有函数继承自QAbstractSocket
33公有函数继承自QIODevice
31公有函数继承自QObject
其他的继承的成员:
- 1 property inherited from QObject
- 1 public slot inherited from QObject
- 6 signals inherited from QAbstractSocket
- 4 signals inherited from QIODevice
- 2 signals inherited from QObject
- 1 public variable inherited from QObject
- 10 static public members inherited from QObject
- 10 protected functions inherited from QAbstractSocket
- 5 protected functions inherited from QIODevice
- 9 protected functions inherited from QObject
- 2 protected variables inherited from QObject
QUdpSocket类提供一个UDP套接字。
UDP(用户数据报协议)是一个轻量级的,不可靠的,面向数据包的无连接的协议。当可靠性不重要的时候,可以使用UDP。QUdpSocket是QAbstractSocket的一个子类,可以用来收发数据报。
这个类最多的用途就是使用bind()绑定一个地址和端口,然后调用writeDatagram()和readDatagram()来传递数据。如果你想用标准QIODevice类的方法read(),readLine(),write()等,你必须首先直接连接到套接字,通过调用connectToHost()函数连接一个同等的用户(peer)。
每次有数据报写到网络中时,套接字发射bytesWritten()信号。如果你只是想发送数据报,你不需要调用bind()函数。
当数据报到达的时候,readyRead()信号被发射。这样,hasPendingDatagrams()返回true。调用pendingDatagramSize()来获得第一个接收到的数据报的大小,使用readDatagram()来读取数据报。
注意:当你接收到readyRead()信号时,到达的数据报必须被读取,否则,信号不会发射给下个数据报。
示例:
void Server::initSocket()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(QHostAddress::LocalHost, 7755); connect(udpSocket, SIGNAL(readyRead()),
this, SLOT(readPendingDatagrams()));
} void Server::readPendingDatagrams()
{
while (udpSocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort; udpSocket->readDatagram(datagram.data(), datagram.size(),
&sender, &senderPort); processTheDatagram(datagram);
}
}
QUdpSocket also supports UDP multicast. Use joinMulticastGroup()
and leaveMulticastGroup()
to control group membership,
and QAbstractSocket::MulticastTtlOption andQAbstractSocket::MulticastLoopbackOption to
set the TTL and loopback socket options. UsesetMulticastInterface()
to control the outgoing interface for multicast datagrams, andmulticastInterface()
to query it.
使用UdpSocket,你可以使用connectToHost()建立一个到UDP服务器的虚拟连接,而且不用指定每条报文的接受者就可以使用read()和write()来交换报文。
The Broadcast Sender, Broadcast
Receiver, Multicast Sender, and Multicast
Receiver examples illustrate how to use QUdpSocket in applications.
See also QTcpSocket.
成员函数文档:
QUdpSocket::QUdpSocket(QObject * parent =
0)
创建一个QUdpSocket对象。
parent继承自QObject的构造函数。
参考socketType()。
QUdpSocket::~QUdpSocket() [virtual]
销毁套接字,如有必要,关闭连接。
参考close()。
bool QUdpSocket::hasPendingDatagrams() const
如果至少有一个报文等到被读,返回true;否则,返回false。
参考pendingDatagramSize()和readDatagram()。
bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress)
Joins the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState,
otherwise an error occurs.
Note that if you are attempting to join an IPv4 group, your socket must not be bound using IPv6 (or in dual mode, using QHostAddress::Any).
You must use QHostAddress::AnyIPv4 instead.
This function returns true if successful; otherwise it returns false and
sets the socket error accordingly.
This function was introduced in Qt 4.8.
See also leaveMulticastGroup().
bool QUdpSocket::joinMulticastGroup(const QHostAddress & groupAddress,
const QNetworkInterface & iface)
This is an overloaded function.
Joins the multicast group address groupAddress on the interface iface.
This function was introduced in Qt 4.8.
See also leaveMulticastGroup().
bool QUdpSocket::leaveMulticastGroup(const QHostAddress & groupAddress)
Leaves the multicast group specified by groupAddress on the default interface chosen by the operating system. The socket must be in BoundState,
otherwise an error occurs.
This function returns true if successful; otherwise it returns false and
sets the socket error accordingly.
This function was introduced in Qt 4.8.
See also joinMulticastGroup().
bool QUdpSocket::leaveMulticastGroup(const QHostAddress & groupAddress,
const QNetworkInterface & iface)
This is an overloaded function.
Leaves the multicast group specified by groupAddress on the interface iface.
This function was introduced in Qt 4.8.
See also joinMulticastGroup().
QNetworkInterface QUdpSocket::multicastInterface()
const
Returns the interface for the outgoing interface for multicast datagrams. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. If no interface has been previously set, this function
returns an invalid QNetworkInterface. The socket must be in BoundState,
otherwise an invalid QNetworkInterface is returned.
This function was introduced in Qt 4.8.
See also setMulticastInterface().
qint64 QUdpSocket::pendingDatagramSize() const
返回第一个待定的报文的大小。如果没有报文待定,返回-1.
参考hasPendingDatagrams()readDatagram()。
qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize, QHostAddress* address =
0, quint16 * port = 0)
接收一个不超过maxsize字节的报文,存到data中。发送者的主机地址和端口号存进*address和*port中(除非指针是0)
返回数据报成功的大小;否则返回-1.
如果maxsize太小,剩下的报文会丢失。为避免数据的丢失,在试图读取之前,调用pendingDatagramSize()来决定待定数据报的大小。如果maxsize是0,数据报将被丢弃。
参考writeDatagram(), hasPendingDatagrams(),
and pendingDatagramSize().
void QUdpSocket::setMulticastInterface(const QNetworkInterface & iface)
Sets the outgoing interface for multicast datagrams to the interface iface. This corresponds to the IP_MULTICAST_IF socket option for IPv4 sockets and the IPV6_MULTICAST_IF socket option for IPv6 sockets. The socket
must be in BoundState, otherwise this function does nothing.
This function was introduced in Qt 4.8.
See also multicastInterface(), joinMulticastGroup(),
and leaveMulticastGroup().
qint64 QUdpSocket::writeDatagram(const char * data, qint64 size,
constQHostAddress & address, quint16 port)
发送size字节大小的数据报给address地址和port端口的主机。返回成功发送的字节数,否则返回-1.
数据报总是被写成一块。数据报的最大大小是高度平台相关的,但是可以低至8192字节。如果报文太大,这个函数会返回-1并且error()会返回DatagramTooLargeError。
发送数据报大于512个字节是不鼓励的,即使成功发送,它们在到达目的地之前也可能被IP层分裂。
警告:在一个连接的UDP套接字上调用这个函数可能产生一个错误,导致没有数据包被发送。如果你使用了连接的套接字,使用write()来发送数据报。
参考readDatagram()
and write().
qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
constQHostAddress & host, quint16 port)
这是一个重载函数。
发送datagram数据报给地址是host,端口是port的主机。
QUdpSocket Class的更多相关文章
- Qt中QUdpSocket序列化问题
写了一个小的Qt网络程序,很简单,发送的网络消息除了字符串还有一个结构体.很简单的想到用memcpy()函数来将数据序列化为BYTE数组从而实现网络传输. 序列化是Java中一个概念,C中并没有,C+ ...
- 5.关于QT中的网络编程,QTcpSocket,QUdpSocket
1 新建一个项目:TCPServer.pro A 修改TCPServer.pro,注意:如果是想使用网络库,需要加上network SOURCES += \ TcpServer.cpp \ T ...
- QT 使用QUdpSocket QUdpServer UDP 建立客户端与服务器端
1. 模拟天气监控,每隔两秒从Server发送天气信息到Client. 2. 示例代码 --------------------------- Server 端 ------------------- ...
- C/C++学习)22.QTcpServer、QTcpSocket、QUdpSocket使用
一.TCP/UDP通信在Qt中的实现过程: 废话不说,首先下面是Qt中TCP/UDP的实现图解: 1.Qt下TCP通信详解: 针对上图进行简单的说明: QTcpServer用来创建服务 ...
- QT下UDP套接字通信——QUdpSocket 简单使用
QT下UDP套接字通信--QUdpSocket QUdpSocket类提供一个UDP套接字. UDP(用户数据报协议)是一种轻量级.不可靠.面向数据报.无连接的协议.它可以在可靠性不重要的情况下使用. ...
- Qt下的udp编程
项目需要一个基于udp的客户端, 看着Qt是有个QUdpSocket的类的, 但自带的例子和类的说明都没咋说明白: 怎么用一个QUdpSocket既当发送端, 又当接收端? 谷歌一顿后, 发现很多老内 ...
- Qt on Android 核心编程
Qt on Android 核心编程(最好看的Qt编程书!CSDN博主foruok倾力奉献!) 安晓辉 著 ISBN 978-7-121-24457-5 2015年1月出版 定价:65.00元 4 ...
- Qt 多线程和网络编程学习
一,Qt多线程类学习 QThread类,开始一个新的线程就是开始执行重新实现QThread::run(),run()是默认现实调用exec(),QThread::start()开始线程的执行,run( ...
- QT UDP聊天小程序
利用QT的UDP技术,实现两个QT程序之间的聊天程序. #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include < ...
随机推荐
- mac下显示隐藏文件
一.在终端中 ls -a就可以查看隐藏文件. 显示和隐藏的命令例如以下: 显示:defaults write com.apple.finder AppleShowAllFiles -bool true ...
- 基于阿里云server搭建SVNserver
基于阿里云server搭建SVNserver 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/details/8577 ...
- iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程
iOS开发CoreAnimation解读之一——初识CoreAnimation核心动画编程 一.引言 二.初识CoreAnimation 三.锚点对几何属性的影响 四.Layer与View之间的关系 ...
- ASP.NET导入Excel到SQL数据库
protected void btnChange_Click(object sender, EventArgs e) { UserInfoClass tClass = (UserInfoClass)S ...
- 动态设置Head的Title、Descrption
HtmlMeta desc = new HtmlMeta(); desc.Name = "Description"; desc.Content = strTitle.ToStrin ...
- 微软TTS,Neospeech TTS 简单使用
今天搞了下微软的TTS,逛了好多网页.博客,拼拼凑凑搞了点东西吧. 首先添加类库调用,系统自带的system.speech using System.Speech.Synthesis; 然后就能调用方 ...
- Android Studio ---------------- 软件使用小细节(更新中。。。。。。)
###鼠标放到相关类或方法等上,没有提示. *解决方法:File----Setting-----Editor-----General------Show quik documentation on m ...
- Qt Creator下载
Qt官网 http://qt.digia.com qt的历史版本可以到http://download.qt-project.org/archive/qt/下载
- (转) Friendship and inheritance
原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...
- self和this的不同
在Java和C++中,this总是指的是当前实例地址,而在静态方法也就是类方法中,是不可以使用this的.在Objectvie-C中,self是既可以出现在实例方法中,也可以出现在类方法中,并且在不同 ...