TQSocketNotifier Class

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

# TQSocketNotifier Class Reference

The TQSocketNotifier class provides support for socket callbacks. More...

`#include <tqsocketnotifier.h>`

Inherits TQObject.

List of all member functions.

## Public Members

- enum Type { Read, Write, Exception }
- TQSocketNotifier ( int socket, Type type, TQObject * parent = 0, const char * name = 0 )
- ~TQSocketNotifier ()
- int socket () const
- Type type () const
- bool isEnabled () const
- virtual void setEnabled ( bool enable )

## Signals

- void activated ( int socket )

## Detailed Description

The TQSocketNotifier class provides support for socket callbacks.

This class makes it possible to write asynchronous socket-based code in TQt. Using synchronous socket operations blocks the program, which is clearly not acceptable for an event-driven GUI program.

Once you have opened a non-blocking socket (whether for TCP, UDP, a UNIX-domain socket, or any other protocol family your operating system supports), you can create a socket notifier to monitor the socket. Then you connect the activated() signal to the slot you want to be called when a socket event occurs.

Note for Windows users: the socket passed to TQSocketNotifier will become non-blocking, even if it was created as a blocking socket.

There are three types of socket notifiers (read, write and exception); you must specify one of these in the constructor.

The type specifies when the activated() signal is to be emitted:

1. TQSocketNotifier::Read - There is data to be read (socket read event).
2. TQSocketNotifier::Write - Data can be written (socket write event).
3. TQSocketNofifier::Exception - An exception has occurred (socket exception event). We recommend against using this.

For example, if you need to monitor both reads and writes for the same socket you must create two socket notifiers.

For read notifiers it makes little sense to connect the activated() signal to more than one slot because the data can be read from the socket only once.

Also observe that if you do not read all the available data when the read notifier fires, it fires again and again.

For write notifiers, immediately disable the notifier after the activated() signal has been received and you have sent the data to be written on the socket. When you have more data to be written, enable it again to get a new activated() signal. The exception is if the socket data writing operation (send() or equivalent) fails with a "would block" error, which means that some buffer is full and you must wait before sending more data. In that case you do not need to disable and re-enable the write notifier; it will fire again as soon as the system allows more data to be sent.

The behavior of a write notifier that is left in enabled state after having emitting the first activated() signal (and no "would block" error has occurred) is undefined. Depending on the operating system, it may fire on every pass of the event loop or not at all.

If you need a time-out for your sockets you can use either timer events or the TQTimer class.

Socket action is detected in the main event loop of TQt. The X11 version of TQt has a single UNIX select() call that incorporates all socket notifiers and the X socket.

Note that on XFree86 for OS/2, select() works only in the thread in which main() is running; you should therefore use that thread for GUI operations.

See also TQSocket, TQServerSocket, TQSocketDevice, TQFile::handle(), and Input/Output and Networking.

## Member Type Documentation

### TQSocketNotifier::Type

- `TQSocketNotifier::Read`
- `TQSocketNotifier::Write`
- `TQSocketNotifier::Exception`

## Member Function Documentation

### TQSocketNotifier::TQSocketNotifier ( int socket, Type type, TQObject * parent = 0, const char * name = 0 )

Constructs a socket notifier called name, with the parent, parent. It watches socket for type events, and enables it.

It is generally advisable to explicitly enable or disable the socket notifier, especially for write notifiers.

See also setEnabled() and isEnabled().

### TQSocketNotifier::~TQSocketNotifier ()

Destroys the socket notifier.

### void TQSocketNotifier::activated ( int socket )`[signal]`

This signal is emitted under certain conditions specified by the notifier type():

1. TQSocketNotifier::Read - There is data to be read (socket read event).
2. TQSocketNotifier::Write - Data can be written (socket write event).
3. TQSocketNofifier::Exception - An exception has occurred (socket exception event).

The socket argument is the socket identifier.

See also type() and socket().

### bool TQSocketNotifier::isEnabled () const

Returns true if the notifier is enabled; otherwise returns false.

See also setEnabled().

### void TQSocketNotifier::setEnabled ( bool enable )`[virtual]`

Enables the notifier if enable is true or disables it if enable is false.

The notifier is enabled by default.

If the notifier is enabled, it emits the activated() signal whenever a socket event corresponding to its type occurs. If it is disabled, it ignores socket events (the same effect as not creating the socket notifier).

Write notifiers should normally be disabled immediately after the activated() signal has been emitted; see discussion of write notifiers in the class description above.

See also isEnabled() and activated().

### int TQSocketNotifier::socket () const

Returns the socket identifier specified to the constructor.

See also type().

### Type TQSocketNotifier::type () const

Returns the socket event type specified to the constructor: TQSocketNotifier::Read, TQSocketNotifier::Write, or TQSocketNotifier::Exception.

See also socket().

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