You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/man/man3/tqsocket.3qt

472 lines
20 KiB

'\" t
.TH QSocket 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QSocket \- Buffered TCP connection
.SH SYNOPSIS
\fC#include <ntqsocket.h>\fR
.PP
Inherits QObject and QIODevice.
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "enum \fBError\fR { ErrConnectionRefused, ErrHostNotFound, ErrSocketRead }"
.br
.ti -1c
.BI "\fBQSocket\fR ( QObject * parent = 0, const char * name = 0 )"
.br
.ti -1c
.BI "virtual \fB~QSocket\fR ()"
.br
.ti -1c
.BI "enum \fBState\fR { Idle, HostLookup, Connecting, Connected, Closing, Connection = Connected }"
.br
.ti -1c
.BI "State \fBstate\fR () const"
.br
.ti -1c
.BI "int \fBsocket\fR () const"
.br
.ti -1c
.BI "virtual void \fBsetSocket\fR ( int socket )"
.br
.ti -1c
.BI "QSocketDevice * \fBsocketDevice\fR ()"
.br
.ti -1c
.BI "virtual void \fBsetSocketDevice\fR ( QSocketDevice * device )"
.br
.ti -1c
.BI "virtual void \fBconnectToHost\fR ( const QString & host, TQ_UINT16 port )"
.br
.ti -1c
.BI "QString \fBpeerName\fR () const"
.br
.ti -1c
.BI "virtual bool \fBopen\fR ( int m )"
.br
.ti -1c
.BI "virtual void \fBclose\fR ()"
.br
.ti -1c
.BI "virtual void \fBflush\fR ()"
.br
.ti -1c
.BI "virtual Offset \fBsize\fR () const"
.br
.ti -1c
.BI "virtual Offset \fBat\fR () const"
.br
.ti -1c
.BI "virtual bool \fBat\fR ( Offset index )"
.br
.ti -1c
.BI "virtual bool \fBatEnd\fR () const"
.br
.ti -1c
.BI "TQ_ULONG \fBbytesAvailable\fR () const"
.br
.ti -1c
.BI "TQ_ULONG \fBwaitForMore\fR ( int msecs, bool * timeout ) const"
.br
.ti -1c
.BI "TQ_ULONG \fBwaitForMore\fR ( int msecs ) const"
.br
.ti -1c
.BI "TQ_ULONG \fBbytesToWrite\fR () const"
.br
.ti -1c
.BI "void \fBclearPendingData\fR ()"
.br
.ti -1c
.BI "virtual TQ_LONG \fBreadBlock\fR ( char * data, TQ_ULONG maxlen )"
.br
.ti -1c
.BI "virtual TQ_LONG \fBwriteBlock\fR ( const char * data, TQ_ULONG len )"
.br
.ti -1c
.BI "virtual int \fBgetch\fR ()"
.br
.ti -1c
.BI "virtual int \fBputch\fR ( int ch )"
.br
.ti -1c
.BI "virtual int \fBungetch\fR ( int ch )"
.br
.ti -1c
.BI "bool \fBcanReadLine\fR () const"
.br
.ti -1c
.BI "virtual QString \fBreadLine\fR ()"
.br
.ti -1c
.BI "TQ_UINT16 \fBport\fR () const"
.br
.ti -1c
.BI "TQ_UINT16 \fBpeerPort\fR () const"
.br
.ti -1c
.BI "QHostAddress \fBaddress\fR () const"
.br
.ti -1c
.BI "QHostAddress \fBpeerAddress\fR () const"
.br
.ti -1c
.BI "void \fBsetReadBufferSize\fR ( TQ_ULONG bufSize )"
.br
.ti -1c
.BI "TQ_ULONG \fBreadBufferSize\fR () const"
.br
.in -1c
.SS "Signals"
.in +1c
.ti -1c
.BI "void \fBhostFound\fR ()"
.br
.ti -1c
.BI "void \fBconnected\fR ()"
.br
.ti -1c
.BI "void \fBconnectionClosed\fR ()"
.br
.ti -1c
.BI "void \fBdelayedCloseFinished\fR ()"
.br
.ti -1c
.BI "void \fBreadyRead\fR ()"
.br
.ti -1c
.BI "void \fBbytesWritten\fR ( int nbytes )"
.br
.ti -1c
.BI "void \fBerror\fR ( int )"
.br
.in -1c
.SH DESCRIPTION
The QSocket class provides a buffered TCP connection.
.PP
It provides a totally non-blocking QIODevice, and modifies and extends the API of QIODevice with socket-specific code.
.PP
Note that a QApplication must have been constructed before this class can be used.
.PP
The functions you're likely to call most are connectToHost(), bytesAvailable(), canReadLine() and the ones it inherits from QIODevice.
.PP
connectToHost() is the most-used function. As its name implies, it opens a connection to a named host.
.PP
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.
.PP
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.
.PP
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()). socketDevice() returns a pointer to the QSocketDevice used for this socket.
.PP
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.
.PP
\fBWarning:\fR QSocket is not suitable for use in threads. If you need to uses sockets in threads use the lower-level QSocketDevice class.
.PP
\fBWarning:\fR Because TQt doesn't use the native socketstream implementation on Mac OS X, QSocket has an implicit transfer latency of 100ms. You can achieve lower latency on Mac OS X by using QSocketDevice instead.
.PP
See also QSocketDevice, QHostAddress, QSocketNotifier, and Input/Output and Networking.
.SS "Member Type Documentation"
.SH "QSocket::Error"
This enum specifies the possible errors:
.TP
\fCQSocket::ErrConnectionRefused\fR - if the connection was refused
.TP
\fCQSocket::ErrHostNotFound\fR - if the host was not found
.TP
\fCQSocket::ErrSocketRead\fR - if a read from the socket failed
.SH "QSocket::State"
This enum defines the connection states:
.TP
\fCQSocket::Idle\fR - if there is no connection
.TP
\fCQSocket::HostLookup\fR - during a DNS lookup
.TP
\fCQSocket::Connecting\fR - during TCP connection establishment
.TP
\fCQSocket::Connected\fR - when there is an operational connection
.TP
\fCQSocket::Closing\fR - if the socket is closing down, but is not yet closed.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QSocket::QSocket ( QObject * parent = 0, const char * name = 0 )"
Creates a QSocket object in QSocket::Idle state.
.PP
The \fIparent\fR and \fIname\fR arguments are passed on to the QObject constructor.
.PP
Note that a QApplication must have been constructed before sockets can be used.
.SH "QSocket::~QSocket ()\fC [virtual]\fR"
Destroys the socket. Closes the connection if necessary.
.PP
See also close().
.SH "QHostAddress QSocket::address () const"
Returns the host address of this socket. (This is normally the main IP address of the host, but can be e.g. 127.0.0.1 for connections to localhost.)
.SH "Offset QSocket::at () const\fC [virtual]\fR"
Returns the current read index. Since QSocket is a sequential device, the current read index is always zero.
.PP
Reimplemented from QIODevice.
.SH "bool QSocket::at ( Offset index )\fC [virtual]\fR"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Moves the read index forward to \fIindex\fR and returns TRUE if the operation was successful; otherwise returns FALSE. Moving the index forward means skipping incoming data.
.PP
Reimplemented from QIODevice.
.SH "bool QSocket::atEnd () const\fC [virtual]\fR"
Returns TRUE if there is no more data to read; otherwise returns FALSE.
.PP
Reimplemented from QIODevice.
.SH "TQ_ULONG QSocket::bytesAvailable () const"
Returns the number of incoming bytes that can be read, i.e. the size of the input buffer. Equivalent to size().
.PP
This function can trigger the readyRead() signal, if more data has arrived on the socket.
.PP
See also bytesToWrite().
.PP
Example: network/networkprotocol/nntp.cpp.
.SH "TQ_ULONG QSocket::bytesToWrite () const"
Returns the number of bytes that are waiting to be written, i.e. the size of the output buffer.
.PP
See also bytesAvailable() and clearPendingData().
.SH "void QSocket::bytesWritten ( int nbytes )\fC [signal]\fR"
This signal is emitted when data has been written to the network. The \fInbytes\fR parameter specifies how many bytes were written.
.PP
The bytesToWrite() function is often used in the same context; it indicates how many buffered bytes there are left to write.
.PP
See also writeBlock() and bytesToWrite().
.SH "bool QSocket::canReadLine () const"
Returns TRUE if it's possible to read an entire line of text from this socket at this time; otherwise returns FALSE.
.PP
Note that if the peer closes the connection unexpectedly, this function returns FALSE. This means that loops such as this won't work:
.PP
.nf
.br
while( !socket->canReadLine() ) // WRONG
.br
;
.br
.fi
.PP
See also readLine().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp, and network/networkprotocol/nntp.cpp.
.SH "void QSocket::clearPendingData ()"
Deletes the data that is waiting to be written. This is useful if you want to close the socket without waiting for all the data to be written.
.PP
See also bytesToWrite(), close(), and delayedCloseFinished().
.SH "void QSocket::close ()\fC [virtual]\fR"
Closes the socket.
.PP
The read buffer is cleared.
.PP
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.
.PP
If you don't want that the data of the output buffer is written, call clearPendingData() before you call close().
.PP
See also state(), bytesToWrite(), and clearPendingData().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/httpd/httpd.cpp, and network/networkprotocol/nntp.cpp.
.PP
Reimplemented from QIODevice.
.SH "void QSocket::connectToHost ( const QString & host, TQ_UINT16 port )\fC [virtual]\fR"
Attempts to make a connection to \fIhost\fR on the specified \fIport\fR and return immediately.
.PP
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().
.PP
\fIhost\fR 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 \fIport\fR is in native byte order, unlike some other libraries.
.PP
See also state().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/mail/smtp.cpp, and network/networkprotocol/nntp.cpp.
.SH "void QSocket::connected ()\fC [signal]\fR"
This signal is emitted after connectToHost() has been called and a connection has been successfully established.
.PP
See also connectToHost() and connectionClosed().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/mail/smtp.cpp, and network/networkprotocol/nntp.cpp.
.SH "void QSocket::connectionClosed ()\fC [signal]\fR"
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.
.PP
See also connectToHost() and close().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/clientserver/server/server.cpp, and network/networkprotocol/nntp.cpp.
.SH "void QSocket::delayedCloseFinished ()\fC [signal]\fR"
This signal is emitted when a delayed close is finished.
.PP
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.
.PP
See also close().
.PP
Examples:
.)l network/clientserver/client/client.cpp and network/httpd/httpd.cpp.
.SH "void QSocket::error ( int )\fC [signal]\fR"
This signal is emitted after an error occurred. The parameter is the Error value.
.PP
Examples:
.)l network/clientserver/client/client.cpp and network/networkprotocol/nntp.cpp.
.SH "void QSocket::flush ()\fC [virtual]\fR"
Implementation of the abstract virtual QIODevice::flush() function.
.PP
Reimplemented from QIODevice.
.SH "int QSocket::getch ()\fC [virtual]\fR"
Reads a single byte/character from the internal read buffer. Returns the byte/character read, or -1 if there is nothing to be read.
.PP
See also bytesAvailable() and putch().
.PP
Reimplemented from QIODevice.
.SH "void QSocket::hostFound ()\fC [signal]\fR"
This signal is emitted after connectToHost() has been called and the host lookup has succeeded.
.PP
See also connected().
.PP
Example: network/networkprotocol/nntp.cpp.
.SH "bool QSocket::open ( int m )\fC [virtual]\fR"
Opens the socket using the specified QIODevice file mode \fIm\fR. This function is called automatically when needed and you should not call it yourself.
.PP
See also close().
.PP
Reimplemented from QIODevice.
.SH "QHostAddress QSocket::peerAddress () const"
Returns the address of the connected peer if the socket is in Connected state; otherwise an empty QHostAddress is returned.
.SH "QString QSocket::peerName () const"
Returns the host name as specified to the connectToHost() function. An empty string is returned if none has been set.
.PP
Example: network/mail/smtp.cpp.
.SH "TQ_UINT16 QSocket::peerPort () const"
Returns the peer's host port number, normally as specified to the connectToHost() function. If none has been set, this function returns 0.
.PP
Note that TQt always uses native byte order, i.e. 67 is 67 in Qt; there is no need to call htons().
.SH "TQ_UINT16 QSocket::port () const"
Returns the host port number of this socket, in native byte order.
.SH "int QSocket::putch ( int ch )\fC [virtual]\fR"
Writes the character \fIch\fR to the output buffer.
.PP
Returns \fIch\fR, or -1 if an error occurred.
.PP
See also getch().
.PP
Reimplemented from QIODevice.
.SH "TQ_LONG QSocket::readBlock ( char * data, TQ_ULONG maxlen )\fC [virtual]\fR"
Reads \fImaxlen\fR bytes from the socket into \fIdata\fR and returns the number of bytes read. Returns -1 if an error occurred.
.PP
Example: network/networkprotocol/nntp.cpp.
.PP
Reimplemented from QIODevice.
.SH "TQ_ULONG QSocket::readBufferSize () const"
Returns the size of the read buffer.
.PP
See also setReadBufferSize().
.SH "QString QSocket::readLine ()\fC [virtual]\fR"
Returns a line of text including a terminating newline character (\\n). Returns "" if canReadLine() returns FALSE.
.PP
See also canReadLine().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp, and network/networkprotocol/nntp.cpp.
.SH "void QSocket::readyRead ()\fC [signal]\fR"
This signal is emitted every time there is new incoming data.
.PP
Bear in mind that new incoming data is only reported once; if you do not read all the data, this class buffers the data and you can read it later, but no signal is emitted unless new data arrives. A good practice is to read all data in the slot connected to this signal unless you are sure that you need to receive more data to be able to process it.
.PP
See also readBlock(), readLine(), and bytesAvailable().
.PP
Examples:
.)l network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp, and network/networkprotocol/nntp.cpp.
.SH "void QSocket::setReadBufferSize ( TQ_ULONG bufSize )"
Sets the size of the QSocket's internal read buffer to \fIbufSize\fR.
.PP
Usually QSocket reads all data that is available from the operating system's socket. If the buffer size is limited to a certain size, this means that the QSocket class doesn't buffer more than this size of data.
.PP
If the size of the read buffer is 0, the read buffer is unlimited and all incoming data is buffered. This is the default.
.PP
If you read the data in the readyRead() signal, you shouldn't use this option since it might slow down your program unnecessary. This option is useful if you only need to read the data at certain points in time, like in a realtime streaming application.
.PP
See also readBufferSize().
.SH "void QSocket::setSocket ( int socket )\fC [virtual]\fR"
Sets the socket to use \fIsocket\fR and the state() to Connected. The socket must already be connected.
.PP
This allows us to use the QSocket class as a wrapper for other socket types (e.g. Unix Domain Sockets).
.PP
Example: network/httpd/httpd.cpp.
.SH "void QSocket::setSocketDevice ( QSocketDevice * device )\fC [virtual]\fR"
Sets the internal socket device to \fIdevice\fR. Passing a \fIdevice\fR of 0 will cause the internal socket device to be used. Any existing connection will be disconnected before using the new \fIdevice\fR.
.PP
The new device should not be connected before being associated with a QSocket; after setting the socket call connectToHost() to make the connection.
.PP
This function is useful if you need to subclass QSocketDevice and want to use the QSocket API, for example, to implement Unix domain sockets.
.SH "Offset QSocket::size () const\fC [virtual]\fR"
Returns the number of incoming bytes that can be read right now (like bytesAvailable()).
.PP
Reimplemented from QIODevice.
.SH "int QSocket::socket () const"
Returns the socket number, or -1 if there is no socket at the moment.
.SH "QSocketDevice * QSocket::socketDevice ()"
Returns a pointer to the internal socket device.
.PP
There is normally no need to manipulate the socket device directly since this class does the necessary setup for most applications.
.SH "State QSocket::state () const"
Returns the current state of the socket connection.
.PP
See also QSocket::State.
.PP
Examples:
.)l network/clientserver/client/client.cpp and network/networkprotocol/nntp.cpp.
.SH "int QSocket::ungetch ( int ch )\fC [virtual]\fR"
This implementation of the virtual function QIODevice::ungetch() prepends the character \fIch\fR to the read buffer so that the next read returns this character as the first character of the output.
.PP
Reimplemented from QIODevice.
.SH "TQ_ULONG QSocket::waitForMore ( int msecs, bool * timeout ) const"
Wait up to \fImsecs\fR milliseconds for more data to be available.
.PP
If \fImsecs\fR is -1 the call will block indefinitely.
.PP
Returns the number of bytes available.
.PP
If \fItimeout\fR is non-null and no error occurred (i.e. it does not return -1): this function sets \fI*timeout\fR to TRUE, if the reason for returning was that the timeout was reached; otherwise it sets \fI*timeout\fR to FALSE. This is useful to find out if the peer closed the connection.
.PP
\fBWarning:\fR This is a blocking call and should be avoided in event driven applications.
.PP
See also bytesAvailable().
.SH "TQ_ULONG QSocket::waitForMore ( int msecs ) const"
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.SH "TQ_LONG QSocket::writeBlock ( const char * data, TQ_ULONG len )\fC [virtual]\fR"
Writes \fIlen\fR bytes to the socket from \fIdata\fR and returns the number of bytes written. Returns -1 if an error occurred.
.PP
Example: network/networkprotocol/nntp.cpp.
.PP
Reimplemented from QIODevice.
.SH "SEE ALSO"
.BR http://doc.trolltech.com/ntqsocket.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive TQt documentation is provided in HTML format; it is
located at $QTDIR/doc/html and can be read using TQt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech.
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (tqsocket.3qt) and the Qt
version (3.3.8).