source: trunk/doc/html/qsocket.html@ 190

Last change on this file since 190 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 29.9 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/network/qsocket.cpp:223 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QSocket Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QSocket Class Reference<br><small>[<a href="network.html">network module</a>]</small></h1>
33
34<p>The QSocket class provides a buffered TCP connection.
35<a href="#details">More...</a>
36<p><tt>#include &lt;<a href="qsocket-h.html">qsocket.h</a>&gt;</tt>
37<p>Inherits <a href="qobject.html">QObject</a> and <a href="qiodevice.html">QIODevice</a>.
38<p><a href="qsocket-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn>enum <a href="#Error-enum"><b>Error</b></a> { ErrConnectionRefused, ErrHostNotFound, ErrSocketRead }</li>
42<li class=fn><a href="#QSocket"><b>QSocket</b></a> ( QObject&nbsp;*&nbsp;parent = 0, const&nbsp;char&nbsp;*&nbsp;name = 0 )</li>
43<li class=fn>virtual <a href="#~QSocket"><b>~QSocket</b></a> ()</li>
44<li class=fn>enum <a href="#State-enum"><b>State</b></a> { Idle, HostLookup, Connecting, Connected, Closing, Connection = Connected }</li>
45<li class=fn>State <a href="#state"><b>state</b></a> () const</li>
46<li class=fn>int <a href="#socket"><b>socket</b></a> () const</li>
47<li class=fn>virtual void <a href="#setSocket"><b>setSocket</b></a> ( int&nbsp;socket )</li>
48<li class=fn>QSocketDevice * <a href="#socketDevice"><b>socketDevice</b></a> ()</li>
49<li class=fn>virtual void <a href="#setSocketDevice"><b>setSocketDevice</b></a> ( QSocketDevice&nbsp;*&nbsp;device )</li>
50<li class=fn>virtual void <a href="#connectToHost"><b>connectToHost</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;host, Q_UINT16&nbsp;port )</li>
51<li class=fn>QString <a href="#peerName"><b>peerName</b></a> () const</li>
52<li class=fn>virtual bool <a href="#open"><b>open</b></a> ( int&nbsp;m )</li>
53<li class=fn>virtual void <a href="#close"><b>close</b></a> ()</li>
54<li class=fn>virtual void <a href="#flush"><b>flush</b></a> ()</li>
55<li class=fn>virtual Offset <a href="#size"><b>size</b></a> () const</li>
56<li class=fn>virtual Offset <a href="#at"><b>at</b></a> () const</li>
57<li class=fn>virtual bool <a href="#at-2"><b>at</b></a> ( Offset&nbsp;index )</li>
58<li class=fn>virtual bool <a href="#atEnd"><b>atEnd</b></a> () const</li>
59<li class=fn>Q_ULONG <a href="#bytesAvailable"><b>bytesAvailable</b></a> () const</li>
60<li class=fn>Q_ULONG <a href="#waitForMore"><b>waitForMore</b></a> ( int&nbsp;msecs, bool&nbsp;*&nbsp;timeout ) const</li>
61<li class=fn>Q_ULONG <a href="#waitForMore-2"><b>waitForMore</b></a> ( int&nbsp;msecs ) const</li>
62<li class=fn>Q_ULONG <a href="#bytesToWrite"><b>bytesToWrite</b></a> () const</li>
63<li class=fn>void <a href="#clearPendingData"><b>clearPendingData</b></a> ()</li>
64<li class=fn>virtual Q_LONG <a href="#readBlock"><b>readBlock</b></a> ( char&nbsp;*&nbsp;data, Q_ULONG&nbsp;maxlen )</li>
65<li class=fn>virtual Q_LONG <a href="#writeBlock"><b>writeBlock</b></a> ( const&nbsp;char&nbsp;*&nbsp;data, Q_ULONG&nbsp;len )</li>
66<li class=fn>virtual int <a href="#getch"><b>getch</b></a> ()</li>
67<li class=fn>virtual int <a href="#putch"><b>putch</b></a> ( int&nbsp;ch )</li>
68<li class=fn>virtual int <a href="#ungetch"><b>ungetch</b></a> ( int&nbsp;ch )</li>
69<li class=fn>bool <a href="#canReadLine"><b>canReadLine</b></a> () const</li>
70<li class=fn>virtual QString <a href="#readLine"><b>readLine</b></a> ()</li>
71<li class=fn>Q_UINT16 <a href="#port"><b>port</b></a> () const</li>
72<li class=fn>Q_UINT16 <a href="#peerPort"><b>peerPort</b></a> () const</li>
73<li class=fn>QHostAddress <a href="#address"><b>address</b></a> () const</li>
74<li class=fn>QHostAddress <a href="#peerAddress"><b>peerAddress</b></a> () const</li>
75<li class=fn>void <a href="#setReadBufferSize"><b>setReadBufferSize</b></a> ( Q_ULONG&nbsp;bufSize )</li>
76<li class=fn>Q_ULONG <a href="#readBufferSize"><b>readBufferSize</b></a> () const</li>
77</ul>
78<h2>Signals</h2>
79<ul>
80<li class=fn>void <a href="#hostFound"><b>hostFound</b></a> ()</li>
81<li class=fn>void <a href="#connected"><b>connected</b></a> ()</li>
82<li class=fn>void <a href="#connectionClosed"><b>connectionClosed</b></a> ()</li>
83<li class=fn>void <a href="#delayedCloseFinished"><b>delayedCloseFinished</b></a> ()</li>
84<li class=fn>void <a href="#readyRead"><b>readyRead</b></a> ()</li>
85<li class=fn>void <a href="#bytesWritten"><b>bytesWritten</b></a> ( int&nbsp;nbytes )</li>
86<li class=fn>void <a href="#error"><b>error</b></a> ( int )</li>
87</ul>
88<hr><a name="details"></a><h2>Detailed Description</h2>
89
90
91The QSocket class provides a buffered TCP connection.
92
93<p>
94
95<p> It provides a totally non-blocking <a href="qiodevice.html">QIODevice</a>, and modifies and
96extends the API of QIODevice with socket-specific code.
97<p> Note that a <a href="qapplication.html">QApplication</a> must have been constructed before this
98class can be used.
99<p> The functions you're likely to call most are <a href="#connectToHost">connectToHost</a>(),
100<a href="#bytesAvailable">bytesAvailable</a>(), <a href="#canReadLine">canReadLine</a>() and the ones it inherits from
101QIODevice.
102<p> connectToHost() is the most-used function. As its name implies,
103it opens a connection to a named host.
104<p> Most network protocols are either packet-oriented or
105line-oriented. canReadLine() indicates whether a connection
106contains an entire unread line or not, and bytesAvailable()
107returns the number of bytes available for reading.
108<p> The signals <a href="#error">error</a>(), <a href="#connected">connected</a>(), <a href="#readyRead">readyRead</a>() and
109<a href="#connectionClosed">connectionClosed</a>() inform you of the progress of the connection.
110There are also some less commonly used signals. <a href="#hostFound">hostFound</a>() is
111emitted when connectToHost() has finished its DNS lookup and is
112starting its TCP connection. <a href="#delayedCloseFinished">delayedCloseFinished</a>() is emitted
113when <a href="#close">close</a>() succeeds. <a href="#bytesWritten">bytesWritten</a>() is emitted when QSocket
114moves data from its "to be written" queue into the TCP
115implementation.
116<p> There are several access functions for the socket: <a href="#state">state</a>() returns
117whether the object is idle, is doing a DNS lookup, is connecting,
118has an operational connection, etc. <a href="#address">address</a>() and <a href="#port">port</a>() return
119the IP address and port used for the connection. The <a href="#peerAddress">peerAddress</a>()
120and <a href="#peerPort">peerPort</a>() functions return the IP address and port used by
121the peer, and <a href="#peerName">peerName</a>() returns the name of the peer (normally
122the name that was passed to <a href="#connectToHost">connectToHost</a>()). <a href="#socketDevice">socketDevice</a>()
123returns a pointer to the <a href="qsocketdevice.html">QSocketDevice</a> used for this socket.
124<p> QSocket inherits <a href="qiodevice.html">QIODevice</a>, and reimplements some functions. In
125general, you can treat it as a QIODevice for writing, and mostly
126also for reading. The match isn't perfect, since the QIODevice
127API is designed for devices that are controlled by the same
128machine, and an asynchronous peer-to-peer network connection isn't
129quite like that. For example, there is nothing that matches
130<a href="qiodevice.html#size">QIODevice::size</a>() exactly. The documentation for <a href="#open">open</a>(), <a href="#close">close</a>(),
131<a href="#flush">flush</a>(), <a href="#size">size</a>(), <a href="#at">at</a>(), <a href="#atEnd">atEnd</a>(), <a href="#readBlock">readBlock</a>(), <a href="#writeBlock">writeBlock</a>(),
132<a href="#getch">getch</a>(), <a href="#putch">putch</a>(), <a href="#ungetch">ungetch</a>() and <a href="#readLine">readLine</a>() describes the
133differences in detail.
134<p> <b>Warning:</b> QSocket is not suitable for use in threads. If you need
135to uses sockets in threads use the lower-level <a href="qsocketdevice.html">QSocketDevice</a> class.
136<p> <b>Warning:</b> Because Qt doesn't use the native socketstream
137implementation on Mac OS X, QSocket has an implicit transfer
138latency of 100ms. You can achieve lower latency on Mac OS X by
139using QSocketDevice instead.
140<p> <p>See also <a href="qsocketdevice.html">QSocketDevice</a>, <a href="qhostaddress.html">QHostAddress</a>, <a href="qsocketnotifier.html">QSocketNotifier</a>, and <a href="io.html">Input/Output and Networking</a>.
141
142<hr><h2>Member Type Documentation</h2>
143<h3 class=fn><a name="Error-enum"></a>QSocket::Error</h3>
144
145<p> This enum specifies the possible errors:
146<ul>
147<li><tt>QSocket::ErrConnectionRefused</tt> - if the connection was refused
148<li><tt>QSocket::ErrHostNotFound</tt> - if the host was not found
149<li><tt>QSocket::ErrSocketRead</tt> - if a read from the socket failed
150</ul>
151<h3 class=fn><a name="State-enum"></a>QSocket::State</h3>
152
153<p> This enum defines the connection states:
154<ul>
155<li><tt>QSocket::Idle</tt> - if there is no connection
156<li><tt>QSocket::HostLookup</tt> - during a DNS lookup
157<li><tt>QSocket::Connecting</tt> - during TCP connection establishment
158<li><tt>QSocket::Connected</tt> - when there is an operational connection
159<li><tt>QSocket::Closing</tt> - if the socket is closing down, but is not yet closed.
160</ul>
161<hr><h2>Member Function Documentation</h2>
162<h3 class=fn><a name="QSocket"></a>QSocket::QSocket ( <a href="qobject.html">QObject</a>&nbsp;*&nbsp;parent = 0, const&nbsp;char&nbsp;*&nbsp;name = 0 )
163</h3>
164Creates a QSocket object in <a href="#State-enum">QSocket::Idle</a> state.
165<p> The <em>parent</em> and <em>name</em> arguments are passed on to the <a href="qobject.html">QObject</a>
166constructor.
167<p> Note that a <a href="qapplication.html">QApplication</a> must have been constructed before sockets
168can be used.
169
170<h3 class=fn><a name="~QSocket"></a>QSocket::~QSocket ()<tt> [virtual]</tt>
171</h3>
172Destroys the socket. Closes the connection if necessary.
173<p> <p>See also <a href="#close">close</a>().
174
175<h3 class=fn><a href="qhostaddress.html">QHostAddress</a> <a name="address"></a>QSocket::address () const
176</h3>
177Returns the host address of this socket. (This is normally the
178main IP address of the host, but can be e.g. 127.0.0.1 for
179connections to localhost.)
180
181<h3 class=fn><a href="qiodevice.html#Offset">Offset</a> <a name="at"></a>QSocket::at () const<tt> [virtual]</tt>
182</h3>
183Returns the current read index. Since QSocket is a sequential
184device, the current read index is always zero.
185
186<p>Reimplemented from <a href="qiodevice.html#at">QIODevice</a>.
187<h3 class=fn>bool <a name="at-2"></a>QSocket::at ( <a href="qiodevice.html#Offset">Offset</a>&nbsp;index )<tt> [virtual]</tt>
188</h3>
189This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
190<p> Moves the read index forward to <em>index</em> and returns TRUE if the
191operation was successful; otherwise returns FALSE. Moving the
192index forward means skipping incoming data.
193
194<p>Reimplemented from <a href="qiodevice.html#at-2">QIODevice</a>.
195<h3 class=fn>bool <a name="atEnd"></a>QSocket::atEnd () const<tt> [virtual]</tt>
196</h3>
197Returns TRUE if there is no more data to read; otherwise returns FALSE.
198
199<p>Reimplemented from <a href="qiodevice.html#atEnd">QIODevice</a>.
200<h3 class=fn>Q_ULONG <a name="bytesAvailable"></a>QSocket::bytesAvailable () const
201</h3>
202Returns the number of incoming bytes that can be read, i.e. the
203size of the input buffer. Equivalent to <a href="#size">size</a>().
204<p> This function can trigger the <a href="#readyRead">readyRead</a>() signal, if more data has
205arrived on the socket.
206<p> <p>See also <a href="#bytesToWrite">bytesToWrite</a>().
207
208<p>Example: <a href="networkprotocol-example.html#x677">network/networkprotocol/nntp.cpp</a>.
209<h3 class=fn>Q_ULONG <a name="bytesToWrite"></a>QSocket::bytesToWrite () const
210</h3>
211Returns the number of bytes that are waiting to be written, i.e.
212the size of the output buffer.
213<p> <p>See also <a href="#bytesAvailable">bytesAvailable</a>() and <a href="#clearPendingData">clearPendingData</a>().
214
215<h3 class=fn>void <a name="bytesWritten"></a>QSocket::bytesWritten ( int&nbsp;nbytes )<tt> [signal]</tt>
216</h3>
217
218<p> This signal is emitted when data has been written to the network.
219The <em>nbytes</em> parameter specifies how many bytes were written.
220<p> The <a href="#bytesToWrite">bytesToWrite</a>() function is often used in the same context; it
221indicates how many buffered bytes there are left to write.
222<p> <p>See also <a href="#writeBlock">writeBlock</a>() and <a href="#bytesToWrite">bytesToWrite</a>().
223
224<h3 class=fn>bool <a name="canReadLine"></a>QSocket::canReadLine () const
225</h3>
226Returns TRUE if it's possible to read an entire line of text from
227this socket at this time; otherwise returns FALSE.
228<p> Note that if the peer closes the connection unexpectedly, this
229function returns FALSE. This means that loops such as this won't
230work:
231<p> <pre>
232 while( !socket-&gt;canReadLine() ) // WRONG
233 ;
234 </pre>
235
236<p> <p>See also <a href="#readLine">readLine</a>().
237
238<p>Examples: <a href="clientserver-example.html#x795">network/clientserver/client/client.cpp</a>, <a href="httpd-example.html#x727">network/httpd/httpd.cpp</a>, <a href="mail-example.html#x709">network/mail/smtp.cpp</a>, and <a href="networkprotocol-example.html#x678">network/networkprotocol/nntp.cpp</a>.
239<h3 class=fn>void <a name="clearPendingData"></a>QSocket::clearPendingData ()
240</h3>
241Deletes the data that is waiting to be written. This is useful if you want
242to close the socket without waiting for all the data to be written.
243<p> <p>See also <a href="#bytesToWrite">bytesToWrite</a>(), <a href="#close">close</a>(), and <a href="#delayedCloseFinished">delayedCloseFinished</a>().
244
245<h3 class=fn>void <a name="close"></a>QSocket::close ()<tt> [virtual]</tt>
246</h3>
247Closes the socket.
248<p> The read buffer is cleared.
249<p> If the output buffer is empty, the state is set to <a href="#State-enum">QSocket::Idle</a> and the connection is terminated immediately. If the
250output buffer still contains data to be written, QSocket goes into
251the <a href="#State-enum">QSocket::Closing</a> state and the rest of the data will be
252written. When all of the outgoing data have been written, the
253state is set to <a href="#State-enum">QSocket::Idle</a> and the connection is terminated.
254At this point, the <a href="#delayedCloseFinished">delayedCloseFinished</a>() signal is emitted.
255<p> If you don't want that the data of the output buffer is written, call
256<a href="#clearPendingData">clearPendingData</a>() before you call <a href="#close">close</a>().
257<p> <p>See also <a href="#state">state</a>(), <a href="#bytesToWrite">bytesToWrite</a>(), and <a href="#clearPendingData">clearPendingData</a>().
258
259<p>Examples: <a href="clientserver-example.html#x796">network/clientserver/client/client.cpp</a>, <a href="httpd-example.html#x728">network/httpd/httpd.cpp</a>, and <a href="networkprotocol-example.html#x679">network/networkprotocol/nntp.cpp</a>.
260<p>Reimplemented from <a href="qiodevice.html#close">QIODevice</a>.
261<h3 class=fn>void <a name="connectToHost"></a>QSocket::connectToHost ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;host, Q_UINT16&nbsp;port )<tt> [virtual]</tt>
262</h3>
263Attempts to make a connection to <em>host</em> on the specified <em>port</em>
264and return immediately.
265<p> Any connection or pending connection is closed immediately, and
266QSocket goes into the <a href="#State-enum">HostLookup</a> state. When the lookup
267succeeds, it emits <a href="#hostFound">hostFound</a>(), starts a TCP connection and goes
268into the <a href="#State-enum">Connecting</a> state. Finally, when the connection
269succeeds, it emits <a href="#connected">connected</a>() and goes into the <a href="#State-enum">Connected</a>
270state. If there is an error at any point, it emits <a href="#error">error</a>().
271<p> <em>host</em> may be an IP address in string form, or it may be a DNS
272name. QSocket will do a normal DNS lookup if required. Note that
273<em>port</em> is in native byte order, unlike some other libraries.
274<p> <p>See also <a href="#state">state</a>().
275
276<p>Examples: <a href="clientserver-example.html#x797">network/clientserver/client/client.cpp</a>, <a href="mail-example.html#x710">network/mail/smtp.cpp</a>, and <a href="networkprotocol-example.html#x680">network/networkprotocol/nntp.cpp</a>.
277<h3 class=fn>void <a name="connected"></a>QSocket::connected ()<tt> [signal]</tt>
278</h3>
279
280<p> This signal is emitted after <a href="#connectToHost">connectToHost</a>() has been called and a
281connection has been successfully established.
282<p> <p>See also <a href="#connectToHost">connectToHost</a>() and <a href="#connectionClosed">connectionClosed</a>().
283
284<p>Examples: <a href="clientserver-example.html#x798">network/clientserver/client/client.cpp</a>, <a href="mail-example.html#x711">network/mail/smtp.cpp</a>, and <a href="networkprotocol-example.html#x681">network/networkprotocol/nntp.cpp</a>.
285<h3 class=fn>void <a name="connectionClosed"></a>QSocket::connectionClosed ()<tt> [signal]</tt>
286</h3>
287
288<p> This signal is emitted when the other end has closed the
289connection. The read buffers may contain buffered input data which
290you can read after the connection was closed.
291<p> <p>See also <a href="#connectToHost">connectToHost</a>() and <a href="#close">close</a>().
292
293<p>Examples: <a href="clientserver-example.html#x799">network/clientserver/client/client.cpp</a>, <a href="clientserver-example.html#x785">network/clientserver/server/server.cpp</a>, and <a href="networkprotocol-example.html#x682">network/networkprotocol/nntp.cpp</a>.
294<h3 class=fn>void <a name="delayedCloseFinished"></a>QSocket::delayedCloseFinished ()<tt> [signal]</tt>
295</h3>
296
297<p> This signal is emitted when a delayed close is finished.
298<p> If you call <a href="#close">close</a>() and there is buffered output data to be
299written, QSocket goes into the <a href="#State-enum">QSocket::Closing</a> state and
300returns immediately. It will then keep writing to the socket until
301all the data has been written. Then, the <a href="#delayedCloseFinished">delayedCloseFinished</a>()
302signal is emitted.
303<p> <p>See also <a href="#close">close</a>().
304
305<p>Examples: <a href="clientserver-example.html#x800">network/clientserver/client/client.cpp</a> and <a href="httpd-example.html#x729">network/httpd/httpd.cpp</a>.
306<h3 class=fn>void <a name="error"></a>QSocket::error ( int )<tt> [signal]</tt>
307</h3>
308
309<p> This signal is emitted after an error occurred. The parameter is
310the <a href="#Error-enum">Error</a> value.
311
312<p>Examples: <a href="clientserver-example.html#x801">network/clientserver/client/client.cpp</a> and <a href="networkprotocol-example.html#x683">network/networkprotocol/nntp.cpp</a>.
313<h3 class=fn>void <a name="flush"></a>QSocket::flush ()<tt> [virtual]</tt>
314</h3>
315Implementation of the abstract virtual <a href="qiodevice.html#flush">QIODevice::flush</a>() function.
316
317<p>Reimplemented from <a href="qiodevice.html#flush">QIODevice</a>.
318<h3 class=fn>int <a name="getch"></a>QSocket::getch ()<tt> [virtual]</tt>
319</h3>
320Reads a single byte/character from the internal read buffer.
321Returns the byte/character read, or -1 if there is nothing to be
322read.
323<p> <p>See also <a href="#bytesAvailable">bytesAvailable</a>() and <a href="#putch">putch</a>().
324
325<p>Reimplemented from <a href="qiodevice.html#getch">QIODevice</a>.
326<h3 class=fn>void <a name="hostFound"></a>QSocket::hostFound ()<tt> [signal]</tt>
327</h3>
328
329<p> This signal is emitted after <a href="#connectToHost">connectToHost</a>() has been called and
330the host lookup has succeeded.
331<p> <p>See also <a href="#connected">connected</a>().
332
333<p>Example: <a href="networkprotocol-example.html#x684">network/networkprotocol/nntp.cpp</a>.
334<h3 class=fn>bool <a name="open"></a>QSocket::open ( int&nbsp;m )<tt> [virtual]</tt>
335</h3>
336Opens the socket using the specified <a href="qiodevice.html">QIODevice</a> file mode <em>m</em>.
337This function is called automatically when needed and you should
338not call it yourself.
339<p> <p>See also <a href="#close">close</a>().
340
341<p>Reimplemented from <a href="qiodevice.html#open">QIODevice</a>.
342<h3 class=fn><a href="qhostaddress.html">QHostAddress</a> <a name="peerAddress"></a>QSocket::peerAddress () const
343</h3>
344Returns the address of the connected peer if the socket is in
345Connected state; otherwise an empty <a href="qhostaddress.html">QHostAddress</a> is returned.
346
347<h3 class=fn><a href="qstring.html">QString</a> <a name="peerName"></a>QSocket::peerName () const
348</h3>
349Returns the host name as specified to the <a href="#connectToHost">connectToHost</a>()
350function. An empty string is returned if none has been set.
351
352<p>Example: <a href="mail-example.html#x712">network/mail/smtp.cpp</a>.
353<h3 class=fn>Q_UINT16 <a name="peerPort"></a>QSocket::peerPort () const
354</h3>
355Returns the peer's host port number, normally as specified to the
356<a href="#connectToHost">connectToHost</a>() function. If none has been set, this function
357returns 0.
358<p> Note that Qt always uses native byte order, i.e. 67 is 67 in Qt;
359there is no need to call htons().
360
361<h3 class=fn>Q_UINT16 <a name="port"></a>QSocket::port () const
362</h3>
363Returns the host port number of this socket, in native byte order.
364
365<h3 class=fn>int <a name="putch"></a>QSocket::putch ( int&nbsp;ch )<tt> [virtual]</tt>
366</h3>
367Writes the character <em>ch</em> to the output buffer.
368<p> Returns <em>ch</em>, or -1 if an error occurred.
369<p> <p>See also <a href="#getch">getch</a>().
370
371<p>Reimplemented from <a href="qiodevice.html#putch">QIODevice</a>.
372<h3 class=fn>Q_LONG <a name="readBlock"></a>QSocket::readBlock ( char&nbsp;*&nbsp;data, Q_ULONG&nbsp;maxlen )<tt> [virtual]</tt>
373</h3>
374Reads <em>maxlen</em> bytes from the socket into <em>data</em> and returns the
375number of bytes read. Returns -1 if an error occurred.
376
377<p>Example: <a href="networkprotocol-example.html#x685">network/networkprotocol/nntp.cpp</a>.
378<p>Reimplemented from <a href="qiodevice.html#readBlock">QIODevice</a>.
379<h3 class=fn>Q_ULONG <a name="readBufferSize"></a>QSocket::readBufferSize () const
380</h3>
381Returns the size of the read buffer.
382<p> <p>See also <a href="#setReadBufferSize">setReadBufferSize</a>().
383
384<h3 class=fn><a href="qstring.html">QString</a> <a name="readLine"></a>QSocket::readLine ()<tt> [virtual]</tt>
385</h3>
386Returns a line of text including a terminating newline character
387(\n). Returns "" if <a href="#canReadLine">canReadLine</a>() returns FALSE.
388<p> <p>See also <a href="#canReadLine">canReadLine</a>().
389
390<p>Examples: <a href="clientserver-example.html#x802">network/clientserver/client/client.cpp</a>, <a href="httpd-example.html#x730">network/httpd/httpd.cpp</a>, <a href="mail-example.html#x713">network/mail/smtp.cpp</a>, and <a href="networkprotocol-example.html#x686">network/networkprotocol/nntp.cpp</a>.
391<h3 class=fn>void <a name="readyRead"></a>QSocket::readyRead ()<tt> [signal]</tt>
392</h3>
393
394<p> This signal is emitted every time there is new incoming data.
395<p> Bear in mind that new incoming data is only reported once; if you do not
396read all the data, this class buffers the data and you can read it later,
397but no signal is emitted unless new data arrives. A good practice is to
398read all data in the slot connected to this signal unless you are sure that
399you need to receive more data to be able to process it.
400<p> <p>See also <a href="#readBlock">readBlock</a>(), <a href="#readLine">readLine</a>(), and <a href="#bytesAvailable">bytesAvailable</a>().
401
402<p>Examples: <a href="clientserver-example.html#x803">network/clientserver/client/client.cpp</a>, <a href="httpd-example.html#x731">network/httpd/httpd.cpp</a>, <a href="mail-example.html#x714">network/mail/smtp.cpp</a>, and <a href="networkprotocol-example.html#x687">network/networkprotocol/nntp.cpp</a>.
403<h3 class=fn>void <a name="setReadBufferSize"></a>QSocket::setReadBufferSize ( Q_ULONG&nbsp;bufSize )
404</h3>
405Sets the size of the QSocket's internal read buffer to <em>bufSize</em>.
406<p> Usually QSocket reads all data that is available from the operating
407system's socket. If the buffer size is limited to a certain size, this
408means that the QSocket class doesn't buffer more than this size of data.
409<p> If the size of the read buffer is 0, the read buffer is unlimited and all
410incoming data is buffered. This is the default.
411<p> If you read the data in the <a href="#readyRead">readyRead</a>() signal, you shouldn't use this
412option since it might slow down your program unnecessary. This option is
413useful if you only need to read the data at certain points in time, like in
414a realtime streaming application.
415<p> <p>See also <a href="#readBufferSize">readBufferSize</a>().
416
417<h3 class=fn>void <a name="setSocket"></a>QSocket::setSocket ( int&nbsp;socket )<tt> [virtual]</tt>
418</h3>
419Sets the socket to use <em>socket</em> and the <a href="#state">state</a>() to <a href="#State-enum">Connected</a>.
420The socket must already be connected.
421<p> This allows us to use the QSocket class as a wrapper for other
422socket types (e.g. Unix Domain Sockets).
423
424<p>Example: <a href="httpd-example.html#x732">network/httpd/httpd.cpp</a>.
425<h3 class=fn>void <a name="setSocketDevice"></a>QSocket::setSocketDevice ( <a href="qsocketdevice.html">QSocketDevice</a>&nbsp;*&nbsp;device )<tt> [virtual]</tt>
426</h3>
427Sets the internal socket device to <em>device</em>. Passing a <em>device</em>
428of 0 will cause the internal socket device to be used. Any
429existing connection will be disconnected before using the new <em>device</em>.
430<p> The new device should not be connected before being associated
431with a QSocket; after setting the socket call <a href="#connectToHost">connectToHost</a>() to
432make the connection.
433<p> This function is useful if you need to subclass <a href="qsocketdevice.html">QSocketDevice</a> and
434want to use the QSocket API, for example, to implement Unix domain
435sockets.
436
437<h3 class=fn><a href="qiodevice.html#Offset">Offset</a> <a name="size"></a>QSocket::size () const<tt> [virtual]</tt>
438</h3>
439Returns the number of incoming bytes that can be read right now
440(like <a href="#bytesAvailable">bytesAvailable</a>()).
441
442<p>Reimplemented from <a href="qiodevice.html#size">QIODevice</a>.
443<h3 class=fn>int <a name="socket"></a>QSocket::socket () const
444</h3>
445Returns the socket number, or -1 if there is no socket at the moment.
446
447<h3 class=fn><a href="qsocketdevice.html">QSocketDevice</a>&nbsp;* <a name="socketDevice"></a>QSocket::socketDevice ()
448</h3>
449Returns a pointer to the internal socket device.
450<p> There is normally no need to manipulate the socket device directly
451since this class does the necessary setup for most applications.
452
453<h3 class=fn><a href="qsocket.html#State-enum">State</a> <a name="state"></a>QSocket::state () const
454</h3>
455Returns the current state of the socket connection.
456<p> <p>See also <a href="#State-enum">QSocket::State</a>.
457
458<p>Examples: <a href="clientserver-example.html#x804">network/clientserver/client/client.cpp</a> and <a href="networkprotocol-example.html#x688">network/networkprotocol/nntp.cpp</a>.
459<h3 class=fn>int <a name="ungetch"></a>QSocket::ungetch ( int&nbsp;ch )<tt> [virtual]</tt>
460</h3>
461This implementation of the virtual function <a href="qiodevice.html#ungetch">QIODevice::ungetch</a>()
462prepends the character <em>ch</em> to the read buffer so that the next
463read returns this character as the first character of the output.
464
465<p>Reimplemented from <a href="qiodevice.html#ungetch">QIODevice</a>.
466<h3 class=fn>Q_ULONG <a name="waitForMore"></a>QSocket::waitForMore ( int&nbsp;msecs, bool&nbsp;*&nbsp;timeout ) const
467</h3>
468Wait up to <em>msecs</em> milliseconds for more data to be available.
469<p> If <em>msecs</em> is -1 the call will block indefinitely.
470<p> Returns the number of bytes available.
471<p> If <em>timeout</em> is non-null and no error occurred (i.e. it does not
472return -1): this function sets <em>*timeout</em> to TRUE, if the reason
473for returning was that the timeout was reached; otherwise it sets
474<em>*timeout</em> to FALSE. This is useful to find out if the peer
475closed the connection.
476<p> <b>Warning:</b> This is a blocking call and should be avoided in event
477driven applications.
478<p> <p>See also <a href="#bytesAvailable">bytesAvailable</a>().
479
480<h3 class=fn>Q_ULONG <a name="waitForMore-2"></a>QSocket::waitForMore ( int&nbsp;msecs ) const
481</h3> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
482<p>
483<h3 class=fn>Q_LONG <a name="writeBlock"></a>QSocket::writeBlock ( const&nbsp;char&nbsp;*&nbsp;data, Q_ULONG&nbsp;len )<tt> [virtual]</tt>
484</h3>
485Writes <em>len</em> bytes to the socket from <em>data</em> and returns the
486number of bytes written. Returns -1 if an error occurred.
487
488<p>Example: <a href="networkprotocol-example.html#x689">network/networkprotocol/nntp.cpp</a>.
489<p>Reimplemented from <a href="qiodevice.html#writeBlock">QIODevice</a>.
490<!-- eof -->
491<hr><p>
492This file is part of the <a href="index.html">Qt toolkit</a>.
493Copyright &copy; 1995-2007
494<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
495<table width=100% cellspacing=0 border=0><tr>
496<td>Copyright &copy; 2007
497<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
498<td align=right><div align=right>Qt 3.3.8</div>
499</table></div></address></body>
500</html>
Note: See TracBrowser for help on using the repository browser.