~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/networking.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/networking.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Network Module</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Network Module</h1>
 
21
<p>The network module offers classes to make network programming easier and portable. It offers both high-level classes such as <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a> that implement specific application-level protocols, and lower-level classes such as <a href="qtcpsocket.html">QTcpSocket</a>, <a href="qtcpserver.html">QTcpServer</a>, and <a href="qudpsocket.html">QUdpSocket</a>, that enable you to write your own TCP/IP clients and servers.</p>
 
22
<p>This document is an overview of the main classes in the network module. See the individual classes' reference documentation for details.</p>
 
23
<ul><li><a href="#writing-http-and-ftp-clients-with-qhttp-and-qftp">Writing HTTP and FTP Clients with QHttp and QFtp</a></li>
 
24
<li><a href="#using-tcp-with-qtcpsocket-and-qtcpserver">Using TCP with QTcpSocket and QTcpServer</a></li>
 
25
<li><a href="#using-udp-with-qudpsocket">Using UDP with QUdpSocket</a></li>
 
26
<li><a href="#resolving-host-names-using-qhostinfo">Resolving Host Names using QHostInfo</a></li>
 
27
</ul>
 
28
<a name="writing-http-and-ftp-clients-with-qhttp-and-qftp"></a>
 
29
<h2>Writing HTTP and FTP Clients with QHttp and QFtp</h2>
 
30
<p>HTTP (Hypertext Transfer Protocol) is an application-level network protocol used mainly for downloading HTML and XML files, but it is also used as a high-level transport protocol for any types of data. For example, HTTP is often used for transferring purchase orders over the Internet. In contrast, FTP (File Transfer Protocol) is a protocol used almost exclusively for browsing remote directories and transferring files.</p>
 
31
<center><img src="images/httpstack.png" alt="HTTP Client and Server" /></center><p>HTTP is a simpler protocol than FTP in many ways. It uses only one network connection, while FTP uses two (one for sending commands, and one for transferring data). HTTP is a stateless protocol; requests and responses are always self-contained. The FTP protocol has a state and requires the client to send several commands before the file transfer takes place.</p>
 
32
<p>In practice, HTTP clients often use separate connections for separate requests, whereas FTP clients establish one connection and keep it open throughout the session.</p>
 
33
<p>The <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a> classes provide client-side support for HTTP and FTP. Since the two protocols are used to solve the same problems, the <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a> classes have many features in common:</p>
 
34
<ul>
 
35
<li><i>Non-blocking behavior.</i> <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a> are asynchronous. You can schedule a series of commands (also called &quot;requests&quot; for HTTP). The commands are executed later, when control goes back to Qt's event loop.</li>
 
36
<li><i>Command IDs.</i> Each command has a unique ID number that you can use to follow the execution of the command. For example, <a href="qftp.html">QFtp</a> emits the <a href="qftp.html#commandStarted">commandStarted()</a> and <a href="qftp.html#commandFinished">commandFinished()</a> signal with the command ID for each command that is executed. <a href="qhttp.html">QHttp</a> has a <a href="qhttp.html#requestStarted">requestStarted()</a> and a <a href="qhttp.html#requestFinished">requestFinished()</a> signal that works the same way.</li>
 
37
<li><i>Data transfer progress indicators.</i> <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a> emits signals whenever data is transferred (<a href="qftp.html#dataTransferProgress">QFtp::dataTransferProgress</a>(), <a href="qhttp.html#dataReadProgress">QHttp::dataReadProgress</a>(), and <a href="qhttp.html#dataSendProgress">QHttp::dataSendProgress</a>()). You can connect these signals to QProgressBar::setProgress() or QProgressDialog::setProgress().</li>
 
38
<li><i><a href="qiodevice.html">QIODevice</a> support.</i> Both classes support convenient uploading of and downloading to <a href="qiodevice.html">QIODevice</a>s, in addition to a <a href="qbytearray.html">QByteArray</a>-based API.</li>
 
39
</ul>
 
40
<p>There are two main ways of using <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a>. The most common approach is to keep track of the command IDs and follow the execution of every command by connecting to the appropriate signals. The other approach is to schedule all commands at once and only connect to the done() signal, which is emitted when all scheduled commands have been executed. The first approach requires more work, but it gives you more control over the execution of individual commands and allows you to initiate new commands based on the result of a previous command. It also enables you to provide detailed feedback to the user.</p>
 
