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 这是我的<通信网络>的课程设计作业,之 ...
随机推荐
- ImageView 最大bitmap 4096
ImageView 最大bitmap 4096,超出不显示图片
- C#整数三种强制类型转换int、Convert.ToInt32()、int.Parse()、string到object 的区别
1.int适合简单数据类型之间的转换,C#的默认整型是int32(不支持bool型); 2.int.Parse(string sParameter)是个构造函数,参数类型只支持string类型; 3. ...
- (原创)用C++11的std::async代替线程的创建
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + ); t.join(); 但是线程毕竟是属于比较 ...
- linux 命令(43):bash 快捷键操作
ctrl 命令行快捷键: CTRL+A: – 快速移动到行首. 我们假设你输入了像下面这样的命令.当你在第 N 行时,你发现在行首字符有一个输入错误 $ gind . -mtime -1 -type ...
- Spark SQL利器:cacheTable/uncacheTable【转】
转自:http://www.cnblogs.com/yurunmiao/p/4936583.html Spark相对于Hadoop MapReduce有一个很显著的特性就是“迭代计算”(作为一个Map ...
- VS2015打开项目慢、死机和卡顿解决方案
VS2015功能强大,但是经常慢 网上有解决方案如下几个,留着备用: 1.选项-环境-基于客户端性能自动调整视觉体验,去掉选项. 2.选项-源代码管理-插件选无. 3.选项-文本编辑器-所有语言-滚动 ...
- Mysql查看连接数相关信息
MySQL查看连接数相关信息在 数据库:INFORMATION_SCHEMA 表:PROCESSLIST 表结构如下: mysql> desc PROCESSLIST; +---------+- ...
- CentOS7 yum 安装mysql 5.6
原文:https://www.cnblogs.com/zhangyin6985/p/6307219.html 转自:http://www.centoscn.com/mysql/2016/0315/68 ...
- IE11 不能正常方法网页
由于将IE11升级到了 11 之前的网站无法正常使用, 如果是开发人员碰到之问题,使用了微软的asp.net 控件,那么将服务器的.net framework 升级到 4.5 http://www.m ...
- ADO.NET 实体数据模型 异常-“序列化类型为 XX 的对象时检测到循环引用”
发生异常的代码如下: 1: public JsonResult GetSaleByNo1(string id) 2: { 3: SaleMvcUI.Helper.saleDBEntities sale ...