File: qsocketnotifier.html

package info (click to toggle)
qt-x11 3%3A2.3.1-22
  • links: PTS
  • area: main
  • in suites: woody
  • size: 48,524 kB
  • ctags: 46,337
  • sloc: cpp: 260,077; ansic: 32,457; makefile: 31,131; yacc: 2,444; sh: 1,513; lex: 480; perl: 422; xml: 68; lisp: 15
file content (182 lines) | stat: -rw-r--r-- 10,550 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Toolkit - 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: white; color: black; }
--></style>
</head><body bgcolor="#ffffff">

<table width="100%">
<tr><td><a href="index.html">
<img width="100" height="100" src="qtlogo.png"
alt="Home" border="0"><img width="100"
height="100" src="face.png" alt="Home" border="0">
</a><td valign=top><div align=right><img src="dochead.png" width="472" height="27"><br>
<a href="classes.html"><b>Classes</b></a>
-<a href="annotated.html">Annotated</a>
- <a href="hierarchy.html">Tree</a>
-<a href="functions.html">Functions</a>
-<a href="index.html">Home</a>
-<a href="topicals.html"><b>Structure</b></a>
</div>
</table>

<h1 align=center>QSocketNotifier Class Reference</h1><br clear="all">
<p>
The QSocketNotifer class provides support for socket callbacks.
<a href="#details">More...</a>
<p>
<code>#include &lt;<a href="qsocketnotifier-h.html">qsocketnotifier.h</a>&gt;</code>
<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<b>Type</b>{Read, Write, Exception}</div>
<li><div class="fn"><a href="#317dae"><b>QSocketNotifier</b></a>(intsocket, Type, QObject*parent=0, constchar*name=0)</div>
<li><div class="fn"><a href="#25bff9"><b>~QSocketNotifier</b></a>()</div>
<li><div class="fn">int<a href="#08d55e"><b>socket</b></a>()const</div>
<li><div class="fn">Type<a href="#dce885"><b>type</b></a>()const</div>
<li><div class="fn">bool<a href="#b4e547"><b>isEnabled</b></a>()const</div>
<li><div class="fn">virtualvoid<a href="#fc30c4"><b>setEnabled</b></a>(bool)</div>
</ul>
<h2>Signals</h2>
<ul>
<li><div class="fn">void<a href="#ee5bac"><b>activated</b></a>(intsocket)</div>
</ul>
<hr><h2><a name="details"></a>Detailed Description</h2>
The QSocketNotifer class provides support for socket callbacks.
<p>
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.
<p>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 <a href="#ee5bac">activated</a>() signal to the slot you want to
be called when a socket event occurs.
<p>There are three types of socket notifiers (read, write and exception)
and you must specify one of these in the constructor.
<p>The type specifies when the activated() signal is to be emitted:
<ol>
<li> <code>QSocketNotifier::Read:</code> There is data to be read (socket read event).
<li> <code>QSocketNotifier::Write:</code> Data can be written (socket write event).
<li> <code>QSocketNofifier::Exception:</code> An exception has occurred (socket
exception event).  We recommend against using this.
</ol>
<p>For example, if you need to monitor both reads and writes for the same
socket, you must create two socket notifiers.
<p>Example:
<pre>    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*)&amp;sa,   // connect to host
               sizeof(sa) );                    //   NOT QObject::connect()!
    <a href="qsocketnotifier.html">QSocketNotifier</a> *sn;
    sn = new <a href="qsocketnotifier.html">QSocketNotifier</a>( sockfd, QSocketNotifier::Read, parent );
    <a href="qobject.html#c2d209">QObject::connect</a>( sn, SIGNAL(activated(int)),
                      myObject, SLOT(dataReceived()) );
</pre>
<p>The optional <em>parent</em> argument can be set to make the socket notifier a
child of some widget and therefore be automatically destroyed when the
widget is destroyed.
<p>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.
<p>Also observe that if you do not read all the available data when the
read notifier fires, it fires again and again.
<p>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 <em>have</em> to use TCP urgent data.)
<p>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.
<p>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.
<p>If you need a time-out for your sockets, you can use either
<a href="qobject.html#906d99">timer events</a> or the <a href="qtimer.html">QTimer</a> class.
<p>Socket action is detected in the <a href="qapplication.html#7076af">main event
loop</a> of Qt.  The X11 version of Qt has has a single UNIX
select() call which incorporates all socket notifiers and the X socket.
<p>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.
<p>See also  <a href="qsocket.html">QSocket</a>, <a href="qserversocket.html">QServerSocket</a> and <a href="qsocketdevice.html">QSocketDevice</a>.