41
<p>The <a href="network-http.html">HTTP</a> and <a href="network-ftp.html">FTP</a> examples illustrate how to write an HTTP and an FTP client.</p>
 
42
<p>Writing your own HTTP or FTP server is possible using the lower-level classes <a href="qtcpsocket.html">QTcpSocket</a> and <a href="qtcpserver.html">QTcpServer</a>.</p>
 
43
<a name="using-tcp-with-qtcpsocket-and-qtcpserver"></a>
 
44
<h2>Using TCP with QTcpSocket and QTcpServer</h2>
 
45
<p>TCP (Transmission Control Protocol) is a low-level network protocol used by most Internet protocols, including HTTP and FTP, for data transfer. It is a reliable, stream-oriented, connection-oriented transport protocol. It is especially well suited for continuous transmission of data.</p>
 
46
<center><img src="images/tcpstream.png" alt="A TCP Stream" /></center><p>The <a href="qtcpsocket.html">QTcpSocket</a> class provides an interface for TCP. You can use <a href="qtcpsocket.html">QTcpSocket</a> to implement standard network protocols such as POP3, SMTP, and NNTP, as well as custom protocols.</p>
 
47
<p>A TCP connection must be established to a remote host and port before any data transfer can begin. Once the connection has been established, the IP address and port of the peer are available through <a href="qabstractsocket.html#peerAddress">QTcpSocket::peerAddress</a>() and <a href="qabstractsocket.html#peerPort">QTcpSocket::peerPort</a>(). At any time, the peer can close the connection, and data transfer will then stop immediately.</p>
 
48
<p><a href="qtcpsocket.html">QTcpSocket</a> works asynchronously and emits signals to report status changes and errors, just like <a href="qhttp.html">QHttp</a> and <a href="qftp.html">QFtp</a>. It relies on the event loop to detect incoming data and to automatically flush outgoing data. You can write data to the socket using <a href="qiodevice.html#write">QTcpSocket::write</a>(), and read data using <a href="qiodevice.html#read">QTcpSocket::read</a>(). <a href="qtcpsocket.html">QTcpSocket</a> represents two independent streams of data: one for reading and one for writing.</p>
 
49
<p>Since <a href="qtcpsocket.html">QTcpSocket</a> inherits <a href="qiodevice.html">QIODevice</a>, you can use it with <a href="qtextstream.html">QTextStream</a> and <a href="qdatastream.html">QDataStream</a>. When reading from a <a href="qtcpsocket.html">QTcpSocket</a>, you must make sure that enough data is available by calling <a href="qabstractsocket.html#bytesAvailable">QTcpSocket::bytesAvailable</a>() beforehand.</p>
 
50
<p>If you need to handle incoming TCP connections (e.g., in a server application), use the <a href="qtcpserver.html">QTcpServer</a> class. Call <a href="qtcpserver.html#listen">QTcpServer::listen</a>() to set up the server, and connect to the <a href="qtcpserver.html#newConnection">QTcpServer::newConnection</a>() signal, which is emitted once for every client that connects. In your slot, call <a href="qtcpserver.html#nextPendingConnection">QTcpServer::nextPendingConnection</a>() to accept the connection and use the returned <a href="qtcpsocket.html">QTcpSocket</a> to communicate with the client.</p>
 
51
<p>Although most of its functions work asynchronously, it's possible to use <a href="qtcpsocket.html">QTcpSocket</a> synchronously (i.e., blocking). The trick is to call <a href="qtcpsocket.html">QTcpSocket</a>'s waitFor...() functions, which suspend the calling thread until a signal has been emitted. For example, after calling the non-blocking <a href="qabstractsocket.html#connectToHost">QTcpSocket::connectToHost</a>() function, call <a href="qabstractsocket.html#waitForConnected">QTcpSocket::waitForConnected</a>() to block the thread until the <a href="qabstractsocket.html#connected">connected()</a> signal has been emitted.</p>
 
52
<p>Synchronous sockets often lead to code with a simpler control flow. The main disadvantage of the waitFor...() approach is that events won't be processed while a waitFor...() function is blocked. If used in the GUI thread, this might freeze the application's user interface. For this reason, we recommend that you use synchronous sockets only in non-GUI threads. When used synchronously, <a href="qtcpsocket.html">QTcpSocket</a> doesn't require an event loop.</p>
 
