Qt 4.8
|
The QAbstractSocket class provides the base functionality common to all socket types. More...
#include <qabstractsocket.h>
Signals | |
void | connected () |
This signal is emitted after connectToHost() has been called and a connection has been successfully established. More... | |
void | disconnected () |
This signal is emitted when the socket has been disconnected. More... | |
void | error (QAbstractSocket::SocketError) |
This signal is emitted after an error occurred. More... | |
void | hostFound () |
This signal is emitted after connectToHost() has been called and the host lookup has succeeded. More... | |
void | proxyAuthenticationRequired (const QNetworkProxy &proxy, QAuthenticator *authenticator) |
void | stateChanged (QAbstractSocket::SocketState) |
This signal is emitted whenever QAbstractSocket's state changes. More... | |
Signals inherited from QIODevice | |
void | aboutToClose () |
This signal is emitted when the device is about to close. More... | |
void | bytesWritten (qint64 bytes) |
This signal is emitted every time a payload of data has been written to the device. More... | |
void | readChannelFinished () |
This signal is emitted when the input (reading) stream is closed in this device. More... | |
void | readyRead () |
This signal is emitted once every time new data is available for reading from the device. More... | |
Signals inherited from QObject | |
void | destroyed (QObject *=0) |
This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More... | |
Public Functions | |
void | abort () |
Aborts the current connection and resets the socket. More... | |
bool | atEnd () const |
Returns true if no more data is currently available for reading; otherwise returns false. More... | |
qint64 | bytesAvailable () const |
Returns the number of incoming bytes that are waiting to be read. More... | |
qint64 | bytesToWrite () const |
Returns the number of bytes that are waiting to be written. More... | |
bool | canReadLine () const |
Returns true if a line of data can be read from the socket; otherwise returns false. More... | |
void | 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. More... | |
void | connectToHost (const QString &hostName, quint16 port, OpenMode mode=ReadWrite) |
Attempts to make a connection to hostName on the given port. More... | |
void | connectToHost (const QHostAddress &address, quint16 port, OpenMode mode=ReadWrite) |
Attempts to make a connection to address on port port. More... | |
void | disconnectFromHost () |
Attempts to close the socket. More... | |
SocketError | error () const |
Returns the type of error that last occurred. More... | |
bool | flush () |
This function writes as much as possible from the internal write buffer to the underlying network socket, without blocking. More... | |
bool | isSequential () const |
Reimplemented Function More... | |
bool | isValid () const |
Returns true if the socket is valid and ready for use; otherwise returns false. More... | |
QHostAddress | localAddress () const |
Returns the host address of the local socket if available; otherwise returns QHostAddress::Null. More... | |
quint16 | localPort () const |
Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0. More... | |
QHostAddress | peerAddress () const |
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHostAddress::Null. More... | |
QString | peerName () const |
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() has not been called. More... | |
quint16 | peerPort () const |
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0. More... | |
QNetworkProxy | proxy () const |
Returns the network proxy for this socket. More... | |
QAbstractSocket (SocketType socketType, QObject *parent) | |
Creates a new abstract socket of type socketType. More... | |
qint64 | readBufferSize () const |
Returns the size of the internal read buffer. More... | |
void | setProxy (const QNetworkProxy &networkProxy) |
Sets the explicit network proxy for this socket to networkProxy. More... | |
void | setReadBufferSize (qint64 size) |
Sets the size of QAbstractSocket's internal read buffer to be size bytes. More... | |
bool | setSocketDescriptor (int socketDescriptor, SocketState state=ConnectedState, OpenMode openMode=ReadWrite) |
Initializes QAbstractSocket with the native socket descriptor socketDescriptor. More... | |
void | setSocketOption (QAbstractSocket::SocketOption option, const QVariant &value) |
Sets the given option to the value described by value. More... | |
int | socketDescriptor () const |
Returns the native socket descriptor of the QAbstractSocket object if this is available; otherwise returns -1. More... | |
QVariant | socketOption (QAbstractSocket::SocketOption option) |
Returns the value of the option option. More... | |
SocketType | socketType () const |
Returns the socket type (TCP, UDP, or other). More... | |
SocketState | state () const |
Returns the state of the socket. More... | |
bool | waitForBytesWritten (int msecs=30000) |
Reimplemented Function More... | |
bool | waitForConnected (int msecs=30000) |
Waits until the socket is connected, up to msecs milliseconds. More... | |
bool | waitForDisconnected (int msecs=30000) |
Waits until the socket has disconnected, up to msecs milliseconds. More... | |
bool | waitForReadyRead (int msecs=30000) |
This function blocks until new data is available for reading and the QIODevice::readyRead() signal has been emitted. More... | |
virtual | ~QAbstractSocket () |
Destroys the socket. More... | |
Public Functions inherited from QIODevice | |
QString | errorString () const |
Returns a human-readable description of the last device error that occurred. More... | |
bool | getChar (char *c) |
Reads one character from the device and stores it in c. More... | |
bool | isOpen () const |
Returns true if the device is open; otherwise returns false. More... | |
bool | isReadable () const |
Returns true if data can be read from the device; otherwise returns false. More... | |
bool | isTextModeEnabled () const |
Returns true if the Text flag is enabled; otherwise returns false. More... | |
bool | isWritable () const |
Returns true if data can be written to the device; otherwise returns false. More... | |
virtual bool | open (OpenMode mode) |
Opens the device and sets its OpenMode to mode. More... | |
OpenMode | openMode () const |
Returns the mode in which the device has been opened; i.e. More... | |
qint64 | peek (char *data, qint64 maxlen) |
Reads at most maxSize bytes from the device into data, without side effects (i. More... | |
QByteArray | peek (qint64 maxlen) |
Peeks at most maxSize bytes from the device, returning the data peeked as a QByteArray. More... | |
virtual qint64 | pos () const |
For random-access devices, this function returns the position that data is written to or read from. More... | |
bool | putChar (char c) |
Writes the character c to the device. More... | |
QIODevice () | |
Constructs a QIODevice object. More... | |
QIODevice (QObject *parent) | |
Constructs a QIODevice object with the given parent. More... | |
qint64 | read (char *data, qint64 maxlen) |
Reads at most maxSize bytes from the device into data, and returns the number of bytes read. More... | |
QByteArray | read (qint64 maxlen) |
Reads at most maxSize bytes from the device, and returns the data read as a QByteArray. More... | |
QByteArray | readAll () |
Reads all available data from the device, and returns it as a QByteArray. More... | |
qint64 | readLine (char *data, qint64 maxlen) |
This function reads a line of ASCII characters from the device, up to a maximum of maxSize - 1 bytes, stores the characters in data, and returns the number of bytes read. More... | |
QByteArray | readLine (qint64 maxlen=0) |
Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray. More... | |
virtual bool | reset () |
Seeks to the start of input for random-access devices. More... | |
virtual bool | seek (qint64 pos) |
For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred. More... | |
void | setTextModeEnabled (bool enabled) |
If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. More... | |
virtual qint64 | size () const |
For open random-access devices, this function returns the size of the device. More... | |
void | ungetChar (char c) |
Puts the character c back into the device, and decrements the current position unless the position is 0. More... | |
qint64 | write (const char *data, qint64 len) |
Writes at most maxSize bytes of data from data to the device. More... | |
qint64 | write (const char *data) |
Writes data from a zero-terminated string of 8-bit characters to the device. More... | |
qint64 | write (const QByteArray &data) |
Writes the content of byteArray to the device. More... | |
virtual | ~QIODevice () |
The destructor is virtual, and QIODevice is an abstract base class. More... | |
Public Functions inherited from QObject | |
bool | blockSignals (bool b) |
If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More... | |
const QObjectList & | children () const |
Returns a list of child objects. More... | |
bool | connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const |
bool | disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0) |
bool | disconnect (const QObject *receiver, const char *member=0) |
void | dumpObjectInfo () |
Dumps information about signal connections, etc. More... | |
void | dumpObjectTree () |
Dumps a tree of children to the debug output. More... | |
QList< QByteArray > | dynamicPropertyNames () const |
Returns the names of all properties that were dynamically added to the object using setProperty(). More... | |
virtual bool | event (QEvent *) |
This virtual function receives events to an object and should return true if the event e was recognized and processed. More... | |
virtual bool | eventFilter (QObject *, QEvent *) |
Filters events if this object has been installed as an event filter for the watched object. More... | |
template<typename T > | |
T | findChild (const QString &aName=QString()) const |
Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More... | |
template<typename T > | |
QList< T > | findChildren (const QString &aName=QString()) const |
Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More... | |
template<typename T > | |
QList< T > | findChildren (const QRegExp &re) const |
bool | inherits (const char *classname) const |
Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More... | |
void | installEventFilter (QObject *) |
Installs an event filter filterObj on this object. More... | |
bool | isWidgetType () const |
Returns true if the object is a widget; otherwise returns false. More... | |
void | killTimer (int id) |
Kills the timer with timer identifier, id. More... | |
virtual const QMetaObject * | metaObject () const |
Returns a pointer to the meta-object of this object. More... | |
void | moveToThread (QThread *thread) |
Changes the thread affinity for this object and its children. More... | |
QString | objectName () const |
QObject * | parent () const |
Returns a pointer to the parent object. More... | |
QVariant | property (const char *name) const |
Returns the value of the object's name property. More... | |
Q_INVOKABLE | QObject (QObject *parent=0) |
Constructs an object with parent object parent. More... | |
void | removeEventFilter (QObject *) |
Removes an event filter object obj from this object. More... | |
void | setObjectName (const QString &name) |
void | setParent (QObject *) |
Makes the object a child of parent. More... | |
bool | setProperty (const char *name, const QVariant &value) |
Sets the value of the object's name property to value. More... | |
void | setUserData (uint id, QObjectUserData *data) |
bool | signalsBlocked () const |
Returns true if signals are blocked; otherwise returns false. More... | |
int | startTimer (int interval) |
Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More... | |
QThread * | thread () const |
Returns the thread in which the object lives. More... | |
QObjectUserData * | userData (uint id) const |
virtual | ~QObject () |
Destroys the object, deleting all its child objects. More... | |
Protected Slots | |
void | connectToHostImplementation (const QString &hostName, quint16 port, OpenMode mode=ReadWrite) |
Contains the implementation of connectToHost(). More... | |
void | disconnectFromHostImplementation () |
Contains the implementation of disconnectFromHost(). More... | |
Protected Functions | |
QAbstractSocket (SocketType socketType, QAbstractSocketPrivate &dd, QObject *parent=0) | |
Constructs a new abstract socket of type socketType. More... | |
qint64 | readData (char *data, qint64 maxlen) |
Reimplemented Function More... | |
qint64 | readLineData (char *data, qint64 maxlen) |
Reimplemented Function More... | |
void | setLocalAddress (const QHostAddress &address) |
Sets the address on the local side of a connection to address. More... | |
void | setLocalPort (quint16 port) |
Sets the port on the local side of a connection to port. More... | |
void | setPeerAddress (const QHostAddress &address) |
Sets the address of the remote side of the connection to address. More... | |
void | setPeerName (const QString &name) |
Sets the host name of the remote peer to name. More... | |
void | setPeerPort (quint16 port) |
Sets the port of the remote side of the connection to port. More... | |
void | setSocketError (SocketError socketError) |
Sets the type of error that last occurred to socketError. More... | |
void | setSocketState (SocketState state) |
Sets the state of the socket to state. More... | |
qint64 | writeData (const char *data, qint64 len) |
Reimplemented Function More... | |
Protected Functions inherited from QIODevice | |
QIODevice (QIODevicePrivate &dd, QObject *parent=0) | |
void | setErrorString (const QString &errorString) |
Sets the human readable description of the last device error that occurred to str. More... | |
void | setOpenMode (OpenMode openMode) |
Sets the OpenMode of the device to openMode. More... | |
Protected Functions inherited from QObject | |
virtual void | childEvent (QChildEvent *) |
This event handler can be reimplemented in a subclass to receive child events. More... | |
virtual void | connectNotify (const char *signal) |
This virtual function is called when something has been connected to signal in this object. More... | |
virtual void | customEvent (QEvent *) |
This event handler can be reimplemented in a subclass to receive custom events. More... | |
virtual void | disconnectNotify (const char *signal) |
This virtual function is called when something has been disconnected from signal in this object. More... | |
QObject (QObjectPrivate &dd, QObject *parent=0) | |
int | receivers (const char *signal) const |
Returns the number of receivers connected to the signal. More... | |
QObject * | sender () const |
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More... | |
int | senderSignalIndex () const |
virtual void | timerEvent (QTimerEvent *) |
This event handler can be reimplemented in a subclass to receive timer events for the object. More... | |
Additional Inherited Members | |
Public Slots inherited from QObject | |
void | deleteLater () |
Schedules this object for deletion. More... | |
Static Public Functions inherited from QObject | |
static bool | connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection) |
Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More... | |
static bool | connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection) |
static bool | disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member) |
Disconnects signal in object sender from method in object receiver. More... | |
static bool | disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member) |
static uint | registerUserData () |
static QString | tr (const char *sourceText, const char *comment=0, int n=-1) |
static QString | trUtf8 (const char *sourceText, const char *comment=0, int n=-1) |
Static Public Variables inherited from QObject | |
static const QMetaObject | staticMetaObject |
This variable stores the meta-object for the class. More... | |
Protected Variables inherited from QObject | |
QScopedPointer< QObjectData > | d_ptr |
Static Protected Variables inherited from QObject | |
static const QMetaObject | staticQtMetaObject |
Related Functions inherited from QObject | |
T | qFindChildqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QString &name)() |
QList< T > | qFindChildrenqFindChildren (const QObject *obj, const QRegExp ®Exp)() |
T * | qobject_cast (QObject *object) |
QObjectList | |
void * | qt_find_obj_child (QObject *parent, const char *type, const QString &name) |
Returns a pointer to the object named name that inherits type and with a given parent. More... | |
The QAbstractSocket class provides the base functionality common to all socket types.
QAbstractSocket is the base class for QTcpSocket and QUdpSocket and contains all common functionality of these two classes. If you need a socket, you have two options:
TCP (Transmission Control Protocol) is a reliable, stream-oriented, connection-oriented transport protocol. UDP (User Datagram Protocol) is an unreliable, datagram-oriented, connectionless protocol. In practice, this means that TCP is better suited for continuous transmission of data, whereas the more lightweight UDP can be used when reliability isn't important.
QAbstractSocket's API unifies most of the differences between the two protocols. For example, although UDP is connectionless, connectToHost() establishes a virtual connection for UDP sockets, enabling you to use QAbstractSocket in more or less the same way regardless of the underlying protocol. Internally, QAbstractSocket remembers the address and port passed to connectToHost(), and functions like read() and write() use these values.
At any time, QAbstractSocket has a state (returned by state()). The initial state is UnconnectedState. After calling connectToHost(), the socket first enters HostLookupState. If the host is found, QAbstractSocket enters ConnectingState and emits the hostFound() signal. When the connection has been established, it enters ConnectedState and emits connected(). If an error occurs at any stage, error() is emitted. Whenever the state changes, stateChanged() is emitted. For convenience, isValid() returns true if the socket is ready for reading and writing, but note that the socket's state must be ConnectedState before reading and writing can occur.
Read or write data by calling read() or write(), or use the convenience functions readLine() and readAll(). QAbstractSocket also inherits getChar(), putChar(), and ungetChar() from QIODevice, which work on single bytes. The bytesWritten() signal is emitted when data has been written to the socket (i.e., when the client has read the data). Note that Qt does not limit the write buffer size. You can monitor its size by listening to this signal.
The readyRead() signal is emitted every time a new chunk of data has arrived. bytesAvailable() then returns the number of bytes that are available for reading. Typically, you would connect the readyRead() signal to a slot and read all available data there. If you don't read all the data at once, the remaining data will still be available later, and any new incoming data will be appended to QAbstractSocket's internal read buffer. To limit the size of the read buffer, call setReadBufferSize().
To close the socket, call disconnectFromHost(). QAbstractSocket enters QAbstractSocket::ClosingState. After all pending data has been written to the socket, QAbstractSocket actually closes the socket, enters QAbstractSocket::ClosedState, and emits disconnected(). If you want to abort a connection immediately, discarding all pending data, call abort() instead. If the remote host closes the connection, QAbstractSocket will emit error(QAbstractSocket::RemoteHostClosedError), during which the socket state will still be ConnectedState, and then the disconnected() signal will be emitted.
The port and address of the connected peer is fetched by calling peerPort() and peerAddress(). peerName() returns the host name of the peer, as passed to connectToHost(). localPort() and localAddress() return the port and address of the local socket.
QAbstractSocket provides a set of functions that suspend the calling thread until certain signals are emitted. These functions can be used to implement blocking sockets:
waitForConnected() blocks until a connection has been established.
waitForReadyRead() blocks until new data is available for reading.
waitForBytesWritten() blocks until one payload of data has been written to the socket.
We show an example:
If QIODevice::waitForReadyRead() returns false, the connection has been closed or an error has occurred.
Programming with a blocking socket is radically different from programming with a non-blocking socket. A blocking socket doesn't require an event loop and typically leads to simpler code. However, in a GUI application, blocking sockets should only be used in non-GUI threads, to avoid freezing the user interface. See the network/fortuneclient and network/blockingfortuneclient examples for an overview of both approaches.
QAbstractSocket can be used with QTextStream and QDataStream's stream operators (operator<<() and operator>>()). There is one issue to be aware of, though: You must make sure that enough data is available before attempting to read it using operator>>().
Definition at line 64 of file qabstractsocket.h.
This enum describes the network layer protocol values used in Qt.
Enumerator | |
---|---|
IPv4Protocol | |
IPv6Protocol | |
UnknownNetworkLayerProtocol |
Definition at line 74 of file qabstractsocket.h.
This enum describes the socket errors that can occur.
Definition at line 79 of file qabstractsocket.h.
This enum represents the options that can be set on a socket.
If desired, they can be set after having received the connected() signal from the socket or after having received a new socket from a QTcpServer.
Enumerator | |
---|---|
LowDelayOption | |
KeepAliveOption | |
MulticastTtlOption | |
MulticastLoopbackOption |
Definition at line 120 of file qabstractsocket.h.
This enum describes the different states in which a socket can be.
Enumerator | |
---|---|
UnconnectedState | |
HostLookupState | |
ConnectingState | |
ConnectedState | |
BoundState | |
ListeningState | |
ClosingState |
Definition at line 102 of file qabstractsocket.h.
This enum describes the transport layer protocol.
Enumerator | |
---|---|
TcpSocket | |
UdpSocket | |
UnknownSocketType |
Definition at line 69 of file qabstractsocket.h.
QAbstractSocket::QAbstractSocket | ( | SocketType | socketType, |
QObject * | parent | ||
) |
Creates a new abstract socket of type socketType.
The parent argument is passed to QObject's constructor.
Definition at line 1351 of file qabstractsocket.cpp.
|
virtual |
Destroys the socket.
Definition at line 1364 of file qabstractsocket.cpp.
|
protected |
Constructs a new abstract socket of type socketType.
The parent argument is passed to QObject's constructor.
Definition at line 1332 of file qabstractsocket.cpp.
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.
Definition at line 2151 of file qabstractsocket.cpp.
Referenced by QHttpPrivate::_q_slotError(), QHttpPrivate::_q_slotSendRequest(), QFtpDTP::abortConnection(), QSocks5SocketEnginePrivate::reauthenticate(), and ~QAbstractSocket().
|
virtual |
Returns true if no more data is currently available for reading; otherwise returns false.
Reimplemented Function
This function is most commonly used when reading data from the socket in a loop. For example:
// This slot is connected to QAbstractSocket::readyRead() void SocketClass::readyReadSlot() { while (!socket.atEnd()) { QByteArray data = socket.read(100); .... } }
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 2198 of file qabstractsocket.cpp.
|
virtual |
Returns the number of incoming bytes that are waiting to be read.
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 1558 of file qabstractsocket.cpp.
Referenced by QHttpNetworkConnectionChannel::_q_error(), QHttpNetworkConnectionChannel::_q_readyRead(), QSslSocketPrivate::_q_readyReadSlot(), QHttpNetworkConnectionChannel::_q_receiveReply(), QHttpPrivate::_q_slotReadyRead(), QFtpDTP::abortConnection(), QHttpNetworkConnectionChannel::allDone(), QFtpDTP::bytesAvailable(), QVNCServer::clientCutText(), QSocks5PasswordAuthenticator::continueAuthenticate(), QSslSocketBackendPrivate::disconnected(), QHttpNetworkReplyPrivate::getChunkSize(), QSocks5SocketEnginePrivate::parseAuthenticationMethodReply(), QRfbSetEncodings::read(), QRfbFrameBufferUpdateRequest::read(), QRfbKeyEvent::read(), QRfbPointerEvent::read(), QRfbClientCutText::read(), QHttpNetworkReplyPrivate::readBody(), QHttpNetworkReplyPrivate::readBodyFast(), QVNCServer::readClient(), QHttpNetworkReplyPrivate::readReplyBodyChunked(), QHttpNetworkReplyPrivate::readReplyBodyRaw(), QHttpNetworkConnectionChannel::sendRequest(), QVNCServer::setEncodings(), QVNCServer::setPixelFormat(), QFtpDTP::socketReadyRead(), and QSslSocketBackendPrivate::transmit().
|
virtual |
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.
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 1544 of file qabstractsocket.cpp.
Referenced by QHttpPrivate::postMoreData(), QHttpNetworkConnectionChannel::sendRequest(), and QFtpDTP::writeData().
|
virtual |
Returns true if a line of data can be read from the socket; otherwise returns false.
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 1644 of file qabstractsocket.cpp.
Referenced by QHttpPrivate::_q_slotReadyRead(), QFtpPI::readyRead(), and QFtpDTP::socketReadyRead().
|
virtual |
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.
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 2550 of file qabstractsocket.cpp.
Referenced by QHttpPrivate::_q_slotReadyRead(), abort(), QSslSocket::close(), QHttpNetworkConnectionChannel::close(), QHttpPrivate::closeConn(), QFtpPI::connectionClosed(), QSocks5PasswordAuthenticator::continueAuthenticate(), QSocks5SocketEnginePrivate::parseAuthenticationMethodReply(), QSocks5SocketEnginePrivate::parseRequestMethodReply(), QSocks5SocketEnginePrivate::reauthenticate(), QFtpDTP::socketReadyRead(), waitForBytesWritten(), waitForDisconnected(), waitForReadyRead(), QFtpDTP::writeData(), and QHttpNetworkConnectionPrivate::~QHttpNetworkConnectionPrivate().
|
signal |
This signal is emitted after connectToHost() has been called and a connection has been successfully established.
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.
Definition at line 1409 of file qabstractsocket.cpp.
Referenced by QHttpPrivate::_q_slotSendRequest(), QFtpDTP::connectToHost(), connectToHost(), QFtpPI::connectToHost(), QHttpNetworkConnectionChannel::ensureConnection(), and QSocks5SocketEnginePrivate::reauthenticate().
void QAbstractSocket::connectToHost | ( | const QHostAddress & | address, |
quint16 | port, | ||
OpenMode | openMode = ReadWrite |
||
) |
Attempts to make a connection to address on port port.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1527 of file qabstractsocket.cpp.
|
protectedslot |
Contains the implementation of connectToHost().
Attempts to make a connection to hostName on the given port. The socket is opened in the given openMode.
Definition at line 1430 of file qabstractsocket.cpp.
|
signal |
This signal is emitted when the socket has been disconnected.
Referenced by disconnectFromHostImplementation().
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.
Definition at line 2578 of file qabstractsocket.cpp.
Referenced by close(), QSslSocketBackendPrivate::disconnectFromHost(), QSslSocketBackendPrivate::startHandshake(), and waitForConnected().
|
protectedslot |
Contains the implementation of disconnectFromHost().
Definition at line 2592 of file qabstractsocket.cpp.
QAbstractSocket::SocketError QAbstractSocket::error | ( | ) | const |
Returns the type of error that last occurred.
Definition at line 2803 of file qabstractsocket.cpp.
Referenced by QSocks5SocketEnginePrivate::_q_emitPendingReadNotification(), QSslSocketPrivate::_q_errorSlot(), QHttpNetworkConnectionChannel::_q_readyRead(), QUdpSocket::bind(), QSocks5SocketEngine::bind(), connectToHostImplementation(), QUdpSocket::readDatagram(), QSocks5SocketEnginePrivate::setErrorState(), QSslSocketBackendPrivate::transmit(), waitForBytesWritten(), waitForDisconnected(), waitForReadyRead(), and QUdpSocket::writeDatagram().
|
signal |
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().
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.
Definition at line 2218 of file qabstractsocket.cpp.
Referenced by QDeclarativeDebugConnectionPrivate::advertisePlugins(), QDeclarativeDebugConnectionPrivate::connected(), QVNCDirtyMap::QVNCDirtyMap(), QSocks5SocketEnginePrivate::sendRequestMethod(), QRfbRawEncoder::write(), and QRfbHextileEncoder< SRC >::write().
|
signal |
This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
|
virtual |
Reimplemented Function
Reimplemented from QIODevice.
Definition at line 2178 of file qabstractsocket.cpp.
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.
Definition at line 1383 of file qabstractsocket.cpp.
Referenced by QUdpSocket::setMulticastInterface(), and QSslSocketBackendPrivate::transmit().
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.
Definition at line 1596 of file qabstractsocket.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), and QFtpPI::startNextCmd().
quint16 QAbstractSocket::localPort | ( | ) | const |
Returns the host port number (in native byte order) of the local socket if available; otherwise returns 0.
Definition at line 1580 of file qabstractsocket.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), and QSocks5SocketEngine::bind().
QHostAddress QAbstractSocket::peerAddress | ( | ) | const |
Returns the address of the connected peer if the socket is in ConnectedState; otherwise returns QHostAddress::Null.
Definition at line 1620 of file qabstractsocket.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), and QFtpPI::processReply().
QString QAbstractSocket::peerName | ( | ) | const |
Returns the name of the peer as specified by connectToHost(), or an empty QString if connectToHost() has not been called.
Definition at line 1632 of file qabstractsocket.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), QHttpPrivate::_q_slotError(), QHttpPrivate::_q_slotSendRequest(), QFtpPI::error(), QHttpNetworkConnectionPrivate::errorDetail(), and QFtpPI::processReply().
quint16 QAbstractSocket::peerPort | ( | ) | const |
Returns the port of the connected peer if the socket is in ConnectedState; otherwise returns 0.
Definition at line 1608 of file qabstractsocket.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), and QHttpPrivate::_q_slotSendRequest().
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.
Definition at line 2859 of file qabstractsocket.cpp.
Referenced by QHttpNetworkConnectionChannel::ensureConnection().
|
signal |
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.
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.
Definition at line 2716 of file qabstractsocket.cpp.
Reimplemented Function
Implements QIODevice.
Reimplemented in QSslSocket.
Definition at line 2233 of file qabstractsocket.cpp.
Reimplemented Function
Reimplemented from QIODevice.
Definition at line 2350 of file qabstractsocket.cpp.
|
protected |
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()).
Definition at line 2469 of file qabstractsocket.cpp.
|
protected |
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()).
Definition at line 2444 of file qabstractsocket.cpp.
|
protected |
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.
Definition at line 2513 of file qabstractsocket.cpp.
|
protected |
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.
Definition at line 2534 of file qabstractsocket.cpp.
|
protected |
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.
Definition at line 2491 of file qabstractsocket.cpp.
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.
Definition at line 2841 of file qabstractsocket.cpp.
Referenced by QHttpPrivate::_q_slotSendRequest(), QSocks5SocketEngine::bind(), QHttpNetworkConnectionChannel::init(), and QSocks5SocketEnginePrivate::initialize().
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.
Definition at line 2743 of file qabstractsocket.cpp.
Referenced by QHttpNetworkConnectionChannel::ensureConnection(), and QSslSocketBackendPrivate::startHandshake().
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.
Definition at line 1684 of file qabstractsocket.cpp.
Referenced by QTcpServer::incomingConnection().
|
protected |
Sets the type of error that last occurred to socketError.
Definition at line 2813 of file qabstractsocket.cpp.
void QAbstractSocket::setSocketOption | ( | QAbstractSocket::SocketOption | option, |
const QVariant & | value | ||
) |
Sets the given option to the value described by value.
Definition at line 1741 of file qabstractsocket.cpp.
Referenced by QHttpNetworkConnectionChannel::_q_connected(), and QFtpPI::connected().
|
protected |
Sets the state of the socket to state.
Definition at line 2783 of file qabstractsocket.cpp.
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.
Definition at line 1666 of file qabstractsocket.cpp.
Referenced by QSslSocketPrivate::_q_connectedSlot(), QLocalServerPrivate::_q_onNewConnection(), QLocalSocket::setSocketDescriptor(), and setSocketDescriptor().
QVariant QAbstractSocket::socketOption | ( | QAbstractSocket::SocketOption | option | ) |
Returns the value of the option option.
Definition at line 1781 of file qabstractsocket.cpp.
QAbstractSocket::SocketType QAbstractSocket::socketType | ( | ) | const |
Returns the socket type (TCP, UDP, or other).
Definition at line 2793 of file qabstractsocket.cpp.
Referenced by QAbstractSocket().
QAbstractSocket::SocketState QAbstractSocket::state | ( | ) | const |
Returns the state of the socket.
Definition at line 2773 of file qabstractsocket.cpp.
Referenced by QSocks5SocketEnginePrivate::_q_emitPendingReadNotification(), QHttpNetworkConnectionChannel::_q_readyRead(), QHttpNetworkConnectionChannel::_q_receiveReply(), QHttpPrivate::_q_slotSendRequest(), QHttpNetworkConnectionPrivate::_q_startNextRequest(), QHttpNetworkConnectionChannel::allDone(), QUdpSocket::bind(), QFtpDTP::bytesAvailable(), QHttpNetworkConnectionChannel::close(), QHttpNetworkConnectionChannel::detectPipeliningSupport(), QHttpNetworkConnectionChannel::ensureConnection(), QHttpNetworkConnectionPrivate::fillPipeline(), QDeclarativeDebugConnection::isConnected(), QVNCDirtyMap::QVNCDirtyMap(), QFtpDTP::read(), QFtpDTP::readAll(), QFtpPI::sendCommands(), setSocketState(), QFtpDTP::state(), QSslSocketBackendPrivate::transmit(), waitForBytesWritten(), waitForConnected(), waitForDisconnected(), waitForReadyRead(), QRfbRawEncoder::write(), and QRfbHextileEncoder< SRC >::write().
|
signal |
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().
Referenced by QUdpSocket::bind(), connectToHostImplementation(), disconnectFromHostImplementation(), setSocketDescriptor(), and waitForConnected().
|
virtual |
Reimplemented Function
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 2006 of file qabstractsocket.cpp.
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.
Definition at line 1851 of file qabstractsocket.cpp.
Referenced by waitForBytesWritten(), waitForDisconnected(), and waitForReadyRead().
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.
Definition at line 2088 of file qabstractsocket.cpp.
|
virtual |
This function blocks until new data is available for reading and the QIODevice::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).
Reimplemented from QIODevice.
Reimplemented in QSslSocket.
Definition at line 1948 of file qabstractsocket.cpp.
Referenced by QSocks5SocketEngine::bind().
Reimplemented Function
Implements QIODevice.
Reimplemented in QSslSocket.
Definition at line 2357 of file qabstractsocket.cpp.