<hr><h2>Member Function Documentation</h2>
<h3 class="fn"><a name="317dae"></a>QSocketNotifier::QSocketNotifier(intsocket, Typetype, <a href="qobject.html">QObject</a>*parent=0, constchar*name=0)</h3>
<p>Constructs a socket notifier with a <em>parent</em> and a <em>name.</em>
<p>Arguments:
<ul>
<li><em>socket</em> is the socket to be monitored.
<li><em>type</em> specifies the socket operation to be detected;
<code>QSocketNotifier::Read, QSocketNotifier::Write</code> or
<code>QSocketNotifier::Exception.</code>
</ul>
The <em>parent</em> and <em>name</em> arguments are sent to the <a href="qobject.html">QObject</a> constructor.
<p>The socket notifier is initially enabled.  It is generally advisable to
explicitly enable or disable it, especially for write notifiers.
<p>See also  <a href="#fc30c4">setEnabled</a>() and <a href="#b4e547">isEnabled</a>().
<h3 class="fn"><a name="25bff9"></a>QSocketNotifier::~QSocketNotifier()</h3>
<p>Destructs the socket notifier.
<h3 class="fn">void<a name="ee5bac"></a>QSocketNotifier::activated(intsocket) <code>[signal]</code></h3>
<p>This signal is emitted under certain conditions, specified by the
notifier <a href="#dce885">type</a>:
<ol>
<li> <code>QSocketNotifier::Read:</code> There is data to be read (socket read event).
<li> <code>QSocketNotifier::Write:</code> Data can be written (socket write event).
<li> <code>QSocketNofifier::Exception:</code> An exception has occurred (socket
exception event).
</ol>
<p>The <em>socket</em> argument is the <a href="#08d55e">socket</a> identifier.
<p>See also  <a href="#dce885">type</a>() and <a href="#08d55e">socket</a>().
<h3 class="fn">bool<a name="e1357f"></a>QSocketNotifier::event(<a href="qevent.html">QEvent</a>*e) <code>[virtualprotected]</code></h3>
<p>Reimplemented for internal reasons; the API is not affected.
<p>Reimplemented from <a href="qobject.html#c8b023">QObject.</a>
<h3 class="fn">bool<a name="b4e547"></a>QSocketNotifier::isEnabled()const</h3>
<p>Returns TRUE if the notifier is enabled, or FALSE if it is disabled.
<p>See also  <a href="#fc30c4">setEnabled</a>().
<h3 class="fn">void<a name="fc30c4"></a>QSocketNotifier::setEnabled(boolenable) <code>[virtual]</code></h3>
<p>Enables the notifier if <em>enable</em> is TRUE, or disables it if <em>enable</em> is
FALSE.
<p>The notifier is by default enabled.
<p>If the notifier is enabled, it emits the <a href="#ee5bac">activated</a>() signal whenever a
socket event corresponding to its <a href="#dce885">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 the
activated() signal has been emitted; see discussion of write
notifiers in the class description above.
<p>See also  <a href="#b4e547">isEnabled</a>() and <a href="#ee5bac">activated</a>().
<h3 class="fn">int<a name="08d55e"></a>QSocketNotifier::socket()const</h3>
<p>Returns the socket identifier specified to the constructor.
<p>See also  <a href="#dce885">type</a>().
<h3 class="fn">Type<a name="dce885"></a>QSocketNotifier::type()const</h3>
<p>Returns the socket event type specified to the constructor;
<code>QSocketNotifier::Read, QSocketNotifier::Write</code> or
<code>QSocketNotifier::Exception.</code>
<p>See also  <a href="#08d55e">socket</a>().
<hr><p>
Search the documentation, FAQ, qt-interest archive and more (uses
<a href="http://www.trolltech.com">www.trolltech.com</a>):<br>
<form method=post action="http://www.trolltech.com/search.cgi">
<input type=hidden name="version" value="2.3.1"><nobr>
<input size="50" name="search"><input type=submit value="Search">
</nobr></form><hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>,
copyright &copy; 1995-2000
<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  2000 Trolltech<td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align="right"><div align="right">Qt version 2.3.1</div>
</table></div></address></body></html>