Qt logo

QSocket Class Reference


The QSocket class provides a buffered socket 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 socket connection.

This class provides a buffered TCP connection over a socket. Both read and write operations are buffered.

See also: QSocketDevice, QSocketAddress and QSocketNotifier.


Member Type Documentation

QSocket::Mode

This enum specifies the communication mode:

QSocket::State

This enum contains the connection states:

Member Function Documentation

QSocket::QSocket ( QObject * parent=0, const char * name=0 )

Creates a QSocket object in QSocket::Idle state.

This socket can be used to make a connection to a host using the connectToHost() function.

QSocket::QSocket ( int socket, QObject * parent=0, const char * name=0 )

Creates a QSocket object for an existing connection using socket.

The socket is set to nonblocking and the socket option QSocketDevice::ReuseAddress is enabled.

QSocket::~QSocket ()

Destroys the socket. Closes the connection if necessary.

See also: close().

bool QSocket::at ( int index ) [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.

int QSocket::at () const [virtual]

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

Reimplemented from QIODevice.

bool QSocket::atEnd () const [virtual]

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

Reimplemented from QIODevice.

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

See also: bytesToWrite();.

int QSocket::bytesToWrite () const

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

See also: bytesAvailable.

void QSocket::bytesWritten ( int nbytes ) [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().

bool QSocket::canReadLine () const

Returns TRUE if the socket is in ascii mode and readLine() can be called to read a line of text. Otherwise FALSE is returned.

See also: setMode() and readLine().

void QSocket::close () [virtual]

Closes the socket. The mode 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.

void QSocket::closed () [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().

bool QSocket::connectToHost ( const QString & host, int port )

Attempts to make a connection to host on the specified port.

Any connection or pending connection is closed immediately when you call this function.

When making a connection, QSocket goes into the QSocket::HostLookup state while looking up the host. When the host has been found, QSocket will try to connect to the host and it goes into the QSocket::Connecting state. Finally, when a connection has been made, the state becomes QSocket::Connection.

TODO### No real error handling so far. Blocking host lookup.

See also: state().

void QSocket::connected () [signal]

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

See also: connectToHost() and closed().

void QSocket::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().

void QSocket::flush () [virtual]

Implementation of the abstract virtual QIODevice::flush() function. This implementation is a no-op.

Reimplemented from QIODevice.

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

QString QSocket::host () const

Returns the host name as specified to the connectToHost() function. If none has been set, the returned host name is "".

void QSocket::hostFound () [signal]

This signal is emitted after connectToHost() has been called and a host has been looked up.

See also: connected().

QSocket::Mode QSocket::mode() const

Returns the current communication mode, either QSocket::Binary or QSocket::Ascii.

The default mode is QSocket::Binary.

The documentation of setMode() explains this mode setting.

See also: setMode().

bool QSocket::open ( int m ) [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.

int QSocket::port () const

Returns the host port as specified to the connectToHost() function. If none has been set, the returned port is 0.

int QSocket::putch ( int ch ) [virtual]

Writes the character ch into the output buffer.

Returns ch, or -1 if some error occurred.

See also: getch().

Reimplemented from QIODevice.

int QSocket::readBlock ( char * data, uint maxlen ) [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.

QString QSocket::readLine ()

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

See also: canReadLine().

void QSocket::readyRead () [signal]

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

When new data comes into the socket device, this signal is emitted immediately. If QSocket has old incoming data that you have not read, the signal is emitted once every second until you read everything.

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

void QSocket::setMode ( Mode mode )

Sets the communication mode to either binary or ascii.

QSocket::Binary is the default.

The mode only relates to incoming data. If binary mode is set, you read data using the readBlock() function. bytesAvailable() tells you how much buffered data that can be read.

If ascii mode set, QSocket will look for newline characters (\n) in the incoming data. You can call the canReadLine() function to check if there is a line of text to be read. If canReadLine() returns TRUE, you can read a complete line of incoming text using the readLine() function. The readBlock() and bytesAvailable() functions work just like in binary mode.

The close() function sets the mode to QSocket::Binary.

See also: mode(), readBlock(), bytesAvailable(), canReadLine(), readLine() and close().

uint QSocket::size () const [virtual]

Returns the number of incoming bytes that can be read, same as bytesAvailable().

Reimplemented from QIODevice.

void QSocket::sn_read () [virtual protected slot]

Internal slot for handling socket read notifications.

void QSocket::sn_write () [virtual protected slot]

Internal slot for handling socket write notifications.

QSocketDevice * QSocket::socketDevice () [protected]

Returns a pointer to the internal socket device. The returned pointer is null if there is no connection or pending connection.

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

QSocket::State QSocket::state() const

Returns the current state of the socket connection.

void QSocket::timerEvent ( QTimerEvent * e ) [virtual protected]

Processes timer events for QSocket. Emits the readyRead() signal if there if there is buffered input available for reading.

Reimplemented from QObject.

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

int QSocket::writeBlock ( const char * data, uint len ) [virtual]

Writes len bytes from 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.troll.no):


This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.


Copyright © 1999 Troll TechTrademarks
Qt version 2.0.2