53
<p>The <a href="network-fortuneclient.html">Fortune Client</a> and <a href="network-fortuneserver.html">Fortune Server</a> examples show how to use <a href="qtcpsocket.html">QTcpSocket</a> and <a href="qtcpserver.html">QTcpServer</a> to write TCP client-server applications. See also <a href="network-blockingfortuneclient.html">Blocking Fortune Client</a> for an example on how to use a synchronous <a href="qtcpsocket.html">QTcpSocket</a> in a separate thread (without using an event loop), and <a href="network-threadedfortuneserver.html">Threaded Fortune Server</a> for an example of a multithreaded TCP server with one thread per active client.</p>
 
54
<a name="using-udp-with-qudpsocket"></a>
 
55
<h2>Using UDP with QUdpSocket</h2>
 
56
<p>UDP (User Datagram Protocol) is a lightweight, unreliable, datagram-oriented, connectionless protocol. It can be used when reliability isn't important. For example, a server that reports the time of day could choose UDP. If a datagram with the time of day is lost, the client can simply make another request.</p>
 
57
<center><img src="images/udppackets.png" alt="UDP Packets" /></center><p>The <a href="qudpsocket.html">QUdpSocket</a> class allows you to send and receive UDP datagrams. It inherits <a href="qabstractsocket.html">QAbstractSocket</a>, and it therefore shares most of <a href="qtcpsocket.html">QTcpSocket</a>'s interface. The main difference is that <a href="qudpsocket.html">QUdpSocket</a> transfers data as datagrams instead of as a continuous stream of data. In short, a datagram is a data packet of limited size (normally smaller than 512 bytes), containing the IP address and port of the datagram's sender and receiver in addition to the data being transferred.</p>
 
58
<p><a href="qudpsocket.html">QUdpSocket</a> supports IPv4 broadcasting. Broadcasting is often used to implement network discovery protocols, such as finding which host on the network has the most free hard disk space. One host broadcasts a datagram to the network that all other hosts receive. Each host that receives a request then sends a reply back to the sender with its current amount of free disk space. The originator waits until it has received replies from all hosts, and can then choose the server with most free space to store data. To broadcast a datagram, simply send it to the special address <a href="qhostaddress.html#SpecialAddress-enum">QHostAddress::Broadcast</a> (255.255.255.255), or to your local network's broadcast address.</p>
 
59
<p><a href="qudpsocket.html#bind">QUdpSocket::bind</a>() prepares the socket for accepting incoming datagrams, much like <a href="qtcpserver.html#listen">QTcpServer::listen</a>() for TCP servers. Whenever one or more datagrams arrive, <a href="qudpsocket.html">QUdpSocket</a> emits the <a href="qiodevice.html#readyRead">readyRead()</a> signal. Call <a href="qudpsocket.html#readDatagram">QUdpSocket::readDatagram</a>() to read the datagram.</p>
 
60
<p>The <a href="network-broadcastsender.html">Broadcast Sender</a> and <a href="network-broadcastreceiver.html">Broadcast Receiver</a> examples show how to write a UDP sender and a UDP receiver using Qt.</p>
 
61
<a name="resolving-host-names-using-qhostinfo"></a>
 
62
<h2>Resolving Host Names using QHostInfo</h2>
 
63
<p>Before establishing a network connection, <a href="qtcpsocket.html">QTcpSocket</a> and <a href="qudpsocket.html">QUdpSocket</a> perform a name lookup, translating the host name you're connecting to into an IP address. This operation is usually performed using the DNS (Domain Name Service) protocol.</p>
 
64
<p><a href="qhostinfo.html">QHostInfo</a> provides a static function that lets you perform such a lookup yourself. By passing a host name to <a href="qhostinfo.html#lookupHost">QHostInfo::lookupHost</a>() with a host name, a <a href="qobject.html">QObject</a> pointer, and a slot signature, <a href="qhostinfo.html">QHostInfo</a> will perform the name lookup and invoke the given slot when the results are ready. The actual lookup is done in a separate thread, making use of the operating system's own methods for performing name lookups.</p>
 
65
<p><a href="qhostinfo.html">QHostInfo</a> also provides a static function called <a href="qhostinfo.html#fromName">QHostInfo::fromName</a>() that takes the host name as argument and returns the results. In this case, the name lookup is performed in the same thread as the caller. This overload is useful for non-GUI applications or for doing name lookups in a separate, non-GUI thread. (Calling this function in a GUI thread may cause your user interface to freeze while the function performs the lookup.)</p>
 
66
<p /><address><hr /><div align="center">
 
67
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
68
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
69
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
70
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
71
</tr></table></div></address></body>
 
72
</html>