Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Classes  ·  Annotées  ·  Hiérarchie  ·  Fonctions  ·  Structure  · 

QSocketNotifier Class Reference


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

#include <qsocketnotifier.h>

Inherits QObject.

List of all member functions.

Public Members

  • enum Type { Read, Write, Exception }
  • QSocketNotifier ( int socket, Type, QObject * parent=0, const char * name=0 ) 
  • int socket () const
  • Type type () const
  • bool isEnabled () const
  • virtual void setEnabled ( bool ) 

Signals

  • void activated ( int socket ) 

Detailed Description

The QSocketNotifer class provides support for socket callbacks.

This class makes it possible to write e.g. asynchronous TCP/IP socket-based code in Qt. Using synchronous socket operations blocks the program, which is clearly not acceptable for an event-based GUI program.

Once you have opened a non-blocking socket (either 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 connect the activated() signal to the slot you want to be called when a socket event occurs.

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

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

  1. QSocketNotifier::Read: There is data to be read (socket read event).
  2. QSocketNotifier::Write: Data can be written (socket write event).
  3. QSocketNofifier::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.

Example:

    int sockfd;                                 // socket identifier
    struct sockaddr_in sa;                      // should contain host address
    sockfd = socket( AF_INET, SOCK_STREAM, 0 ); // create TCP socket
    // make the socket non-blocking here, usually using fcntl( O_NONBLOCK )
    ::connect( sockfd, (struct sockaddr*)&sa,   // connect to host
               sizeof(sa) );                    //   NOT QObject::connect()!
    QSocketNotifier *sn;
    sn = new QSocketNotifier( sockfd, QSocketNotifier::Read, parent );
    QObject::connect( sn, SIGNAL(activated(int)),
                      myObject, SLOT(dataReceived()) );

The optional parent argument can be set to make the socket notifier a child of some widget and therefore be automatically destroyed when the widget is destroyed.

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.

If you disable the read notifier, your program may deadlock. Avoid it if you do not know what you are doing. (The same applies to exception notifiers if you have to use that, for instance if you have to use TCP urgent data.)

For write notifiers, after the activated() signal has been received and you have sent the data to be written on the socket, immediately disable the notifier. 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, meaning that some buffer is full and you must wait before sending more data. In this 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 may be sent.

The behaviour 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 QTimer class.

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

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

See also QSocket, QServerSocket and QSocketDevice.


Member Function Documentation

QSocketNotifier::QSocketNotifier ( int socket, Type type, QObject * parent=0, const char * name=0 )

Constructs a socket notifier with a parent and a name.

Arguments:

  • socket is the socket to be monitored.
  • type specifies the socket operation to be detected; QSocketNotifier::Read, QSocketNotifier::Write or QSocketNotifier::Exception.
The parent and name arguments are sent to the QObject constructor.

The socket notifier is initially enabled. It is generally advisable to explicitly enable or disable it, especially for write notifiers.

See also setEnabled() and isEnabled().

QSocketNotifier::~QSocketNotifier ()

Destructs the socket notifier.

void QSocketNotifier::activated ( int socket ) [signal]

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

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

The socket argument is the socket identifier.

See also type() and socket().

bool QSocketNotifier::event ( QEvent * e ) [virtual protected]

Reimplemented for internal reasons; the API is not affected.

Reimplemented from QObject.

bool QSocketNotifier::isEnabled () const

Returns TRUE if the notifier is enabled, or FALSE if it is disabled.

See also setEnabled().

void QSocketNotifier::setEnabled ( bool enable ) [virtual]

Enables the notifier if enable is TRUE, or disables it if enable is FALSE.

The notifier is by default enabled.

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 QSocketNotifier::socket () const

Returns the socket identifier specified to the constructor.

See also type().

Type QSocketNotifier::type () const

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

See also socket().


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


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

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 79
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. 2017 : un quinquennat pour une nouvelle version du C++ ? Possible, selon Herb Sutter 6
Page suivante

Le Qt Developer Network au hasard

Logo

Les bases

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 2.3
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web