Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQSocket Class Reference
[network module]

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

#include <ntqsocket.h>

Inherits TQObject and TQIODevice.

List of all member functions.

Public Members

Signals


Detailed Description

The TQSocket class provides a buffered TCP connection.

It provides a totally non-blocking TQIODevice, and modifies and extends the API of TQIODevice with socket-specific code.

Note that a TQApplication must have been constructed before this class can be used.

The functions you're likely to call most are connectToHost(), bytesAvailable(), canReadLine() and the ones it inherits from TQIODevice.

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 TQSocket 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()). socketDevice() returns a pointer to the TQSocketDevice used for this socket.

TQSocket inherits TQIODevice, and reimplements some functions. In general, you can treat it as a TQIODevice for writing, and mostly also for reading. The match isn't perfect, since the TQIODevice 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 TQIODevice::size() exactly. The documentation for open(), close(), flush(), size(), at(), atEnd(), readBlock(), writeBlock(), getch(), putch(), ungetch() and readLine() describes the differences in detail.

Warning: TQSocket is not suitable for use in threads. If you need to uses sockets in threads use the lower-level TQSocketDevice class.

Warning: Because TQt doesn't use the native socketstream implementation on Mac OS X, TQSocket has an implicit transfer latency of 100ms. You can achieve lower latency on Mac OS X by using TQSocketDevice instead.

See also TQSocketDevice, TQHostAddress, TQSocketNotifier, and Input/Output and Networking.


Member Type Documentation

TQSocket::Error

This enum specifies the possible errors:

TQSocket::State

This enum defines the connection states:


Member Function Documentation

TQSocket::TQSocket ( TQObject * parent = 0, const char * name = 0 )

Creates a TQSocket object in TQSocket::Idle state.

The parent and name arguments are passed on to the TQObject constructor.

Note that a TQApplication must have been constructed before sockets can be used.

TQSocket::~TQSocket () [virtual]

Destroys the socket. Closes the connection if necessary.

See also close().

TQHostAddress TQSocket::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.)

Offset TQSocket::at () const [virtual]

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

Reimplemented from TQIODevice.

bool TQSocket::at ( Offset index ) [virtual]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

Reimplemented from TQIODevice.

bool TQSocket::atEnd () const [virtual]

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

Reimplemented from TQIODevice.

TQ_ULONG TQSocket::bytesAvailable () const

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

This function can trigger the readyRead() signal, if more data has arrived on the socket.

See also bytesToWrite().

Example: network/networkprotocol/nntp.cpp.

TQ_ULONG TQSocket::bytesToWrite () const

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

See also bytesAvailable() and clearPendingData().

void TQSocket::bytesWritten ( int nbytes ) [signal]

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

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

See also writeBlock() and bytesToWrite().

bool TQSocket::canReadLine () const

Returns TRUE if it's possible to read an entire line of text from this socket at this time; otherwise returns FALSE.

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.

void TQSocket::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.

See also bytesToWrite(), close(), and delayedCloseFinished().

void TQSocket::close () [virtual]

Closes the socket.

The read buffer is cleared.

If the output buffer is empty, the state is set to TQSocket::Idle and the connection is terminated immediately. If the output buffer still contains data to be written, TQSocket goes into the TQSocket::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 TQSocket::Idle and the connection is terminated. At this point, the delayedCloseFinished() signal is emitted.

If you don't want that the data of the output buffer is written, call clearPendingData() before you call close().

See also state(), bytesToWrite(), and clearPendingData().

Examples: network/clientserver/client/client.cpp, network/httpd/httpd.cpp, and network/networkprotocol/nntp.cpp.

Reimplemented from TQIODevice.

void TQSocket::connectToHost ( const TQString & host, TQ_UINT16 port ) [virtual]

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

Any connection or pending connection is closed immediately, and TQSocket 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. TQSocket 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.

void TQSocket::connected () [signal]

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.

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

Examples: network/clientserver/client/client.cpp, network/clientserver/server/server.cpp, and network/networkprotocol/nntp.cpp.

void TQSocket::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, TQSocket goes into the TQSocket::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.

