Files
qt4-x11/doc/html/qt4-network.html
2025-08-24 20:55:26 +08:00

380 lines
30 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qt4-network.qdoc -->
<title>Qt 4.8: The Network Module in Qt 4</title>
<link rel="stylesheet" type="text/css" href="style/style.css" />
<script src="scripts/jquery.js" type="text/javascript"></script>
<script src="scripts/functions.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="style/superfish.css" />
<link rel="stylesheet" type="text/css" href="style/narrow.css" />
<!--[if IE]>
<meta name="MSSmartTagsPreventParsing" content="true">
<meta http-equiv="imagetoolbar" content="no">
<![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie6.css">
<![endif]-->
<!--[if IE 7]>
<link rel="stylesheet" type="text/css" href="style/style_ie7.css">
<![endif]-->
<!--[if IE 8]>
<link rel="stylesheet" type="text/css" href="style/style_ie8.css">
<![endif]-->
<script src="scripts/superfish.js" type="text/javascript"></script>
<script src="scripts/narrow.js" type="text/javascript"></script>
</head>
<body class="" onload="CheckEmptyAndLoadList();">
<div class="header" id="qtdocheader">
<div class="content">
<div id="nav-logo">
<a href="index.html">Home</a></div>
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
<div id="narrowsearch"></div>
<div id="nav-topright">
<ul>
<li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li>
<li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li>
<li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/">
DOC</a></li>
<li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li>
</ul>
</div>
<div id="shortCut">
<ul>
<li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li>
<li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li>
</ul>
</div>
<ul class="sf-menu" id="narrowmenu">
<li><a href="#">API Lookup</a>
<ul>
<li><a href="classes.html">Class index</a></li>
<li><a href="functions.html">Function index</a></li>
<li><a href="modules.html">Modules</a></li>
<li><a href="namespaces.html">Namespaces</a></li>
<li><a href="qtglobal.html">Global Declarations</a></li>
<li><a href="qdeclarativeelements.html">QML elements</a></li>
</ul>
</li>
<li><a href="#">Qt Topics</a>
<ul>
<li><a href="qt-basic-concepts.html">Programming with Qt</a></li>
<li><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>
<li><a href="qt-gui-concepts.html">UI Design with Qt</a></li>
<li><a href="supported-platforms.html">Supported Platforms</a></li>
<li><a href="technology-apis.html">Qt and Key Technologies</a></li>
<li><a href="best-practices.html">How-To's and Best Practices</a></li>
</ul>
</li>
<li><a href="#">Examples</a>
<ul>
<li><a href="all-examples.html">Examples</a></li>
<li><a href="tutorials.html">Tutorials</a></li>
<li><a href="demos.html">Demos</a></li>
<li><a href="qdeclarativeexamples.html">QML Examples</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="wrapper">
<div class="hd">
<span></span>
</div>
<div class="bd group">
<div class="sidebar">
<div class="searchlabel">
Search index:</div>
<div class="search" id="sidebarsearch">
<form id="qtdocsearch" action="" onsubmit="return false;">
<fieldset>
<input type="text" name="searchstring" id="pageType" value="" />
<div id="resultdialog">
<a href="#" id="resultclose">Close</a>
<p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p>
<p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span>&nbsp;results:</p>
<ul id="resultlist" class="all">
</ul>
</div>
</fieldset>
</form>
</div>
<div class="box first bottombar" id="lookup">
<h2 title="API Lookup"><span></span>
API Lookup</h2>
<div id="list001" class="list">
<ul id="ul001" >
<li class="defaultLink"><a href="classes.html">Class index</a></li>
<li class="defaultLink"><a href="functions.html">Function index</a></li>
<li class="defaultLink"><a href="modules.html">Modules</a></li>
<li class="defaultLink"><a href="namespaces.html">Namespaces</a></li>
<li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li>
<li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li>
</ul>
</div>
</div>
<div class="box bottombar" id="topics">
<h2 title="Qt Topics"><span></span>
Qt Topics</h2>
<div id="list002" class="list">
<ul id="ul002" >
<li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li>
<li class="defaultLink"><a href="qtquick.html">Device UIs &amp; Qt Quick</a></li>
<li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li>
<li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li>
<li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li>
<li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li>
</ul>
</div>
</div>
<div class="box" id="examples">
<h2 title="Examples"><span></span>
Examples</h2>
<div id="list003" class="list">
<ul id="ul003">
<li class="defaultLink"><a href="all-examples.html">Examples</a></li>
<li class="defaultLink"><a href="tutorials.html">Tutorials</a></li>
<li class="defaultLink"><a href="demos.html">Demos</a></li>
<li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li>
</ul>
</div>
</div>
</div>
<div class="wrap">
<div class="toolbar">
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li>The Network Module in Qt 4</li>
</ul>
</div>
<div class="toolbuttons toolblock">
<ul>
<li id="smallA" class="t_button">A</li>
<li id="medA" class="t_button active">A</li>
<li id="bigA" class="t_button">A</li>
<li id="print" class="t_button"><a href="javascript:this.print();">
<span>Print</span></a></li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="qt4-sql.html" />
<link rel="next" href="qt4-styles.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qt4-sql.html">The Qt 4 Database GUI Layer</a>
<a class="nextPage" href="qt4-styles.html">The Qt 4 Style API</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#general-overview">General Overview</a></li>
<li class="level1"><a href="#example-code">Example Code</a></li>
<li class="level2"><a href="#tcp-client">TCP Client</a></li>
<li class="level2"><a href="#tcp-server">TCP Server</a></li>
<li class="level2"><a href="#udp-senders-and-receivers">UDP Senders and Receivers</a></li>
<li class="level1"><a href="#comparison-with-qt-3">Comparison with Qt 3</a></li>
</ul>
</div>
<h1 class="title">The Network Module in Qt 4</h1>
<span class="subtitle"></span>
<!-- $$$qt4-network.html-description -->
<div class="descr"> <a name="details"></a>
<p>The network module in Qt 4 provides some new features, such as support for internationalized domain names, better IPv6 support, and better performance. And since Qt 4 allows us to break binary compatibility with previous releases, we took this opportunity to improve the class names and API to make them more intuitive to use.</p>
<a name="general-overview"></a>
<h2>General Overview</h2>
<p>Compared to Qt 3, the network module in Qt 4 brings the following benefits:</p>
<ul>
<li>The Qt 4 network classes have more intuitive names and APIs. For example, <a href="porting4.html#qserversocket">QServerSocket</a> has been renamed <a href="qtcpserver.html">QTcpServer</a>.</li>
<li>The entire network module is <a href="threads-reentrancy.html#reentrant">reentrant</a>, making it possible to use them simultaneously from multiple threads.</li>
<li>It is now possible to send and receive UDP datagrams and to use synchronous (i.e&#x2e;, blocking) sockets without having to use a low-level API (<a href="porting4.html#qsocketdevice">QSocketDevice</a> in Qt 3).</li>
<li><a href="qhostaddress.html">QHostAddress</a> and <a href="qhostinfo.html">QHostInfo</a> support internationalized domain names (RFC 3492).</li>
<li><a href="qurl.html">QUrl</a> is more lightweight and fully supports the latest URI specification draft.</li>
<li>UDP broadcasting is now supported.</li>
</ul>
<p>The Qt 4 network module provides fundamental classes for writing TCP and UDP applications, as well as higher-level classes that implement the client side of the HTTP and FTP protocols.</p>
<p>Here's an overview of the TCP and UDP classes:</p>
<ul>
<li><a href="qtcpsocket.html">QTcpSocket</a> encapsulates a TCP socket. It inherits from <a href="qiodevice.html">QIODevice</a>, so you can use <a href="qtextstream.html">QTextStream</a> and <a href="qdatastream.html">QDataStream</a> to read or write data. It is useful for writing both clients and servers.</li>
<li><a href="qtcpserver.html">QTcpServer</a> allows you to listen on a certain port on a server. It emits a <a href="qtcpserver.html#newConnection">newConnection()</a> signal every time a client tries to connect to the server. Once the connection is established, you can talk to the client using <a href="qtcpsocket.html">QTcpSocket</a>.</li>
<li><a href="qudpsocket.html">QUdpSocket</a> is an API for sending and receiving UDP datagrams.</li>
</ul>
<p><a href="qtcpsocket.html">QTcpSocket</a> and <a href="qudpsocket.html">QUdpSocket</a> inherit most of their functionality from <a href="qabstractsocket.html">QAbstractSocket</a>. You can also use <a href="qabstractsocket.html">QAbstractSocket</a> directly as a wrapper around a native socket descriptor.</p>
<p>By default, the socket classes work asynchronously (i.e&#x2e;, they are non-blocking), emitting signals to notify when data has arrived or when the peer has closed the connection. In multithreaded applications and in non-GUI applications, you also have the opportunity of using blocking (synchronous) functions on the socket, which often results in a more straightforward style of programming, with the networking logic concentrated in one or two functions instead of spread across multiple slots.</p>
<p><a href="qftp.html">QFtp</a> and <a href="qnetworkaccessmanager.html">QNetworkAccessManager</a> and its associated classes use <a href="qtcpsocket.html">QTcpSocket</a> internally to implement the FTP and HTTP protocols. The classes work asynchronously and can schedule (i.e&#x2e;, queue) requests.</p>
<p>The network module contains four helper classes: <a href="qhostaddress.html">QHostAddress</a>, <a href="qhostinfo.html">QHostInfo</a>, <a href="qurl.html">QUrl</a>, and <a href="qurlinfo.html">QUrlInfo</a>. <a href="qhostaddress.html">QHostAddress</a> stores an IPv4 or IPv6 address, <a href="qhostinfo.html">QHostInfo</a> resolves host names into addresses, <a href="qurl.html">QUrl</a> stores a URL, and <a href="qurlinfo.html">QUrlInfo</a> stores information about a resource pointed to by a URL, such as the file size and modification date. (Because <a href="qurl.html">QUrl</a> is used by <a href="qtextbrowser.html">QTextBrowser</a>, it is part of the <a href="qtcore.html">QtCore</a> library and not of <a href="qtnetwork.html">QtNetwork</a>.)</p>
<p>See the <a href="qtnetwork.html">QtNetwork</a> module overview for more information.</p>
<a name="example-code"></a>
<h2>Example Code</h2>
<p>All the code snippets presented here are quoted from self-contained, compilable examples located in Qt's <tt>examples/network</tt> directory.</p>
<a name="tcp-client"></a>
<h3>TCP Client</h3>
<p>The first example illustrates how to write a TCP client using <a href="qtcpsocket.html">QTcpSocket</a>. The client talks to a fortune server that provides fortune to the user. Here's how to set up the socket:</p>
<pre class="cpp"> tcpSocket <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtcpsocket.html">QTcpSocket</a></span>(<span class="keyword">this</span>);
connect(tcpSocket<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(readFortune()));
connect(tcpSocket<span class="operator">,</span> SIGNAL(error(<span class="type"><a href="qabstractsocket.html">QAbstractSocket</a></span><span class="operator">::</span>SocketError))<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(displayError(<span class="type"><a href="qabstractsocket.html">QAbstractSocket</a></span><span class="operator">::</span>SocketError)));</pre>
<p>When the user requests a new fortune, the client establishes a connection to the server:</p>
<pre class="cpp"> tcpSocket<span class="operator">-</span><span class="operator">&gt;</span>connectToHost(hostLineEdit<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">,</span>
portLineEdit<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toInt());</pre>
<p>When the server answers, the following code is executed to read the data from the socket:</p>
<pre class="cpp"> <span class="type"><a href="qdatastream.html">QDataStream</a></span> in(tcpSocket);
in<span class="operator">.</span>setVersion(<span class="type"><a href="qdatastream.html">QDataStream</a></span><span class="operator">::</span>Qt_4_0);
<span class="keyword">if</span> (blockSize <span class="operator">=</span><span class="operator">=</span> <span class="number">0</span>) {
<span class="keyword">if</span> (tcpSocket<span class="operator">-</span><span class="operator">&gt;</span>bytesAvailable() <span class="operator">&lt;</span> (<span class="type">int</span>)<span class="keyword">sizeof</span>(<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>))
<span class="keyword">return</span>;
in <span class="operator">&gt;</span><span class="operator">&gt;</span> blockSize;
}
<span class="keyword">if</span> (tcpSocket<span class="operator">-</span><span class="operator">&gt;</span>bytesAvailable() <span class="operator">&lt;</span> blockSize)
<span class="keyword">return</span>;
<span class="type"><a href="qstring.html">QString</a></span> nextFortune;
in <span class="operator">&gt;</span><span class="operator">&gt;</span> nextFortune;
<span class="keyword">if</span> (nextFortune <span class="operator">=</span><span class="operator">=</span> currentFortune) {
<span class="type"><a href="qtimer.html">QTimer</a></span><span class="operator">::</span>singleShot(<span class="number">0</span><span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(requestNewFortune()));
<span class="keyword">return</span>;
}
currentFortune <span class="operator">=</span> nextFortune;</pre>
<p>The server's answer starts with a <i>size</i> field (which we store in <tt>blockSize</tt>), followed by <i>size</i> bytes of data. If the client hasn't received all the data yet, it waits for the server to send more.</p>
<p>An alternative approach is to use a blocking socket. The code can then be concentrated in one function:</p>
<pre class="cpp"> <span class="keyword">const</span> <span class="type">int</span> Timeout <span class="operator">=</span> <span class="number">5</span> <span class="operator">*</span> <span class="number">1000</span>;
<span class="type"><a href="qtcpsocket.html">QTcpSocket</a></span> socket;
socket<span class="operator">.</span>connectToHost(serverName<span class="operator">,</span> serverPort);
<span class="keyword">if</span> (<span class="operator">!</span>socket<span class="operator">.</span>waitForConnected(Timeout)) {
<span class="keyword">emit</span> error(socket<span class="operator">.</span>error()<span class="operator">,</span> socket<span class="operator">.</span>errorString());
<span class="keyword">return</span>;
}
<span class="keyword">while</span> (socket<span class="operator">.</span>bytesAvailable() <span class="operator">&lt;</span> (<span class="type">int</span>)<span class="keyword">sizeof</span>(<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>)) {
<span class="keyword">if</span> (<span class="operator">!</span>socket<span class="operator">.</span>waitForReadyRead(Timeout)) {
<span class="keyword">emit</span> error(socket<span class="operator">.</span>error()<span class="operator">,</span> socket<span class="operator">.</span>errorString());
<span class="keyword">return</span>;
}
}
<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span> blockSize;
<span class="type"><a href="qdatastream.html">QDataStream</a></span> in(<span class="operator">&amp;</span>socket);
in<span class="operator">.</span>setVersion(<span class="type"><a href="qdatastream.html">QDataStream</a></span><span class="operator">::</span>Qt_4_0);
in <span class="operator">&gt;</span><span class="operator">&gt;</span> blockSize;
<span class="keyword">while</span> (socket<span class="operator">.</span>bytesAvailable() <span class="operator">&lt;</span> blockSize) {
<span class="keyword">if</span> (<span class="operator">!</span>socket<span class="operator">.</span>waitForReadyRead(Timeout)) {
<span class="keyword">emit</span> error(socket<span class="operator">.</span>error()<span class="operator">,</span> socket<span class="operator">.</span>errorString());
<span class="keyword">return</span>;
}
}
mutex<span class="operator">.</span>lock();
<span class="type"><a href="qstring.html">QString</a></span> fortune;
in <span class="operator">&gt;</span><span class="operator">&gt;</span> fortune;
<span class="keyword">emit</span> newFortune(fortune);</pre>
<a name="tcp-server"></a>
<h3>TCP Server</h3>
<p>The following code snippets illustrate how to write a TCP server using <a href="qtcpserver.html">QTcpServer</a> and <a href="qtcpsocket.html">QTcpSocket</a>. Here's how to set up a TCP server:</p>
<pre class="cpp"> tcpServer <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qtcpserver.html">QTcpServer</a></span>(<span class="keyword">this</span>);
<span class="keyword">if</span> (<span class="operator">!</span>tcpServer<span class="operator">-</span><span class="operator">&gt;</span>listen()) {
<span class="type"><a href="qmessagebox.html">QMessageBox</a></span><span class="operator">::</span>critical(<span class="keyword">this</span><span class="operator">,</span> tr(<span class="string">&quot;Fortune Server&quot;</span>)<span class="operator">,</span>
tr(<span class="string">&quot;Unable to start the server: %1.&quot;</span>)
<span class="operator">.</span>arg(tcpServer<span class="operator">-</span><span class="operator">&gt;</span>errorString()));
close();
<span class="keyword">return</span>;
}
connect(tcpServer<span class="operator">,</span> SIGNAL(newConnection())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> SLOT(sendFortune()));</pre>
<p>When a client tries to connect to the server, the following code in the sendFortune() slot is executed:</p>
<pre class="cpp"> <span class="type"><a href="qbytearray.html">QByteArray</a></span> block;
<span class="type"><a href="qdatastream.html">QDataStream</a></span> out(<span class="operator">&amp;</span>block<span class="operator">,</span> <span class="type"><a href="qiodevice.html">QIODevice</a></span><span class="operator">::</span>WriteOnly);
out<span class="operator">.</span>setVersion(<span class="type"><a href="qdatastream.html">QDataStream</a></span><span class="operator">::</span>Qt_4_0);
out <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>)<span class="number">0</span>;
out <span class="operator">&lt;</span><span class="operator">&lt;</span> fortunes<span class="operator">.</span>at(qrand() <span class="operator">%</span> fortunes<span class="operator">.</span>size());
out<span class="operator">.</span>device()<span class="operator">-</span><span class="operator">&gt;</span>seek(<span class="number">0</span>);
out <span class="operator">&lt;</span><span class="operator">&lt;</span> (<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>)(block<span class="operator">.</span>size() <span class="operator">-</span> <span class="keyword">sizeof</span>(<span class="type"><a href="qtglobal.html#quint16-typedef">quint16</a></span>));
<span class="type"><a href="qtcpsocket.html">QTcpSocket</a></span> <span class="operator">*</span>clientConnection <span class="operator">=</span> tcpServer<span class="operator">-</span><span class="operator">&gt;</span>nextPendingConnection();
connect(clientConnection<span class="operator">,</span> SIGNAL(disconnected())<span class="operator">,</span>
clientConnection<span class="operator">,</span> SLOT(deleteLater()));
clientConnection<span class="operator">-</span><span class="operator">&gt;</span>write(block);
clientConnection<span class="operator">-</span><span class="operator">&gt;</span>disconnectFromHost();</pre>
<a name="udp-senders-and-receivers"></a>
<h3>UDP Senders and Receivers</h3>
<p>Here's how to broadcast a UDP datagram:</p>
<pre class="cpp"> udpSocket <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qudpsocket.html">QUdpSocket</a></span>(<span class="keyword">this</span>);
<span class="type"><a href="qbytearray.html">QByteArray</a></span> datagram <span class="operator">=</span> <span class="string">&quot;Broadcast message &quot;</span> <span class="operator">+</span> <span class="type"><a href="qbytearray.html">QByteArray</a></span><span class="operator">::</span>number(messageNo);
udpSocket<span class="operator">-</span><span class="operator">&gt;</span>writeDatagram(datagram<span class="operator">.</span>data()<span class="operator">,</span> datagram<span class="operator">.</span>size()<span class="operator">,</span>
<span class="type"><a href="qhostaddress.html">QHostAddress</a></span><span class="operator">::</span>Broadcast<span class="operator">,</span> <span class="number">45454</span>);</pre>
<p>Here's how to receive a UDP datagram:</p>
<pre class="cpp"> udpSocket <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qudpsocket.html">QUdpSocket</a></span>(<span class="keyword">this</span>);
udpSocket<span class="operator">-</span><span class="operator">&gt;</span>bind(<span class="number">45454</span><span class="operator">,</span> <span class="type"><a href="qudpsocket.html">QUdpSocket</a></span><span class="operator">::</span>ShareAddress);
connect(udpSocket<span class="operator">,</span> SIGNAL(readyRead())<span class="operator">,</span>
<span class="keyword">this</span><span class="operator">,</span> SLOT(processPendingDatagrams()));</pre>
<p>Then in the processPendingDatagrams() slot:</p>
<pre class="cpp"> <span class="keyword">while</span> (udpSocket<span class="operator">-</span><span class="operator">&gt;</span>hasPendingDatagrams()) {
<span class="type"><a href="qbytearray.html">QByteArray</a></span> datagram;
datagram<span class="operator">.</span>resize(udpSocket<span class="operator">-</span><span class="operator">&gt;</span>pendingDatagramSize());
udpSocket<span class="operator">-</span><span class="operator">&gt;</span>readDatagram(datagram<span class="operator">.</span>data()<span class="operator">,</span> datagram<span class="operator">.</span>size());
statusLabel<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;Received datagram: \&quot;%1\&quot;&quot;</span>)
<span class="operator">.</span>arg(datagram<span class="operator">.</span>data()));
}</pre>
<a name="comparison-with-qt-3"></a>
<h2>Comparison with Qt 3</h2>
<p>The main difference between Qt 3 and Qt 4 is that the very high level <a href="porting4.html#qnetworkprotocol">QNetworkProtocol</a> and <a href="porting4.html#qurloperator">QUrlOperator</a> abstraction has been eliminated. These classes attempted the impossible (unify FTP and HTTP under one roof), and unsurprisingly failed at that. Qt 4 still provides <a href="qftp.html">QFtp</a>, and it also provides the <a href="qnetworkaccessmanager.html">QNetworkAccessManager</a>.</p>
<p>The <a href="porting4.html#qsocket">QSocket</a> class in Qt 3 has been renamed <a href="qtcpsocket.html">QTcpSocket</a>. The new class is reentrant and supports blocking. It's also easier to handle closing than with Qt 3, where you had to connect to both the QSocket::connectionClosed() and the QSocket::delayedCloseFinished() signals.</p>
<p>The <a href="porting4.html#qserversocket">QServerSocket</a> class in Qt 3 has been renamed <a href="qtcpserver.html">QTcpServer</a>. The API has changed quite a bit. While in Qt 3 it was necessary to subclass <a href="porting4.html#qserversocket">QServerSocket</a> and reimplement the newConnection() pure virtual function, <a href="qtcpserver.html">QTcpServer</a> now emits a <a href="qtcpserver.html#newConnection">newConnection()</a> signal that you can connect to a slot.</p>
<p>The <a href="qhostinfo.html">QHostInfo</a> class has been redesigned to use the operating system's getaddrinfo() function instead of implementing the DNS protocol. Internally, <a href="qhostinfo.html">QHostInfo</a> simply starts a thread and calls getaddrinfo() in that thread. This wasn't possible in Qt 3 because getaddrinfo() is a blocking call and Qt 3 could be configured without multithreading support.</p>
<p>The <a href="porting4.html#qsocketdevice">QSocketDevice</a> class in Qt 3 is no longer part of the public Qt API. If you used <a href="porting4.html#qsocketdevice">QSocketDevice</a> to send or receive UDP datagrams, use <a href="qudpsocket.html">QUdpSocket</a> instead. If you used <a href="porting4.html#qsocketdevice">QSocketDevice</a> because it supported blocking sockets, use <a href="qtcpsocket.html">QTcpSocket</a> or <a href="qudpsocket.html">QUdpSocket</a> instead and use the blocking functions (<a href="qabstractsocket.html#waitForConnected">waitForConnected()</a>, <a href="qabstractsocket.html#waitForConnected">waitForReadyRead()</a>, etc.)&#x2e; If you used <a href="porting4.html#qsocketdevice">QSocketDevice</a> from a non-GUI thread because it was the only reentrant networking class in Qt 3, use <a href="qtcpsocket.html">QTcpSocket</a>, <a href="qtcpserver.html">QTcpServer</a>, or <a href="qudpsocket.html">QUdpSocket</a> instead.</p>
<p>Internally, Qt 4 has a class called QSocketLayer that provides a cross-platform low-level socket API. It resembles the old <a href="porting4.html#qsocketdevice">QSocketDevice</a> class. We might make it public in a later release if users ask for it.</p>
<p>As an aid to porting to Qt 4, the <a href="qt3support.html">Qt3Support</a> library includes <a href="q3dns.html" class="compat">Q3Dns</a>, <a href="q3serversocket.html" class="compat">Q3ServerSocket</a>, <a href="q3socket.html" class="compat">Q3Socket</a>, and <a href="q3socketdevice.html" class="compat">Q3SocketDevice</a> classes.</p>
</div>
<!-- @@@qt4-network.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qt4-sql.html">The Qt 4 Database GUI Layer</a>
<a class="nextPage" href="qt4-styles.html">The Qt 4 Style API</a>
</p>
</div>
</div>
</div>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">&copy;</acronym> 2015 The Qt Company Ltd.
Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Qt and respective logos are trademarks of The Qt Company Ltd
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
<script src="scripts/functions.js" type="text/javascript"></script>
</body>
</html>