Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The QSocket class provides a buffered TCP connection. More...
#include <qsocket.h>
Inherits QObject and QIODevice.
It provides a totally non-blocking QIODevice, and modifies and extends the API of QIODevice with socket-specific code.
The functions you're likely to call most are connectToHost(), bytesAvailable(), canReadLine() and the ones it inherits from QIODevice.
connectToHost() is the most-used function. As its name implies, it opens a connection to a named host.
Most network protocols are either packet-oriented or line-oriented. canReadLine() indicates whether a connection contains an entire unread line or not, and bytesAvailable() returns the number of bytes available for reading.
The signals error(), connected(), readyRead() and connectionClosed() inform you of the progress of the connection. There are also some less commonly used signals. hostFound() is emitted when connectToHost() has finished its DNS lookup and is starting its TCP connection. delayedCloseFinished() is emitted when close() succeeds. bytesWritten() is emitted when QSocket moves data from its "to be written" queue into the TCP implementation.
There are several access functions for the socket: state() returns whether the object is idle, is doing a DNS lookup, is connecting, has an operational connection, etc. address() and port() return the IP address and port used for the connection. The peerAddress() and peerPort() functions return the IP address and port used by the peer, and peerName() returns the name of the peer (normally the name that was passed to connectToHost()). socket() returns a pointer to the QSocketDevice used for this socket.
QSocket inherits QIODevice, and reimplements some functions. In general, you can treat it as a QIODevice for writing, and mostly also for reading. The match isn't perfect, since the QIODevice API is designed for devices that are controlled by the same machine, and an asynchronous peer-to-peer network connection isn't quite like that. For example, there is nothing that matches QIODevice::size() exactly. The documentation for open(), close(), flush(), size(), at(), atEnd(), readBlock(), writeBlock(), getch(), putch(), ungetch() and readLine() describes the differences in detail.
See also QSocketDevice, QHostAddress, QSocketNotifier and Input/Output and Networking.
This enum specifies the possible errors:
This enum defines the connection states:
The parent and name arguments are passed on to the QObject constructor.
See also close().
Reimplemented from QIODevice.
Moves the read index forward to index and returns TRUE if the operation was successful. Moving the index forward means skipping incoming data.
Reimplemented from QIODevice.
Reimplemented from QIODevice.
See also bytesToWrite().
Example: network/networkprotocol/nntp.cpp.
See also bytesAvailable().
This signal is emitted when data has actually been written to the network. The nbytes parameter specifies how many bytes were written.
The bytesToWrite() function is often used in the same context, and it says how many buffered bytes there are left to write.
See also writeBlock() and bytesToWrite().
Note that if the peer closes the connection unexpectedly, this function returns FALSE. This means that loops such as this won't work:
while( !socket->canReadLine() ) // Wrong. ...
See also readLine().
Examples: network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp and network/networkprotocol/nntp.cpp.
The read buffer is cleared.
If the output buffer is empty, the state is set to QSocket::Idle and the connection is terminated immediately. If the output buffer still contains data to be written, QSocket goes into the QSocket::Closing state and the rest of the data will be written. When all of the outgoing data have been written, the state is set to QSocket::Idle and the connection is terminated. At this point, the delayedCloseFinished() signal is emitted.
See also state() and bytesToWrite().
Examples: network/clientserver/client/client.cpp, network/httpd/httpd.cpp and network/networkprotocol/nntp.cpp.
Reimplemented from QIODevice.
Any connection or pending connection is closed immediately, and QSocket goes into the HostLookup state. When the lookup succeeds, it emits hostFound(), starts a TCP connection and goes into the Connecting state. Finally, when the connection succeeds, it emits connected() and goes into the Connected state. If there is an error at any point, it emits error().
host may be an IP address in string form, or it may be a DNS name. QSocket will do a normal DNS lookup if required. Note that port is in native byte order, unlike some other libraries.
See also state().
Examples: network/clientserver/client/client.cpp, network/mail/smtp.cpp and network/networkprotocol/nntp.cpp.
This signal is emitted after connectToHost() has been called and a connection has been successfully established.
See also connectToHost() and connectionClosed().
Examples: network/clientserver/client/client.cpp, network/mail/smtp.cpp and network/networkprotocol/nntp.cpp.
This signal is emitted when the other end has closed the connection. The read buffers may contain buffered input data which you can read after the connection was closed.
See also connectToHost() and close().
Examples: network/clientserver/client/client.cpp and network/networkprotocol/nntp.cpp.
This signal is emitted when a delayed close is finished.
If you call close() and there is buffered output data to be written, QSocket goes into the QSocket::Closing state and returns immediately. It will then keep writing to the socket until all the data has been written. Then, the delayedCloseFinished() signal is emitted.
See also close().
Examples: network/clientserver/client/client.cpp and network/httpd/httpd.cpp.
This signal is emitted after an error occurred. The parameter is the Error value.
Examples: network/clientserver/client/client.cpp and network/networkprotocol/nntp.cpp.
Reimplemented from QIODevice.
See also bytesAvailable() and putch().
Reimplemented from QIODevice.
This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
See also connected().
Example: network/networkprotocol/nntp.cpp.
See also close().
Reimplemented from QIODevice.
Example: network/mail/smtp.cpp.
Note that Qt always uses native byte order, i.e. 67 is 67 in Qt, there is no need to call htons().
Returns ch, or -1 if some error occurred.
See also getch().
Reimplemented from QIODevice.
Example: network/networkprotocol/nntp.cpp.
Reimplemented from QIODevice.
See also canReadLine().
This signal is emitted when there is incoming data to be read.
Every time there is new incoming data this signal is emitted once. Bear in mind that new incoming data is only reported once; i.e. if you do not read all data, this signal is not emitted again unless new data arrives on the socket.
See also readBlock(), readLine() and bytesAvailable().
Examples: network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp and network/networkprotocol/nntp.cpp.
This allows us to use the QSocket class as a wrapper for other socket types (e.g. Unix Domain Sockets under Unix).
Example: network/httpd/httpd.cpp.
The new device should not be connected before being associated with a QSocket; after setting the socket call connectToHost() to make the connection.
This function is useful if you need to subclass QSocketDevice and want to use the QSocket API, for example, to implement Unix domain sockets.
Reimplemented from QIODevice.
This function has can usually be only entered once (i.e. no recursive calls). If the argument force is TRUE, the function is executed, but no readyRead() signals are emitted. This behaviour is useful for the waitForMore() function, so that it is possible to call waitForMore() in a slot connected to the readyRead() signal.
There is normally no need to manipulate the socket device directly since this class does the necessary setup for most applications.
See also QSocket::State.
Examples: network/clientserver/client/client.cpp and network/networkprotocol/nntp.cpp.
Reimplemented from QIODevice.
If msecs is -1 the call will block indefinitely.
This is a blocking call and should be avoided in event driven applications.
Returns the number of bytes available.
See also bytesAvailable().
Example: network/networkprotocol/nntp.cpp.
Reimplemented from QIODevice.
This file is part of the Qt toolkit. Copyright © 1995-2002 Trolltech. All Rights Reserved.
Copyright © 2002 Trolltech | Trademarks | Qt version 3.0.4
|