HomeHome

Classes -Annotated - Tree -Functions -Home -Structure

QSocket Class Reference
[ network module ]


The QSocket class provides a buffered TCP connection. More...

#include <qsocket.h>

Inherits QIODevice and QObject.

List of all member functions.

Public Members

Signals

Protected Members

Protected Slots


Detailed Description

The QSocket class provides a buffered TCP connection.

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 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.

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, 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.

QSocket 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.

See also QSocketDevice, QHostAddress and QSocketNotifier.


Member Type Documentation

QSocket::Error

This enum specifies the possible errors:

QSocket::State

This enum defines the connection states:

Member Function Documentation

QSocket::QSocket(QObject*parent=0, constchar*name=0)

Creates a QSocket object in QSocket::Idle state.

QSocket::~QSocket() [virtual]

Destructs the socket. Closes the connection if necessary.

See also close().

QHostAddressQSocket::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.).

boolQSocket::at(intindex) [virtual]

Moves the read index forward and returns TRUE if the operation was successful. Moving the index forward means skipping incoming data.

Reimplemented from QIODevice.

intQSocket::at()const [virtual]

Returns the current read index. Since QSocket is a sequential device, the current read index is always zero.

Reimplemented from QIODevice.

boolQSocket::atEnd()const [virtual]

Returns TRUE if there is no more data to read, otherwise FALSE.

Reimplemented from QIODevice.

intQSocket::bytesAvailable()const

Returns the number of incoming bytes that can be read, i.e. the size of the input buffer. Equivalent to size().

See also bytesToWrite().

intQSocket::bytesToWrite()const

Returns the number of bytes that are waiting to be written, i.e. the size of the output buffer.

See also bytesAvailable().

voidQSocket::bytesWritten(intnbytes) [signal]

This signal is emitted when data actually has been written to the network. The nbytes parameter says how many bytes were written.

The bytesToWrite() function is often used in the same context, and it tells how many buffered bytes there are left to write.

See also writeBlock() and bytesToWrite().

boolQSocket::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.

See also setMode() and readLine().

voidQSocket::close() [virtual]

Closes the socket.

The mode is set to QSocket::Binary and 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(), setMode() and bytesToWrite().

Reimplemented from QIODevice.

voidQSocket::connectToHost(constQString&host, Q_UINT16port) [virtual]

Attempts to make a connection to host on the specified port and return immediately.

Any connection or pending connection is closed immediately, and QSocket goes into HostLookup state. When the lookup succeeds, it emits hostFound(), starts a TCP connection and goes into Connecting state. Finally, when the connection succeeds, it emits connected() and goes into Connection state. If there is an error at any point, it emits error().

See also state().

voidQSocket::connected() [signal]

This signal is emitted after connectToHost() has been called and a connection has been successfully established.

See also connectToHost() and connectionClosed().

voidQSocket::connectionClosed() [signal]

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().

voidQSocket::delayedCloseFinished() [signal]

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 delayCloseFinished() signal is emitted.

See also close().

voidQSocket::error(int) [signal]

This signal is emitted after an error occurred.

voidQSocket::flush() [virtual]

Implementation of the abstract virtual QIODevice::flush() function.

Reimplemented from QIODevice.

intQSocket::getch() [virtual]

Reads a single byte/character from the internal read buffer. Returns the byte/character read, or -1 if there is nothing to be read.

See also bytesAvailable() and putch().

Reimplemented from QIODevice.

voidQSocket::hostFound() [signal]

This signal is emitted after connectToHost() has been called and the host lookup has succeeded.

See also connected().

boolQSocket::open(intm) [virtual]

Opens the socket using the specified QIODevice file mode. This function is called automatically when needed and you should not call it yourself.

See also close().

Reimplemented from QIODevice.

QHostAddressQSocket::peerAddress()const

Returns the host address as resolved from the name specified to the connectToHost() function.

QStringQSocket::peerName()const

Returns the host name as specified to the connectToHost() function. An empty string is returned if none has been set.

Q_UINT16QSocket::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.

Q_UINT16QSocket::port()const

Returns the host port number of this socket.

intQSocket::putch(intch) [virtual]

Writes the character ch into the output buffer.

Returns ch, or -1 if some error occurred.

See also getch().

Reimplemented from QIODevice.

intQSocket::readBlock(char*data, uintmaxlen) [virtual]

Reads max maxlen bytes from the socket into data and returns the number of bytes read. Returns -1 if an error occurred.

Reimplemented from QIODevice.

QStringQSocket::readLine() [virtual]

Returns a line of text including a terminating newline character (\n). Returns "" if canReadLine() returns FALSE.

See also canReadLine().

intQSocket::readLine(char*data, uintmaxlen) [virtual]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QIODevice.

voidQSocket::readyRead() [signal]

This signal is emitted when there is incoming data to be read.

Every 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.

See also readBlock(), readLine() and bytesAvailable().

voidQSocket::setSocket(intsocket) [virtual]

Sets the socket to use socket and the state() to Connected. The socket should already be connected.

This allows one to use the QSocket class as a wrapper for other socket types (e.g. Unix Domain Sockets under Unix).

uintQSocket::size()const [virtual]

Returns the number of incoming bytes that can be read right now (like bytesAvailable()).

Reimplemented from QIODevice.

voidQSocket::sn_read() [virtualprotected slot]

Internal slot for handling socket read notifications.

voidQSocket::sn_write() [virtualprotected slot]

Internal slot for handling socket write notifications.

intQSocket::socket()const

Returns the socket number, or -1 if there is no socket at the moment.

QSocketDevice*QSocket::socketDevice() [protected]

Returns a pointer to the internal socket device. The returned pointer may be null.

There is normally no need to manipulate the socket device directly since this class does the necessary setup for most applications.

QSocket::StateQSocket::state()const

Returns the current state of the socket connection.

See also QSocket::State.

intQSocket::ungetch(int) [virtual]

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.

Reimplemented from QIODevice.

intQSocket::waitForMore(intmsecs)const

Wait upto msecs milliseconds for more data to be available.

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().

intQSocket::writeBlock(constchar*data, uintlen) [virtual]

Writes len bytes to the socket from data and returns the number of bytes written. Returns -1 if an error occurred.

Reimplemented from QIODevice.


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.


Copyright 2000 TrolltechTrademarks
Qt version 2.3.1