📄 qsocket.3qt
字号:
.TH QSocket 3qt "10 November 2000" "Trolltech AS" \" -*- nroff -*-.\" Copyright 1992-2000 Trolltech AS. All rights reserved. See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQSocket \- Buffered TCP connection<h1 align=center>QSocket Class Reference.br<small>[ network module ]</small></h1>.br.PP\fC#include <qsocket.h>\fR.PPInherits QIODevice and QObject..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, Listening, Connection, Closing }".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 ) ".br.ti -1c.BI "virtual void \fBconnectToHost\fR ( const QString & " "host" ", Q_UINT16 port ) ".br.ti -1c.BI "QString \fBpeerName\fR () const".br.ti -1c.BI "virtual bool \fBopen\fR ( int mode ) ".br.ti -1c.BI "virtual void \fBclose\fR () ".br.ti -1c.BI "virtual void \fBflush\fR () ".br.ti -1c.BI "virtual uint \fBsize\fR () const".br.ti -1c.BI "virtual int \fBat\fR () const".br.ti -1c.BI "virtual bool \fBat\fR ( int ) ".br.ti -1c.BI "virtual bool \fBatEnd\fR () const".br.ti -1c.BI "int \fBbytesAvailable\fR () const".br.ti -1c.BI "int \fBwaitForMore\fR ( int msecs ) const".br.ti -1c.BI "int \fBbytesToWrite\fR () const".br.ti -1c.BI "virtual int \fBreadBlock\fR ( char * " "data" ", uint maxlen ) ".br.ti -1c.BI "virtual int \fBwriteBlock\fR ( const char * " "data" ", uint len ) ".br.ti -1c.BI "virtual int \fBgetch\fR () ".br.ti -1c.BI "virtual int \fBputch\fR ( int ) ".br.ti -1c.BI "virtual int \fBungetch\fR ( int ) ".br.ti -1c.BI "bool \fBcanReadLine\fR () const".br.ti -1c.BI "virtual QString \fBreadLine\fR () ".br.ti -1c.BI "Q_UINT16 \fBport\fR () const".br.ti -1c.BI "Q_UINT16 \fBpeerPort\fR () const".br.ti -1c.BI "QHostAddress \fBaddress\fR () const".br.ti -1c.BI "QHostAddress \fBpeerAddress\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.SS "Protected Members".in +1c.ti -1c.BI "QSocketDevice* \fBsocketDevice\fR () ".br.in -1c.SS "Protected Slots".in +1c.ti -1c.BI "virtual void \fBsn_read\fR () ".br.ti -1c.BI "virtual void \fBsn_write\fR () ".br.in -1c.SH DESCRIPTIONThe QSocket class provides a buffered TCP connection..PPIt provides a totally non-blocking QIODevice, and modifies and extends the API of QIODevice with socket-specific code..PPThe functions you're likely to call most are connectToHost(), bytesAvailable(), canReadLine() and the ones it inherits from QIODevice. The Linux Kernel Version Walkthrough uses them in a very simple program..PPconnectToHost() is the most-used function. As its name implies, it opens a connection to a named host..PPMost 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..PPThe 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 DSN 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..PPThere 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, peerAddress() and peerPort() 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..PPQSocket inherits QIODevice, and reimplements some of the 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 each of open(), close(), flush(), size(), at(), atEnd(), readBlock(), writeBlock(), getch(), putch(), ungetch() and readLine() describe the differences in detail..PPSee also QSocketDevice, QHostAddress and QSocketNotifier..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::Connection\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 \fCQSocket::Idle\fR state..SH "QSocket::~QSocket () \fC[virtual]\fR"Destructs the socket. Closes the connection if necessary..PPSee also close()..SH "QHostAddress QSocket::address () const"Returns the host address of this socket. (This is normally be the main IP address of the host, but can be e.g. 127.0.0.1 for connections to localhost.)..SH "bool QSocket::at ( int index ) \fC[virtual]\fR"Moves the read index forward and returns TRUE if the operation was successful. Moving the index forward means skipping incoming data..PPReimplemented from QIODevice..SH "int QSocket::at () const \fC[virtual]\fR"Returns the current read index. Since QSocket is a sequential device, the current read index is always zero..PPReimplemented from QIODevice..SH "bool QSocket::atEnd () const \fC[virtual]\fR"Returns TRUE if there is no more data to read, otherwise FALSE..PPReimplemented from QIODevice..SH "int QSocket::bytesAvailable () const"Returns the number of incoming bytes that can be read, i.e. the size of the input buffer. Equivalent to size()..PPSee also bytesToWrite()..SH "int QSocket::bytesToWrite () const"Returns the number of bytes that are waiting to be written, i.e. the size of the output buffer..PPSee also bytesAvailable()..SH "void QSocket::bytesWritten ( int nbytes ) \fC[signal]\fR"This signal is emitted when data actually has been written to the network. The \fInbytes\fR parameter says how many bytes were written..PPThe bytesToWrite() function is often used in the same context, and it tells how many buffered bytes there are left to write..PPSee 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, or FALSE if not..PPSee also setMode() and readLine()..SH "void QSocket::close () \fC[virtual]\fR"Closes the socket..PPThe mode is set to \fCQSocket::Binary\fR and the read buffer is cleared..PPIf the output buffer is empty, the state is set to \fCQSocket::Idle\fR and the connection is terminated immediately. If the output buffer still contains data to be written, QSocket goes into the \fCQSocket::Closing\fR state and the rest of the data will be written. When all of the outgoing data have been written, the state is set to \fCQSocket::Idle\fR and the connection is terminated. At this point, the delayedCloseFinished() signal is emitted..PPSee also state(), setMode() and bytesToWrite()..PPReimplemented from QIODevice..SH "void QSocket::connectToHost ( const QString & host, Q_UINT16 port ) \fC[virtual]\fR"Attempts to make a connection to \fIhost\fR on the specified \fIport\fR and return immediately..PPAny connection or pending connection is closed immediately, and QSocket goes into \fCHostLookup\fR state. When the lookup succeeds, it emits hostFound(), starts a TCP connection and goes into \fCConnecting\fR state. Finally, when the connection succeeds, it emits connected() and goes into \fCConnection\fR state. If there is an error at any point, it emits error()..PPSee also state()..SH "void QSocket::connected () \fC[signal]\fR"This signal is emitted after connectToHost() has been called and a connection has been successfully established..PPSee also connectToHost() and connectionClosed()..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..PPSee also connectToHost() and close()..SH "void QSocket::delayedCloseFinished () \fC[signal]\fR"This signal is emitted when a delayed close is finished..PPIf you call close() and there is buffered output data to be written, QSocket goes into the \fCQSocket::Closing\fR state and returns immediately. It will then keep writing to the socket until all the data has been written. Then, the delayCloseFinished() signal is emitted..PPSee also close()..SH "void QSocket::error ( int ) \fC[signal]\fR"This signal is emitted after an error occurred..SH "void QSocket::flush () \fC[virtual]\fR"Implementation of the abstract virtual QIODevice::flush() function..PPReimplemented 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..PPSee also bytesAvailable() and putch()..PPReimplemented from QIODevice..SH "void QSocket::hostFound () \fC[signal]\fR"This signal is emitted after connectToHost() has been called and the host lookup has succeeded..PPSee also connected()..SH "bool QSocket::open ( int m ) \fC[virtual]\fR"Opens the socket using the specified QIODevice file mode. This function is called automatically when needed and you should not call it yourself..PPSee also close()..PPReimplemented from QIODevice..SH "QHostAddress QSocket::peerAddress () const"Returns the host address as resolved from the name specified to the connectToHost() function..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..SH "Q_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..SH "Q_UINT16 QSocket::port () const"Returns the host port number of this socket..SH "int QSocket::putch ( int ch ) \fC[virtual]\fR"Writes the character \fIch\fR into the output buffer..PPReturns \fIch,\fR or -1 if some error occurred..PPSee also getch()..PPReimplemented from QIODevice..SH "int QSocket::readBlock ( char * data, uint maxlen ) \fC[virtual]\fR"Reads max \fImaxlen\fR bytes from the socket into \fIdata\fR and returns the number of bytes read. Returns -1 if an error occurred..PPReimplemented from QIODevice..SH "QString QSocket::readLine () \fC[virtual]\fR"Returns a line of text including a terminating newline character (\\n). Returns "" if canReadLine() returns FALSE..PPSee also canReadLine()..SH "int QSocket::readLine ( char * data, uint maxlen ) \fC[virtual]\fR"Reimplemented for internal reasons; the API is not affected..PPReimplemented from QIODevice..SH "void QSocket::readyRead () \fC[signal]\fR"This signal is emitted when there is incoming data to be read..PPEvery time when there is new incoming data this signal is emitted once. Keep 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..PPSee also readBlock(), readLine() and bytesAvailable()..SH "void QSocket::setSocket ( int socket ) \fC[virtual]\fR"Sets the socket to use \fIsocket\fR and the state() to \fCConnected.\fR The socket should already be connected..PPThis allows one to use the QSocket class as a wrapper for other socket types (e.g. Unix Domain Sockets under Unix)..SH "uint QSocket::size () const \fC[virtual]\fR"Returns the number of incoming bytes that can be read right now (like bytesAvailable())..PPReimplemented from QIODevice..SH "void QSocket::sn_read () \fC[virtual protected slot]\fR"Internal slot for handling socket read notifications..SH "void QSocket::sn_write () \fC[virtual protected slot]\fR"Internal slot for handling socket write notifications..SH "int QSocket::socket () const"Returns the socket number, or -1 if there is no socket at the moment..SH "QSocketDevice * QSocket::socketDevice () \fC[protected]\fR"Returns a pointer to the internal socket device. The returned pointer may be null..PPThere is normally no need to manipulate the socket device directly since this class does the necessary setup for most applications..SH "QSocket::State QSocket::state() const"Returns the current state of the socket connection..PPSee also QSocket::State..SH "int QSocket::ungetch ( int ) \fC[virtual]\fR"This implementation of the virtual function QIODevice::ungetch() always returns -1 (error) because a QSocket is a sequential device and does not allow any ungetch operation..PPReimplemented from QIODevice..SH "int QSocket::waitForMore ( int msecs ) const"Wait upto \fImsecs\fR milliseconds for more data to be available..PPIf \fImsecs\fR is -1 the call will block indefinitely. This is a blocking call and should be avoided in event driven applications..PPReturns the number of bytes available..PPSee also bytesAvailable()..SH "int QSocket::writeBlock ( const char * data, uint 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..PPReimplemented from QIODevice..SH "SEE ALSO".BR http://doc.trolltech.com/qsocket.html.SH COPYRIGHTCopyright 1992-2000 Trolltech AS, http://www.trolltech.com/. See thelicense file included in the distribution for a complete licensestatement..SH AUTHORGenerated automatically from the source code.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -