QAbstractSocket Class | Qt 4.8 (original) (raw)
The QAbstractSocket class provides the base functionality common to all socket types. More...
Member Function Documentation
QAbstractSocket::QAbstractSocket(SocketType socketType, QObject * parent)
Creates a new abstract socket of type socketType. The parent argument is passed to QObject's constructor.
See also socketType(), QTcpSocket, and QUdpSocket.
[virtual] QAbstractSocket::~QAbstractSocket()
Destroys the socket.
void QAbstractSocket::abort()
Aborts the current connection and resets the socket. Unlike disconnectFromHost(), this function immediately closes the socket, discarding any pending data in the write buffer.
See also disconnectFromHost() and close().
[virtual] bool QAbstractSocket::atEnd() const
Reimplemented from QIODevice::atEnd().
Returns true if no more data is currently available for reading; otherwise returns false.
This function is most commonly used when reading data from the socket in a loop. For example:
void SocketClass::readyReadSlot() { while (!socket.atEnd()) { QByteArray data = socket.read(100); .... } }
See also bytesAvailable() and readyRead().
[virtual] qint64 QAbstractSocket::bytesAvailable() const
Reimplemented from QIODevice::bytesAvailable().
Returns the number of incoming bytes that are waiting to be read.
See also bytesToWrite() and read().
[virtual] qint64 QAbstractSocket::bytesToWrite() const
Reimplemented from QIODevice::bytesToWrite().
Returns the number of bytes that are waiting to be written. The bytes are written when control goes back to the event loop or when flush() is called.
See also bytesAvailable() and flush().
[virtual] bool QAbstractSocket::canReadLine() const
Reimplemented from QIODevice::canReadLine().
Returns true if a line of data can be read from the socket; otherwise returns false.
See also readLine().
[virtual] void QAbstractSocket::close()
Reimplemented from QIODevice::close().
Closes the I/O device for the socket, disconnects the socket's connection with the host, closes the socket, and resets the name, address, port number and underlying socket descriptor.
See QIODevice::close() for a description of the actions that occur when an I/O device is closed.
See also abort().
void QAbstractSocket::connectToHost(const QString & hostName, quint16 port, OpenMode openMode = ReadWrite)
Attempts to make a connection to hostName on the given port.
The socket is opened in the given openMode and first enters HostLookupState, then performs a host name lookup of hostName. If the lookup succeeds, hostFound() is emitted and QAbstractSocket enters ConnectingState. It then attempts to connect to the address or addresses returned by the lookup. Finally, if a connection is established, QAbstractSocket enters ConnectedState and emits connected().
At any point, the socket can emit error() to signal that an error occurred.
hostName may be an IP address in string form (e.g., "43.195.83.32"), or it may be a host name (e.g., "example.com"). QAbstractSocket will do a lookup only if required. port is in native byte order.
See also state(), peerName(), peerAddress(), peerPort(), and waitForConnected().
void QAbstractSocket::connectToHost(const QHostAddress & address, quint16 port, OpenMode openMode = ReadWrite)
This is an overloaded function.
Attempts to make a connection to address on port port.
[protected slot] void QAbstractSocket::connectToHostImplementation(const QString & hostName, quint16 port, OpenMode openMode = ReadWrite)
Contains the implementation of connectToHost().
Attempts to make a connection to hostName on the given port. The socket is opened in the given openMode.
This function was introduced in Qt 4.1.
[signal] void QAbstractSocket::connected()
This signal is emitted after connectToHost() has been called and a connection has been successfully established.
Note: On some operating systems the connected() signal may be directly emitted from the connectToHost() call for connections to the localhost.
See also connectToHost() and disconnected().
void QAbstractSocket::disconnectFromHost()
Attempts to close the socket. If there is pending data waiting to be written, QAbstractSocket will enter ClosingState and wait until all data has been written. Eventually, it will enter UnconnectedState and emit the disconnected() signal.
See also connectToHost().
[protected slot] void QAbstractSocket::disconnectFromHostImplementation()
Contains the implementation of disconnectFromHost().
This function was introduced in Qt 4.1.
[signal] void QAbstractSocket::disconnected()
This signal is emitted when the socket has been disconnected.
Warning: If you need to delete the sender() of this signal in a slot connected to it, use the deleteLater() function.
See also connectToHost(), disconnectFromHost(), and abort().
SocketError QAbstractSocket::error() const
Returns the type of error that last occurred.
See also state() and errorString().
[signal] void QAbstractSocket::error(QAbstractSocket::SocketError socketError)
This signal is emitted after an error occurred. The socketError parameter describes the type of error that occurred.
QAbstractSocket::SocketError is not a registered metatype, so for queued connections, you will have to register it with Q_DECLARE_METATYPE() and qRegisterMetaType().
**Note:**Signal error is overloaded in this class. To connect to this one using the function pointer syntax, you must specify the signal type in a static cast, as shown in this example:
See also error(), errorString(), and Creating Custom Qt Types.
bool QAbstractSocket::flush()
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. If any data was written, this function returns true; otherwise false is returned.
Call this function if you need QAbstractSocket to start sending buffered data immediately. The number of bytes successfully written depends on the operating system. In most cases, you do not need to call this function, because QAbstractSocket will start sending data automatically once control goes back to the event loop. In the absence of an event loop, call waitForBytesWritten() instead.
See also write() and waitForBytesWritten().
[signal] void QAbstractSocket::hostFound()
This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
Note: Since Qt 4.6.3 QAbstractSocket may emit hostFound() directly from the connectToHost() call since a DNS result could have been cached.
See also connected().
[virtual] bool QAbstractSocket::isSequential() const
Reimplemented from QIODevice::isSequential().
bool QAbstractSocket::isValid() const
Returns true if the socket is valid and ready for use; otherwise returns false.
Note: The socket's state must be ConnectedState before reading and writing can occur.
See also state().
QHostAddress QAbstractSocket::localAddress() const
Returns the host address of the local socket if available; otherwise returns QHostAddress::Null.
This is normally the main IP address of the host, but can be QHostAddress::LocalHost (127.0.0.1) for connections to the local host.
See also localPort(), peerAddress(), and setLocalAddress().
quint16 QAbstractSocket::localPort() const
Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0.
See also localAddress(), peerPort(), and setLocalPort().
QHostAddress QAbstractSocket::peerAddress() const
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHostAddress::Null.
See also peerName(), peerPort(), localAddress(), and setPeerAddress().
QString QAbstractSocket::peerName() const
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() has not been called.
See also peerAddress(), peerPort(), and setPeerName().
quint16 QAbstractSocket::peerPort() const
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0.
See also peerAddress(), localPort(), and setPeerPort().
QNetworkProxy QAbstractSocket::proxy() const
Returns the network proxy for this socket. By default QNetworkProxy::DefaultProxy is used, which means this socket will query the default proxy settings for the application.
This function was introduced in Qt 4.1.
See also setProxy(), QNetworkProxy, and QNetworkProxyFactory.
[signal] void QAbstractSocket::proxyAuthenticationRequired(const QNetworkProxy & proxy, QAuthenticator * authenticator)
This signal can be emitted when a proxy that requires authentication is used. The authenticator object can then be filled in with the required details to allow authentication and continue the connection.
Note: It is not possible to use a QueuedConnection to connect to this signal, as the connection will fail if the authenticator has not been filled in with new information when the signal returns.
This function was introduced in Qt 4.3.
See also QAuthenticator and QNetworkProxy.
qint64 QAbstractSocket::readBufferSize() const
Returns the size of the internal read buffer. This limits the amount of data that the client can receive before you call read() or readAll().
A read buffer size of 0 (the default) means that the buffer has no size limit, ensuring that no data is lost.
See also setReadBufferSize() and read().
[virtual protected] qint64 QAbstractSocket::readData(char * data, qint64 maxSize)
Reimplemented from QIODevice::readData().
[virtual protected] qint64 QAbstractSocket::readLineData(char * data, qint64 maxlen)
Reimplemented from QIODevice::readLineData().
[protected] void QAbstractSocket::setLocalAddress(const QHostAddress & address)
Sets the address on the local side of a connection to address.
You can call this function in a subclass of QAbstractSocket to change the return value of the localAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
Note that this function does not bind the local address of the socket prior to a connection (e.g., QUdpSocket::bind()).
This function was introduced in Qt 4.1.
See also localAddress(), setLocalPort(), and setPeerAddress().
[protected] void QAbstractSocket::setLocalPort(quint16 port)
Sets the port on the local side of a connection to port.
You can call this function in a subclass of QAbstractSocket to change the return value of the localPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
Note that this function does not bind the local port of the socket prior to a connection (e.g., QUdpSocket::bind()).
This function was introduced in Qt 4.1.
See also localPort(), localAddress(), setLocalAddress(), and setPeerPort().
[protected] void QAbstractSocket::setPeerAddress(const QHostAddress & address)
Sets the address of the remote side of the connection to address.
You can call this function in a subclass of QAbstractSocket to change the return value of the peerAddress() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
This function was introduced in Qt 4.1.
See also peerAddress(), setPeerPort(), and setLocalAddress().
[protected] void QAbstractSocket::setPeerName(const QString & name)
Sets the host name of the remote peer to name.
You can call this function in a subclass of QAbstractSocket to change the return value of the peerName() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
This function was introduced in Qt 4.1.
See also peerName().
[protected] void QAbstractSocket::setPeerPort(quint16 port)
Sets the port of the remote side of the connection to port.
You can call this function in a subclass of QAbstractSocket to change the return value of the peerPort() function after a connection has been established. This feature is commonly used by proxy connections for virtual connection settings.
This function was introduced in Qt 4.1.
See also peerPort(), setPeerAddress(), and setLocalPort().
void QAbstractSocket::setProxy(const QNetworkProxy & networkProxy)
Sets the explicit network proxy for this socket to networkProxy.
To disable the use of a proxy for this socket, use the QNetworkProxy::NoProxy proxy type:
The default value for the proxy is QNetworkProxy::DefaultProxy, which means the socket will use the application settings: if a proxy is set with QNetworkProxy::setApplicationProxy, it will use that; otherwise, if a factory is set with QNetworkProxyFactory::setApplicationProxyFactory, it will query that factory with type QNetworkProxyQuery::TcpSocket.
This function was introduced in Qt 4.1.
See also proxy(), QNetworkProxy, and QNetworkProxyFactory::queryProxy().
void QAbstractSocket::setReadBufferSize(qint64 size)
Sets the size of QAbstractSocket's internal read buffer to be size bytes.
If the buffer size is limited to a certain size, QAbstractSocket won't buffer more than this size of data. Exceptionally, a buffer size of 0 means that the read buffer is unlimited and all incoming data is buffered. This is the default.
This option is useful if you only read the data at certain points in time (e.g., in a real-time streaming application) or if you want to protect your socket against receiving too much data, which may eventually cause your application to run out of memory.
Only QTcpSocket uses QAbstractSocket's internal buffer; QUdpSocket does not use any buffering at all, but rather relies on the implicit buffering provided by the operating system. Because of this, calling this function on QUdpSocket has no effect.
See also readBufferSize() and read().
bool QAbstractSocket::setSocketDescriptor(int socketDescriptor, SocketState socketState = ConnectedState, OpenMode openMode = ReadWrite)
Initializes QAbstractSocket with the native socket descriptor socketDescriptor. Returns true if socketDescriptor is accepted as a valid socket descriptor; otherwise returns false. The socket is opened in the mode specified by openMode, and enters the socket state specified by socketState.
Note: It is not possible to initialize two abstract sockets with the same native socket descriptor.
See also socketDescriptor().
[protected] void QAbstractSocket::setSocketError(SocketError socketError)
Sets the type of error that last occurred to socketError.
See also setSocketState() and setErrorString().
void QAbstractSocket::setSocketOption(QAbstractSocket::SocketOption option, const QVariant & value)
Sets the given option to the value described by value.
This function was introduced in Qt 4.6.
See also socketOption().
[protected] void QAbstractSocket::setSocketState(SocketState state)
Sets the state of the socket to state.
See also state().
int QAbstractSocket::socketDescriptor() const
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise returns -1.
If the socket is using QNetworkProxy, the returned descriptor may not be usable with native socket functions.
The socket descriptor is not available when QAbstractSocket is in UnconnectedState.
See also setSocketDescriptor().
QVariant QAbstractSocket::socketOption(QAbstractSocket::SocketOption option)
Returns the value of the option option.
This function was introduced in Qt 4.6.
See also setSocketOption().
SocketType QAbstractSocket::socketType() const
Returns the socket type (TCP, UDP, or other).
See also QTcpSocket and QUdpSocket.
SocketState QAbstractSocket::state() const
Returns the state of the socket.
See also error().
[signal] void QAbstractSocket::stateChanged(QAbstractSocket::SocketState socketState)
This signal is emitted whenever QAbstractSocket's state changes. The socketState parameter is the new state.
QAbstractSocket::SocketState is not a registered metatype, so for queued connections, you will have to register it with Q_REGISTER_METATYPE() and qRegisterMetaType().
See also state() and Creating Custom Qt Types.
[virtual] bool QAbstractSocket::waitForBytesWritten(int msecs = 30000)
Reimplemented from QIODevice::waitForBytesWritten().
bool QAbstractSocket::waitForConnected(int msecs = 30000)
Waits until the socket is connected, up to msecs milliseconds. If the connection has been established, this function returns true; otherwise it returns false. In the case where it returns false, you can call error() to determine the cause of the error.
The following example waits up to one second for a connection to be established:
If msecs is -1, this function will not time out.
Note: This function may wait slightly longer than msecs, depending on the time it takes to complete the host lookup.
Note: Multiple calls to this functions do not accumulate the time. If the function times out, the connecting process will be aborted.
See also connectToHost() and connected().
bool QAbstractSocket::waitForDisconnected(int msecs = 30000)
Waits until the socket has disconnected, up to msecs milliseconds. If the connection has been disconnected, this function returns true; otherwise it returns false. In the case where it returns false, you can call error() to determine the cause of the error.
The following example waits up to one second for a connection to be closed:
If msecs is -1, this function will not time out.
See also disconnectFromHost() and close().
[virtual] bool QAbstractSocket::waitForReadyRead(int msecs = 30000)
Reimplemented from QIODevice::waitForReadyRead().
This function blocks until new data is available for reading and the readyRead() signal has been emitted. The function will timeout after msecs milliseconds; the default timeout is 30000 milliseconds.
The function returns true if the readyRead() signal is emitted and there is new data available for reading; otherwise it returns false (if an error occurred or the operation timed out).
See also waitForBytesWritten().
[virtual protected] qint64 QAbstractSocket::writeData(const char * data, qint64 size)
Reimplemented from QIODevice::writeData().