void TQSocket::error ( int ) [signal]

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.

void TQSocket::flush () [virtual]

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

Reimplemented from TQIODevice.

int TQSocket::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 TQIODevice.

void TQSocket::hostFound () [signal]

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

See also connected().

Example: network/networkprotocol/nntp.cpp.

bool TQSocket::open ( int m ) [virtual]

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

See also close().

Reimplemented from TQIODevice.

TQHostAddress TQSocket::peerAddress () const

Returns the address of the connected peer if the socket is in Connected state; otherwise an empty TQHostAddress is returned.

TQString TQSocket::peerName () const

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

Example: network/mail/smtp.cpp.

TQ_UINT16 TQSocket::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.

Note that TQt always uses native byte order, i.e. 67 is 67 in TQt; there is no need to call htons().

TQ_UINT16 TQSocket::port () const

Returns the host port number of this socket, in native byte order.

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

Writes the character ch to the output buffer.

Returns ch, or -1 if an error occurred.

See also getch().

Reimplemented from TQIODevice.

TQ_LONG TQSocket::readBlock ( char * data, TQ_ULONG maxlen ) [virtual]

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

Example: network/networkprotocol/nntp.cpp.

Reimplemented from TQIODevice.

TQ_ULONG TQSocket::readBufferSize () const

Returns the size of the read buffer.

See also setReadBufferSize().

TQString TQSocket::readLine () [virtual]

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

See also canReadLine().

Examples: network/clientserver/client/client.cpp, network/httpd/httpd.cpp, network/mail/smtp.cpp, and network/networkprotocol/nntp.cpp.

void TQSocket::readyRead () [signal]

This signal is emitted every time there is new incoming data.

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.

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.

void TQSocket::setReadBufferSize ( TQ_ULONG bufSize )

Sets the size of the TQSocket's internal read buffer to bufSize.

Usually TQSocket 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 TQSocket class doesn't buffer more than this size of data.

If the size of the read buffer is 0, the read buffer is unlimited and all incoming data is buffered. This is the default.

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.

See also readBufferSize().

void TQSocket::setSocket ( int socket ) [virtual]

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

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

Example: network/httpd/httpd.cpp.

void TQSocket::setSocketDevice ( TQSocketDevice * device ) [virtual]

Sets the internal socket device to device. Passing a device of 0 will cause the internal socket device to be used. Any existing connection will be disconnected before using the new device.

The new device should not be connected before being associated with a TQSocket; after setting the socket call connectToHost() to make the connection.

This function is useful if you need to subclass TQSocketDevice and want to use the TQSocket API, for example, to implement Unix domain sockets.

Offset TQSocket::size () const [virtual]

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

Reimplemented from TQIODevice.

int TQSocket::socket () const

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

TQSocketDevice * TQSocket::socketDevice ()

Returns a pointer to the internal socket device.

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

State TQSocket::state () const

Returns the current state of the socket connection.

See also TQSocket::State.

Examples: network/clientserver/client/client.cpp and network/networkprotocol/nntp.cpp.

int TQSocket::ungetch ( int ch ) [virtual]

This implementation of the virtual function TQIODevice::ungetch() prepends the character ch to the read buffer so that the next read returns this character as the first character of the output.

Reimplemented from TQIODevice.

TQ_ULONG TQSocket::waitForMore ( int msecs, bool * timeout ) const

Wait up to msecs milliseconds for more data to be available.

If msecs is -1 the call will block indefinitely.

Returns the number of bytes available.

If timeout is non-null and no error occurred (i.e. it does not return -1): this function sets *timeout to TRUE, if the reason for returning was that the timeout was reached; otherwise it sets *timeout to FALSE. This is useful to find out if the peer closed the connection.

Warning: This is a blocking call and should be avoided in event driven applications.

See also bytesAvailable().

TQ_ULONG TQSocket::waitForMore ( int msecs ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

TQ_LONG TQSocket::writeBlock ( const char * data, TQ_ULONG len ) [virtual]

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

Example: network/networkprotocol/nntp.cpp.

Reimplemented from TQIODevice.


This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8