Äîêóìåíò âçÿò èç êýøà ïîèñêîâîé ìàøèíû. Àäðåñ
îðèãèíàëüíîãî äîêóìåíòà
: http://rtm-cs.sinp.msu.ru/manual/qt/qsocketnotifier.html
Äàòà èçìåíåíèÿ: Sun Jul 12 02:58:59 1998 Äàòà èíäåêñèðîâàíèÿ: Mon Oct 1 21:02:47 2012 Êîäèðîâêà: |
The QSocketNotifer class provides support for socket callbacks. More...
#include <qsocketnotifier.h>
Inherits QObject.
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:
QSocketNotifier::Read:
There is data to be read (socket read event).
QSocketNotifier::Write:
Data can be written (socket write event).
QSocketNofifier::Exception:
An exception has ocurred (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.
Make sure to disable the socket notifier for write operations when there is nothing to be written, otherwise the notifier fires on every pass of the main event loop. The socket notifier is enabled when it is created.
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.)
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.
Constructs a socket notifier with a parent and a name.
Arguments:
QSocketNotifier::Read, QSocketNotifier::Write
or
QSocketNotifier::Exception.
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().
Destroys the socket notifier.
[signal]
This signal is emitted under certain conditions, specified by the notifier type:
QSocketNotifier::Read:
There is data to be read (socket read event).
QSocketNotifier::Write:
Data can be written (socket write event).
QSocketNofifier::Exception:
An exception has ocurred (socket
exception event).
The socket argument is the socket identifier.
See also: type() and socket().
[virtual protected]
Handles events for the socket notifier object.
Emits the activated() signal when a Event_SockAct
is received.
Reimplemented from QObject.
Returns TRUE if the notifier is enabled, or FALSE if it is disabled.
See also: setEnabled().
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).
Disable the socket notifier for writes if there is nothing to be written, otherwise your program hogs the CPU.
See also: isEnabled() and activated().
Returns the socket identifier specified to the constructor.
See also: type().
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.troll.no):
This file is part of the Qt toolkit, copyright © 1995-98 Troll Tech, all rights reserved.
It was generated from the following files:
Copyright © 1998 Troll Tech | Trademarks | Qt version 1.40
|