qsocketnotifier.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 202 行
HTML
202 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/src/kernel/qsocketnotifier.cpp:45 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>QSocketNotifier Class</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QSocketNotifier Class Reference</h1><p>The QSocketNotifier class provides support for socket callbacks.<a href="#details">More...</a><p><tt>#include <<a href="qsocketnotifier-h.html">qsocketnotifier.h</a>></tt><p>Inherits <a href="qobject.html">QObject</a>.<p><a href="qsocketnotifier-members.html">List of all member functions.</a><h2>Public Members</h2><ul><li><div class=fn>enum <a href="#Type-enum"><b>Type</b></a> { Read, Write, Exception }</div></li><li><div class=fn><a href="#QSocketNotifier"><b>QSocketNotifier</b></a> ( int socket, Type type, QObject * parent = 0, const char * name = 0 )</div></li><li><div class=fn><a href="#~QSocketNotifier"><b>~QSocketNotifier</b></a> ()</div></li><li><div class=fn>int <a href="#socket"><b>socket</b></a> () const</div></li><li><div class=fn>Type <a href="#type"><b>type</b></a> () const</div></li><li><div class=fn>bool <a href="#isEnabled"><b>isEnabled</b></a> () const</div></li><li><div class=fn>virtual void <a href="#setEnabled"><b>setEnabled</b></a> ( bool enable )</div></li></ul><h2>Signals</h2><ul><li><div class=fn>void <a href="#activated"><b>activated</b></a> ( int socket )</div></li></ul><hr><a name="details"></a><h2>Detailed Description</h2>The QSocketNotifier class provides support for socket callbacks.<p> <p> This class makes it possible to write asynchronous socket-basedcode in Qt. Using synchronous socket operations blocks theprogram, which is clearly not acceptable for an event-driven GUIprogram.<p> Once you have opened a non-blocking socket (whether for TCP, UDP,a UNIX-domain socket, or any other protocol family your operatingsystem supports), you can create a socket notifier to monitor thesocket. Then you connect the <a href="#activated">activated</a>() signal to the slot youwant to be called when a socket event occurs.<p> There are three types of socket notifiers (read, write andexception); you must specify one of these in the constructor.<p> The type specifies when the activated() signal is to be emitted:<ol type=1><li> QSocketNotifier::Read - There is data to be read (socket read event).<li> QSocketNotifier::Write - Data can be written (socket write event).<li> QSocketNofifier::Exception - An exception has occurred (socketexception event). We recommend against using this.</ol><p> For example, if you need to monitor both reads and writes for thesame socket you must create two socket notifiers.<p> Example:<pre> int sockfd; // socket identifier struct sockaddr_in sa; // should contain host address sockfd = <a href="#socket">socket</a>( 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::<a href="#Type-enum">Read</a>, parent ); QObject::<a href="qobject.html#connect">connect</a>( sn, SIGNAL(<a href="#activated">activated</a>(int)), myObject, SLOT(dataReceived()) ); </pre> <p> The optional <em>parent</em> argument can be set to make the socketnotifier a child of any <a href="qobject.html">QObject</a>, e.g. a widget. This will ensurethat it is automatically destroyed when the widget is destroyed.<p> For read notifiers it makes little sense to connect the<a href="#activated">activated</a>() signal to more than one slot because the data can beread from the socket only once.<p> Also observe that if you do not read all the available data whenthe read notifier fires, it fires again and again.<p> If you disable the read notifier your program may deadlock. (Thesame applies to exception notifiers if you must use them, forinstance if you <em>must</em> use TCP urgent data.)<p> For write notifiers, immediately disable the notifier after theactivated() signal has been received and you have sent the data tobe written on the socket. When you have more data to be written,enable it again to get a new activated() signal. The exception isif the socket data writing operation (send() or equivalent) failswith a "would block" error, which means that some buffer is fulland you must wait before sending more data. In that case you donot need to disable and re-enable the write notifier; it will fireagain as soon as the system allows more data to be sent.<p> The behavior of a write notifier that is left in enabled stateafter having emitting the first <a href="#activated">activated</a>() signal (and no "wouldblock" error has occurred) is undefined. Depending on theoperating system, it may fire on every pass of the event loop ornot at all.<p> If you need a time-out for your sockets you can use either <a href="qobject.html#startTimer">timer events</a> or the <a href="qtimer.html">QTimer</a> class.<p> Socket action is detected in the <a href="qapplication.html#exec">main event loop</a> of Qt. The X11 version of Qt has a single UNIXselect() call that incorporates all socket notifiers and the Xsocket.<p> Note that on XFree86 for OS/2, select() works only in the threadin which main() is running; you should therefore use that threadfor GUI operations.<p> <p>See also <a href="qsocket.html">QSocket</a>, <a href="qserversocket.html">QServerSocket</a>, <a href="qsocketdevice.html">QSocketDevice</a> and <a href="io.html">Input/Output and Networking</a>.<hr><h2>Member Type Documentation</h2><h3 class=fn><a name="Type-enum"></a>QSocketNotifier::Type</h3><ul><li><tt>QSocketNotifier::Read</tt><li><tt>QSocketNotifier::Write</tt><li><tt>QSocketNotifier::Exception</tt></ul><hr><h2>Member Function Documentation</h2><h3 class=fn><a name="QSocketNotifier"></a>QSocketNotifier::QSocketNotifier ( int socket, <a href="qsocketnotifier.html#Type-enum">Type</a> type, <a href="qobject.html">QObject</a> * parent = 0, const char * name = 0 )</h3>Constructs a socket notifier called <em>name</em>, with the parent, <em>parent</em>. It watches <em>socket</em> for <em>type</em> events, and enables it.<p> It is generally advisable to explicitly enable or disable thesocket notifier, especially for write notifiers.<p> <p>See also <a href="#setEnabled">setEnabled</a>() and <a href="#isEnabled">isEnabled</a>().<h3 class=fn><a name="~QSocketNotifier"></a>QSocketNotifier::~QSocketNotifier ()</h3>Destroys the socket notifier.<h3 class=fn>void <a name="activated"></a>QSocketNotifier::activated ( int socket )<tt> [signal]</tt></h3><p> This signal is emitted under certain conditions specified by thenotifier <a href="#type">type</a>():<ol type=1><li> QSocketNotifier::Read - There is data to be read (socket read event).<li> QSocketNotifier::Write - Data can be written (socket write event).<li> QSocketNofifier::Exception - An exception has occurred (socketexception event).</ol><p> The <em>socket</em> argument is the <a href="#socket">socket</a> identifier.<p> <p>See also <a href="#type">type</a>() and <a href="#socket">socket</a>().<h3 class=fn>bool <a name="isEnabled"></a>QSocketNotifier::isEnabled () const</h3><p> Returns TRUE if the notifier is enabled; otherwise returns FALSE.<p> <p>See also <a href="#setEnabled">setEnabled</a>().<h3 class=fn>void <a name="setEnabled"></a>QSocketNotifier::setEnabled ( bool enable )<tt> [virtual]</tt></h3>Enables the notifier if <em>enable</em> is TRUE or disables it if <em>enable</em> is FALSE.<p> The notifier is enabled by default.<p> If the notifier is enabled, it emits the <a href="#activated">activated</a>() signalwhenever a socket event corresponding to its <a href="#type">type</a> occurs. If it is disabled, it ignores socket events(the same effect as not creating the socket notifier).<p> Write notifiers should normally be disabled immediately after theactivated() signal has been emitted; see discussion of writenotifiers in the <a href="#details">class description</a> above.<p> <p>See also <a href="#isEnabled">isEnabled</a>() and <a href="#activated">activated</a>().<h3 class=fn>int <a name="socket"></a>QSocketNotifier::socket () const</h3><p> Returns the socket identifier specified to the constructor.<p> <p>See also <a href="#type">type</a>().<h3 class=fn><a href="qsocketnotifier.html#Type-enum">Type</a> <a name="type"></a>QSocketNotifier::type () const</h3><p> Returns the socket event type specified to the constructor: <a href="#Type-enum">QSocketNotifier::Read</a>, <a href="#Type-enum">QSocketNotifier::Write</a>, or <a href="#Type-enum">QSocketNotifier::Exception</a>.<p> <p>See also <a href="#socket">socket</a>().<!-- eof --><hr><p>This file is part of the <a href="index.html">Qt toolkit</a>.Copyright © 1995-2002<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?