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 这是我的<通信网络>的课程设计作业,之 ...
随机推荐
- 设置 debug 版本签名与生产版本一致
debug 版本使用生产版本的签名 在开发过程中,app 直接跑到手机上,用的签名文件是 Android Studio 默认的自动生成的一个签名,与生产版本的 app 签名是不一样的.当接入华为推送的 ...
- Android 7.0 出现 ”FileUriExposedException“ 和 ”解析包出现错误“ 异常的解决办法
问题1 :android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.xxx.xxx.xxx.rel ...
- 【Java】日志知识总结和经常使用组合配置(commons-logging,log4j,slf4j,logback)
Log4j Apache的一个开放源码项目,通过使用Log4j,我们能够控制日志信息输送的目的地是控制台.文件.GUI组件.甚至是套接口服务 器.NT的事件记录器.UNIX Syslog守护进程等.用 ...
- Egret入门了解
0.前言 这个星期没有什么事做,就想找点技术了解一下.前段时间看过Egret,用来开发HTML5小游戏.一开始以为很麻烦的,但是经过这两天了解了一下,如果用这个游戏引擎来开发一些简单的游戏,还是蛮方便 ...
- 如何理解java泛型类
//泛型代码 public class Pair<T>{ private T first=null; private T second=null; public Pair(T fir,T ...
- Quartz.Net定时任务EF+MVC版的web服务
之前项目采用JAVA 的 Quartz 进行定时服调度务处理程序,目前在.NET下面使用依然可以完成相同的工作任务,其实什么语言不重要,关键是我们要学会利用语言实现价值.它是一个简单的执行任务计划的组 ...
- angular学习笔记(三十)-指令(2)-restrice,replace,template
本篇主要讲解指令中的 restrict属性, replace属性, template属性 这三个属性 一. restrict: 字符串.定义指令在视图中的使用方式,一共有四种使用方式: 1. 元素: ...
- Mac下用brew搭建PHP(LNMP/LAMP)开发环境
Mac下搭建lamp开发环境很容易,有xampp和mamp现成的集成环境.但是集成环境对于经常需要自定义一些配置的开发者来说会非常麻烦,而且Mac本身自带apache和php,在brew的帮助下非常容 ...
- 【C/C++】程序如何来,编译全过程
概述 编译的目的是把人书写的高级语言代码翻译成目标程序的语言处理程序,编译用的程序(例如gcc)称为编译系统. 一个编译系统把一个源程序翻译成目标程序的工作过程分为5个阶段:词法分析.语法分析. ...
- Python3之pymysql导入mysql
$cat insert.py #!/usr/bin/python # -*- coding: UTF-8 -*- import os import sys import datetime import ...