这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