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 这是我的<通信网络>的课程设计作业,之 ...
随机推荐
- Error:Cause: org/gradle/api/publication/maven/internal/DefaultMavenFactory Android
首先,要看一下自己的项目使用 “Gradle版本” 接着要看一下项目根目录的build.gradle文件中的“dependencies”的 classpath 'com.github.dcendent ...
- 【Unity】3.0 第3章 创建和导入3D模型
分类:Unity.C#.VS2015 创建日期:2016-04-02 一.简介 利用Unity内置的基本模型和工具,不需要借助任何其他的三维建模软件,就可以直接创建出各种3D模型,这是这一章我们首先学 ...
- 菜鸟学Java(九)——Servlet的基本配置
学习JavaWeb的人没有不知道Servlet的吧,而要用Servlet就需要在web.xml中进行配置.相信有很多初学者跟我当初一样,对于一些配置参数不是很理解,今天就说说Servlet最基本的配置 ...
- std::bind技术内幕
引子 最近群里比较热闹,大家都在山寨c++11的std::bind,三位童孩分别实现了自己的bind,代码分别在这里: 木头云的实现 mr.li的实现 null的实现,null的另一个版本的实现 这些 ...
- MonoBehaviour类Invoke, Coroutine
异步函数 在一个方法执行时调用另一个方法.而被调用的方法或者其中的某些语句不是立刻执行,而是过一段时间后才执行. MonoBehaviour提供了两种异步方法 调用(Invoke) 协程(Corout ...
- iOS-通讯录(无界面)
通讯录(无界面) #import "AppDelegate.h" #import <AddressBook/AddressBook.h> @interface AppD ...
- 【ActiveMQ】Spring Jms集成ActiveMQ学习记录
Spring Jms集成ActiveMQ学习记录. 引入依赖包 无论生产者还是消费者均引入这些包: <properties> <spring.version>3.0.5.REL ...
- 【ARM】ARM体系结构-GPIO
GPIO Gerneral-Purpose IO ports,即通用IO口. 在嵌入式系统中常常有数量众多,但是却比较简单的外部设备/电路. 对这些设备/电路,有的需要CPU为之提供控制手段,有 ...
- 如何解压POSIX tar archive文件
下载了一个xxx.gz的文件,使用x xxx.gz(zsh的x插件,十分之好用,再也不用担心tar后面该加哪些参数了)的命令解压,然后出现了一个文件,本以为解压后是一个文件夹:然后一脸蒙逼~ 突然又想 ...
- Python利用jieba获取中文词汇等
import jieba import os import jieba.analyse data = cleaned_comments # 数据来源于评论数据 seg = jieba.lcut(dat ...