QSocketNotifier Class ReferenceThe QSocketNotifier class provides support for socket callbacks. More... #include <qsocketnotifier.h> Inherits QObject. Public Members
Signals
Detailed DescriptionThe QSocketNotifier class provides support for socket callbacks.
This class makes it possible to write asynchronous socket-based code in Qt. 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 QSocketNotifier 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:
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 any QObject, e.g. a widget. This will ensure that it is 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. (The same applies to exception notifiers if you must use them, for instance if you must use TCP urgent data.) 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 QTimer class. Socket action is detected in the main event loop of Qt. The X11 version of Qt 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 QSocket, QServerSocket, QSocketDevice, and Input/Output and Networking. Member Type Documentation
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.
CommunautéRessources
Liens utilesContact
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 3.2 | |
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 ! |
Copyright © 2000-2012 - www.developpez.com