com.trolltech.qt.core
Class QSocketNotifier

java.lang.Object
  extended by com.trolltech.qt.internal.QSignalEmitterInternal
      extended by com.trolltech.qt.QSignalEmitter
          extended by com.trolltech.qt.QtJambiObject
              extended by com.trolltech.qt.core.QObject
                  extended by com.trolltech.qt.core.QSocketNotifier
All Implemented Interfaces:
QtJambiInterface

public class QSocketNotifier
extends QObject

The QSocketNotifier class provides support for monitoring activity on a file descriptor. The QSocketNotifier makes it possible to integrate Qt's event loop with other event loops based on file descriptors. For example, the CORBA Framework uses it to process CORBA events. File descriptor action is detected in Qt's main event loop (QCoreApplication::exec()). Once you have opened a device using a low-level (usually platform-specific) API, you can create a socket notifier to monitor the file descriptor. The socket notifier is enabled by default, i.e. it emits the activated() signal whenever a socket event corresponding to its type occurs. Connect the activated() signal to the slot you want to be called when an event corresponding to your socket notifier's type occurs.

There are three types of socket notifiers: read, write, and exception. The type is described by the Type enum, and must be specified when constructing the socket notifier. After construction it can be determined using the type() function. Note that if you need to monitor both reads and writes for the same file descriptor, you must create two socket notifiers. Note also that it is not possible to install two socket notifiers of the same type (Read , Write , Exception ) on the same socket.

The setEnabled() function allows you to disable as well as enable the socket notifier. It is generally advisable to explicitly enable or disable the socket notifier, especially for write notifiers. A disabled notifier ignores socket events (the same effect as not creating the socket notifier). Use the isEnabled() function to determine the notifier's current status.

Finally, you can use the socket() function to retrieve the socket identifier. Although the class is called QSocketNotifier, it is normally used for other types of devices than sockets. QTcpSocket and QUdpSocket provide notification through signals, so there is normally no need to use a QSocketNotifier on them.

Notes for Windows Users

The socket passed to QSocketNotifier will become non-blocking, even if it was created as a blocking socket. The activated() signal is sometimes triggered by high general activity on the host, even if there is nothing to read. A subsequent read from the socket can then fail, the error indicating that there is no data available (e.g., WSAEWOULDBLOCK). This is an operating system limitation, and not a bug in QSocketNotifier.

To ensure that the socket notifier handles read notifications correctly, follow these steps when you receive a notification:

  1. Disable the notifier.
  2. Read data from the socket.
  3. Re-enable the notifier if you are interested in more data (such as after having written a new command to a remote server).
To ensure that the socket notifier handles write notifications correctly, follow these steps when you receive a notification:
  1. Disable the notifier.
  2. Write as much data as you can (before EWOULDBLOCK is returned).
  3. Re-enable notifier if you have more data to write.
Further information: On Windows, Qt always disables the notifier after getting a notification, and only re-enables it if more data is expected. For example, if data is read from the socket and it can be used to read more, or if reading or writing is not possible because the socket would block, in which case it is necessary to wait before attempting to read or write again.

See also:
QFile, QProcess, QTcpSocket, and QUdpSocket.


Nested Class Summary
static class QSocketNotifier.Type
          This enum describes the various types of events that a socket notifier can recognize.
 
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
QSignalEmitter.AbstractSignal, QSignalEmitter.PrivateSignal0, QSignalEmitter.PrivateSignal1, QSignalEmitter.PrivateSignal2, QSignalEmitter.PrivateSignal3, QSignalEmitter.PrivateSignal4, QSignalEmitter.PrivateSignal5, QSignalEmitter.PrivateSignal6, QSignalEmitter.PrivateSignal7, QSignalEmitter.PrivateSignal8, QSignalEmitter.PrivateSignal9, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9
 
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal
 
Field Summary
 QSignalEmitter.Signal1 activated
          This signal takes 1 generic argument(s).
 
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
currentSender
 
Constructor Summary
QSocketNotifier(int socket, QSocketNotifier.Type arg__2)
          Constructs a socket notifier with the given parent.
QSocketNotifier(int socket, QSocketNotifier.Type arg__2, QObject parent)
          Constructs a socket notifier with the given parent.
 
Method Summary
 boolean isEnabled()
          Returns true if the notifier is enabled; otherwise returns false.
 void setEnabled(boolean arg__1)
          If enable is true, the notifier is enabled; otherwise the notifier is disabled.
 int socket()
          Returns the socket identifier specified to the constructor.
 QSocketNotifier.Type type()
          Returns the socket event type specified to the constructor.
 
Methods inherited from class com.trolltech.qt.core.QObject
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty
 
Methods inherited from class com.trolltech.qt.QtJambiObject
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr
 
Methods inherited from class com.trolltech.qt.QSignalEmitter
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread
 
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
__qt_signalInitialization
 
Methods inherited from class java.lang.Object
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface com.trolltech.qt.QtJambiInterface
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership
 

Field Detail

activated

public final QSignalEmitter.Signal1 activated

This signal takes 1 generic argument(s). We list their type and the name they go by in the description of this signal. <java.lang.Integer(named: socket)>:

This signal is emitted whenever the socket notifier is enabled and a socket event corresponding to its type occurs.

The socket identifier is passed in the socket parameter.

See also:
type(), and socket().

Constructor Detail

QSocketNotifier

public QSocketNotifier(int socket,
                       QSocketNotifier.Type arg__2)
Constructs a socket notifier with the given parent. It enables the socket, and watches for events of the given type.

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

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

See also:
setEnabled(), and isEnabled().


QSocketNotifier

public QSocketNotifier(int socket,
                       QSocketNotifier.Type arg__2,
                       QObject parent)
Constructs a socket notifier with the given parent. It enables the socket, and watches for events of the given type.

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

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

See also:
setEnabled(), and isEnabled().

Method Detail

isEnabled

public final boolean isEnabled()
Returns true if the notifier is enabled; otherwise returns false.

See also:
setEnabled().


setEnabled

public final void setEnabled(boolean arg__1)
If enable is true, the notifier is enabled; otherwise the notifier is disabled.

The notifier is enabled by default, i.e. 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 also:
isEnabled(), and activated() .


socket

public final int socket()
Returns the socket identifier specified to the constructor.

See also:
type().


type

public final QSocketNotifier.Type type()
Returns the socket event type specified to the constructor.

See also:
socket().