qt之QAbstractSocket
这QAbstractSocket 类提供了整个socket的类型,是QTcpSocket和QUdpSocket的基类
创建一个本体套接字,可以调用QAbstractSocket 和 setSocketDescriptor()去包裹一个本地套接字
这个类竟可能的联合了TCP和UDP,尽管UDP是不可靠的连接,但是connectToHost()为UDP建立了一个假的连接,使其步骤尽量与TCP协议相似。在本质上QAbstractSocket通过调用connectToHost()记住了地址和端口,而QAbstratSocket中的成员函数 read和write将会使用保存的这个值
无论在什么时候QAbstractSocket都有一个状态,而我们可以通过调用成员函数state返回这个状态,才开始的状态是 UnconnectedState,
当程序调用了connectToHost之后,QAbstractSocket的状态会变成HostLookupState,,
如果主机被找到,QAbstaractSocket进入connectingState状态并且发射HostFound()信号,当连接被建立的时候QAbstractSocket 进入了connectedState状态 并且发射connected()信号,如果再这些阶段出现了错误,QAbstractSocket将会发射error()信号,无论在什么时候,如果状态改变了,都会发射stateChanged(),如果套接字准备好了读写数据,isValid()将会返回true
State UnconnectState HostLookUpState --(找到了host)--> connectingState---建立了连接---> connectedState
connectToHost()
信号 Emit hostFound() emit conncted()
无论什么时候 QAbstractSocket的状态改变都会发射stateChanged()信号,无论什么时候只要发生了错误,就会发送error()信号。
读写数据
向套接字里读写数据通过调用 read或者是write函数 ,当数据被写进套接字后会发射bytesWritten()信号。需要注意的是,qt没有限制写缓冲区的大小,因此我们可以监听这个信号,以此来获得写缓冲区的大小
当一块数据到达时候,readRead()信号将会被发送,通过调用bytesAliable()函数可以得到可读的数量,因此我们可以连接readRead()信号连接到槽,以此来读取数据,如果不以此读取完毕,剩下的数据还是有效的。,如果要限制读缓冲区可以调用 setReadBufferSize().
关闭连接
关闭一个连接可以调用disconnectFromHost(),这个时候QAbstractSocket会进入closingstate状态,当将数据全部发送完之后,QAbstaractSocket会进入closedState状态,并且发送disconnected()信号。
而对于每个连接我们可以通过调用 peerPort() peerAddress() peerName 返回每个连接的名字,地址,端口,而我们可以通过调用localPort()和localAddress返回本地端口和地址
下列函数可以被用来实现一个阻塞套接字,并且挂起调用线程
waitForConnected 阻塞直到连接被建立
waitForReadyRead() 阻塞直到新的数据到达
waitForBytesWritten() 阻塞直到数据写入完毕
waitForDisconnected() 阻塞直到连接被关闭。
相关API
QAbstractSocket::QAbstractSocket(SocketType socketType, QObject *parent)
创建一个套接字
enum QAbstractSocket::SocketType
This enum describes the transport layer protocol.
Constant |
Value |
Description |
QAbstractSocket::TcpSocket |
0 |
TCP |
QAbstractSocket::UdpSocket |
1 |
UDP |
QAbstractSocket::UnknownSocketType |
-1 |
Other than TCP and UDP |
QAbstractSocket::~QAbstractSocket()
销毁套接字
void QAbstractSocket::abort()
立即终止连接,丢掉还未发送的数据
bool QAbstractSocket::atEnd() const
如果没有更多的数据可读则返回true
bool QAbstractSocket::bind(const QHostAddress & address, quint16port = 0, BindMode mode = DefaultForPlatform)
绑定地址和套接字
对于udp socket , 当有数据报到达的时候,会发送readyRead().,成功返回true,失败返回false
Constant |
Value |
Description |
QAbstractSocket::ShareAddress |
0x1 |
Allow other services to bind to the same address and port. This is useful when multiple processes share the load of a single service by listening to the same address and port (e.g., a web server with several pre-forked listeners can greatly improve response time). However, because any service is allowed to rebind, this option is subject to certain security considerations. Note that by combining this option with ReuseAddressHint, you will also allow your service to rebind an existing shared address. On Unix, this is equivalent to the SO_REUSEADDR socket option. On Windows, this option is ignored. |
QAbstractSocket::DontShareAddress |
0x2 |
Bind the address and port exclusively, so that no other services are allowed to rebind. By passing this option toQAbstractSocket::bind(), you are guaranteed that on successs, your service is the only one that listens to the address and port. No services are allowed to rebind, even if they pass ReuseAddressHint. This option provides more security than ShareAddress, but on certain operating systems, it requires you to run the server with administrator privileges. On Unix and OS X, not sharing is the default behavior for binding an address and port, so this option is ignored. On Windows, this option uses the SO_EXCLUSIVEADDRUSE socket option. |
QAbstractSocket::ReuseAddressHint |
0x4 |
Provides a hint to QAbstractSocket that it should try to rebind the service even if the address and port are already bound by another socket. On Windows, this is equivalent to the SO_REUSEADDR socket option. On Unix, this option is ignored. |
QAbstractSocket::DefaultForPlatform |
0x0 |
The default option for the current platform. On Unix and OS X, this is equivalent to (DontShareAddress + ReuseAddressHint), and on Windows, its equivalent to ShareAddress. |
qint64 QAbstractSocket::bytesAvailable() const
返回可读取数据的数量
qint64 QAbstractSocket::bytesToWrite() const
返回等待被发送数据的数量
bool QAbstractSocket::canReadLine() const
如果可以从套接字读取一行则返回true,否则返回false.
void QAbstractSocket::close()
关闭套接字
void QAbstractSocket::connectToHost(const QString &hostName, quint16 port, OpenMode openMode = ReadWrite,NetworkLayerProtocol protocol = AnyIPProtocol)
建立一个连接
void QAbstractSocket::disconnectFromHost()
销毁一个连接,会等待数据发送完毕。
SocketError QAbstractSocket::error() const
返回最后一次出现的错误。
bool QAbstractSocket::flush()
刷新写缓冲区
QHostAddress QAbstractSocket::localAddress() const
返回本地地址
qint64 QAbstractSocket::readData(char * data,qint64 maxSize)
读取数据
void QAbstractSocket::setPeerAddress(constQHostAddress & address)
设置连接远程的地址
void QAbstractSocket::setProxy(const QNetworkProxy &networkProxy)
设置代理
void QAbstractSocket::setReadBufferSize(qint64 size)
设置读缓冲区的大小
qint64 QAbstractSocket::writeData(const char *data, qint64 size)
写数据
转自:http://blog.csdn.net/u014660247/article/details/52491257
qt之QAbstractSocket的更多相关文章
- Qt 菜鸟的坑 QAbstractSocket::isValid()
我曾经多次在 Qt socket 编程中使用 tcpSocket.isValid 来判断我当前的连接是否可用,最近写程序时才发现此法并不妥当. bool QAbstractSocket::isVali ...
- Qt 多线程和网络编程学习
一,Qt多线程类学习 QThread类,开始一个新的线程就是开始执行重新实现QThread::run(),run()是默认现实调用exec(),QThread::start()开始线程的执行,run( ...
- qt 获取当前主机的信息
随着科技的发展,嵌入式技术在生活中越来越扮演者重要的角色,小到智能手环.手机,大到智能家居.汽车,都和嵌入式技术息息相关.在嵌入式系统中,拥有良好的用户界面会使产品更具市场优势.最近正好有机会用qt做 ...
- Qt之获取本机网络信息(MAC, IP等等,很全)
经常使用命令行来查看一些计算机的配置信息. 1.首先按住键盘上的“开始键+R键”,然后在弹出的对话框中输入“CMD”,回车 另外,还可以依次点击 开始>所有程序>附件>命令提示符 2 ...
- Qt之QHostInfo
简述 QHostInfo 类为主机名查找提供了静态函数. QHostInfo 利用操作系统提供的查询机制来查询与特定主机名相关联的主机的 IP 地址,或者与一个IP地址相关联的主机名.这个类提供了两个 ...
- Qt实现应用程序单实例运行--LocalServer方式
使Qt应用程序能够单实例运行的典型实现方法是使用共享内存实现.该方法实现简单,代码简洁. 但有一个致命缺陷:共享内存(QSharedMemory)实现的单程序运行,当运行环境是UNIX时,并且程序不幸 ...
- Qt之运行一个实例进程
简述 发布程序的时候,我们往往会遇到这种情况: 只需要用户运行一个实例进程 用户可以同时运行多个实例进程 一个实例进程的软件有很多,例如:360.酷狗- 多个实例进程的软件也很多,例如:Visual ...
- 【Qt】QT5 获取IP地址
QT获取本机IP地址 #include <QtNetwork/QHostAddress> #include <QtNetwork/QNetworkInterface> #inc ...
- 基于Qt的P2P局域网聊天及文件传送软件设计
基于Qt的P2P局域网聊天及文件传送软件设计 zouxy09@qq.com http://blog.csdn.net/zouxy09 这是我的<通信网络>的课程设计作业,之 ...
随机推荐
- JAVA中INSTANCEOF关键字的用法总结
https://www.cnblogs.com/jay36/p/7519145.html https://www.cnblogs.com/zjxynq/p/5882756.html https://b ...
- hdu1542 Atlantis (线段树+扫描线+离散化)
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- spark join
https://jaceklaskowski.gitbooks.io/mastering-apache-spark/content/spark-sql-joins.html https://acadg ...
- 每日英语:The Power of Parents Who Say 'No'
I grew up in an affluent area. Most kids owned multiple Cabbage Patch dolls and Gear bags and pairs ...
- (精品)微信支付android端
PayingActivity.java public void WxPay(){ // new Thread(new Runnable() { // @Override // public void ...
- 国内Docker下载镜像提速方法之一
众所周知,Docker Hub并没有在国内部署服务器或者使用国内的CDN服务,因此在国内特殊的网络环境下,镜像下载十分耗时.为了克服跨洋网络延迟,能够快速高效地下载Docker镜像,我采用了DaoCl ...
- Wireshark抓包工具HttpAnalyzerStdV7
http.request.full_uri contains "XXXserver/api/" http.host contains "XXX5.单位.com"
- WPF学习笔记(1)——image控件图片不显示的问题
说明(2017-6-7 16:08:35): 1. 本来想用winform的,用winform就没这么多破事了. 2. 不过项目要用WPF,拉出一个mediaelement控件,视频正常显示. 3. ...
- laravel中facade serviceprovider的理解
一个serviceprovider就是一个解决某个功能的公用模块,实际上可以直接用在di里注册然后从di中取出,为啥还要搞个facade呢? 有几个方面的原因 1.把实例化移入到serviceprov ...
- [转]ssh中如何实现定时任务(spring对quartz的支持)
原文地址:http://blog.csdn.net/qq_18675693/article/details/50413889 实现定时任务:quartz spring中对quartz进行了封装,使得我 ...