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 socketTypeQObject *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 & addressquint16port = 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 &hostNamequint16 portOpenMode 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 *dataqint64 size)

寫數據

 

轉自:http://blog.csdn.net/u014660247/article/details/52491257


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM