📄 qsocketnotifier.3qt
字号:
.TH QSocketNotifier 3qt "3 September 2000" "Trolltech AS" \" -*- nroff -*-.\" Copyright 1992-2000 Trolltech AS. All rights reserved. See the.\" license file included in the distribution for a complete license.\" statement..\".ad l.nh.SH NAMEQSocketNotifier \- Support for socket callbacks.SH SYNOPSIS.br.PP\fC#include <qsocketnotifier.h>\fR.PPInherits QObject..PP.SS "Public Members".in +1c.ti -1c.BI "enum \fBType\fR { Read, Write, Exception }".br.ti -1c.BI "\fBQSocketNotifier\fR ( int " "socket" ", Type, QObject * " "parent" "=0, const char * " "name" "=0 ) ".br.ti -1c.BI "\fB~QSocketNotifier\fR () ".br.ti -1c.BI "int \fBsocket\fR () const".br.ti -1c.BI "Type \fBtype\fR () const".br.ti -1c.BI "bool \fBisEnabled\fR () const".br.ti -1c.BI "virtual void \fBsetEnabled\fR ( bool ) ".br.in -1c.SS "Signals".in +1c.ti -1c.BI "void \fBactivated\fR ( int socket ) ".br.in -1c.SH DESCRIPTIONThe QSocketNotifer class provides support for socket callbacks..PPThis 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..PPOnce 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..PPThere are three types of socket notifiers (read, write and exception) and you must specify one of these in the constructor..PPThe type specifies when the activated() signal is to be emitted:.IP 1\fCQSocketNotifier::Read:\fR There is data to be read (socket read event)..IP 2\fCQSocketNotifier::Write:\fR Data can be written (socket write event)..IP 3\fCQSocketNofifier::Exception:\fR An exception has occurred (socket exception event). We recommend against using this..PPFor example, if you need to monitor both reads and writes for the same socket, you must create two socket notifiers..PPExample:.PP.nf.br int sockfd; // socket identifier.br struct sockaddr_in sa; // should contain host address.br sockfd = socket( AF_INET, SOCK_STREAM, 0 ); // create TCP socket.br // make the socket non-blocking here, usually using fcntl( O_NONBLOCK ).br ::connect( sockfd, (struct sockaddr*)&sa, // connect to host.br sizeof(sa) ); // NOT QObject::connect()!.br QSocketNotifier *sn;.br sn = new QSocketNotifier( sockfd, QSocketNotifier::Read, parent );.br QObject::connect( sn, SIGNAL(activated(int)),.br myObject, SLOT(dataReceived()) );.fi.PPThe optional \fIparent\fR argument can be set to make the socket notifier a child of some widget and therefore be automatically destroyed when the widget is destroyed..PPFor 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..PPAlso observe that if you do not read all the available data when the read notifier fires, it fires again and again..PPIf 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 \fIhave\fR to use TCP urgent data.).PPFor 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..PPThe 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..PPIf you need a time-out for your sockets, you can use either timer events or the QTimer class..PPSocket 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..PPNote 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..PPSee also QSocket, QServerSocket and QSocketDevice..SH MEMBER FUNCTION DOCUMENTATION.SH "QSocketNotifier::QSocketNotifier ( int socket, Type type, QObject * parent=0, const char * name=0 )"Constructs a socket notifier with a \fIparent\fR and a \fIname.\fR.PPArguments:.TP\fIsocket\fR is the socket to be monitored..TP\fItype\fR specifies the socket operation to be detected; \fCQSocketNotifier::Read, QSocketNotifier::Write\fR or \fCQSocketNotifier::Exception.\fR The \fIparent\fR and \fIname\fR arguments are sent to the QObject constructor..PPThe socket notifier is initially enabled. It is generally advisable to explicitly enable or disable it, especially for write notifiers..PPSee also setEnabled() and isEnabled()..SH "QSocketNotifier::~QSocketNotifier ()"Destructs the socket notifier..SH "void QSocketNotifier::activated ( int socket ) \fC[signal]\fR"This signal is emitted under certain conditions, specified by the notifier type:.IP 1\fCQSocketNotifier::Read:\fR There is data to be read (socket read event)..IP 2\fCQSocketNotifier::Write:\fR Data can be written (socket write event)..IP 3\fCQSocketNofifier::Exception:\fR An exception has occurred (socket exception event)..PPThe \fIsocket\fR argument is the socket identifier..PPSee also type() and socket()..SH "bool QSocketNotifier::event ( QEvent * e ) \fC[virtual protected]\fR"Reimplemented for internal reasons; the API is not affected..PPReimplemented from QObject..SH "bool QSocketNotifier::isEnabled () const"Returns TRUE if the notifier is enabled, or FALSE if it is disabled..PPSee also setEnabled()..SH "void QSocketNotifier::setEnabled ( bool enable ) \fC[virtual]\fR"Enables the notifier if \fIenable\fR is TRUE, or disables it if \fIenable\fR is FALSE..PPThe notifier is by default enabled..PPIf 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)..PPWrite notifiers should normally be disabled immediately after the activated() signal has been emitted; see discussion of write notifiers in the class description above..PPSee also isEnabled() and activated()..SH "int QSocketNotifier::socket () const"Returns the socket identifier specified to the constructor..PPSee also type()..SH "Type QSocketNotifier::type () const"Returns the socket event type specified to the constructor; \fCQSocketNotifier::Read, QSocketNotifier::Write\fR or \fCQSocketNotifier::Exception.\fR.PPSee also socket()..SH "SEE ALSO".BR http://doc.trolltech.com/qsocketnotifier.html.SH COPYRIGHTCopyright 1992-2000 Trolltech AS, http://www.trolltech.com/. See thelicense file included in the distribution for a complete licensestatement..SH AUTHORGenerated automatically from the source code.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -