1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \page qt4-network.html
|
---|
44 | \title The Network Module in Qt 4
|
---|
45 |
|
---|
46 | \contentspage {What's New in Qt 4}{Home}
|
---|
47 | \previouspage The Qt 4 Database GUI Layer
|
---|
48 | \nextpage The Qt 4 Style API
|
---|
49 |
|
---|
50 | The network module in Qt 4 provides some new features, such as
|
---|
51 | support for internationalized domain names, better IPv6 support,
|
---|
52 | and better performance. And since Qt 4 allows us to break binary
|
---|
53 | compatibility with previous releases, we took this opportunity to
|
---|
54 | improve the class names and API to make them more intuitive to
|
---|
55 | use.
|
---|
56 |
|
---|
57 | \tableofcontents
|
---|
58 |
|
---|
59 | \section1 General Overview
|
---|
60 |
|
---|
61 | Compared to Qt 3, the network module in Qt 4 brings the following
|
---|
62 | benefits:
|
---|
63 |
|
---|
64 | \list
|
---|
65 | \o The Qt 4 network classes have more intuitive names and APIs.
|
---|
66 | For example, QServerSocket has been renamed QTcpServer.
|
---|
67 | \o The entire network module is \l{reentrant}, making it
|
---|
68 | possible to use them simultaneously from multiple threads.
|
---|
69 | \o It is now possible to send and receive UDP datagrams and to
|
---|
70 | use synchronous (i.e., blocking) sockets without having to
|
---|
71 | use a low-level API (QSocketDevice in Qt 3).
|
---|
72 | \o QHostAddress and QHostInfo support internationalized domain names
|
---|
73 | (RFC 3492).
|
---|
74 | \o QUrl is more lightweight and fully supports the latest URI
|
---|
75 | specification draft.
|
---|
76 | \o UDP broadcasting is now supported.
|
---|
77 | \endlist
|
---|
78 |
|
---|
79 | The Qt 4 network module provides fundamental classes for writing
|
---|
80 | TCP and UDP applications, as well as higher-level classes that
|
---|
81 | implement the client side of the HTTP and FTP protocols.
|
---|
82 |
|
---|
83 | Here's an overview of the TCP and UDP classes:
|
---|
84 |
|
---|
85 | \list
|
---|
86 | \o QTcpSocket encapsulates a TCP socket. It inherits from
|
---|
87 | QIODevice, so you can use QTextStream and QDataStream to read
|
---|
88 | or write data. It is useful for writing both clients and
|
---|
89 | servers.
|
---|
90 | \o QTcpServer allows you to listen on a certain port on a
|
---|
91 | server. It emits a
|
---|
92 | \l{QTcpServer::newConnection()}{newConnection()} signal every
|
---|
93 | time a client tries to connect to the server. Once the
|
---|
94 | connection is established, you can talk to the client using
|
---|
95 | QTcpSocket.
|
---|
96 | \o QUdpSocket is an API for sending and receiving UDP datagrams.
|
---|
97 | \endlist
|
---|
98 |
|
---|
99 | QTcpSocket and QUdpSocket inherit most of their functionality
|
---|
100 | from QAbstractSocket. You can also use QAbstractSocket directly
|
---|
101 | as a wrapper around a native socket descriptor.
|
---|
102 |
|
---|
103 | By default, the socket classes work asynchronously (i.e., they
|
---|
104 | are non-blocking), emitting signals to notify when data has
|
---|
105 | arrived or when the peer has closed the connection. In
|
---|
106 | multithreaded applications and in non-GUI applications, you also
|
---|
107 | have the opportunity of using blocking (synchronous) functions on
|
---|
108 | the socket, which often results in a more straightforward style
|
---|
109 | of programming, with the networking logic concentrated in one or
|
---|
110 | two functions instead of spread across multiple slots.
|
---|
111 |
|
---|
112 | QFtp and QNetworkAccessManager and its associated classes use
|
---|
113 | QTcpSocket internally to implement the FTP and HTTP protocols. The
|
---|
114 | classes work asynchronously and can schedule (i.e., queue)
|
---|
115 | requests.
|
---|
116 |
|
---|
117 | The network module contains four helper classes: QHostAddress,
|
---|
118 | QHostInfo, QUrl, and QUrlInfo. QHostAddress stores an IPv4 or IPv6
|
---|
119 | address, QHostInfo resolves host names into addresses, QUrl stores a
|
---|
120 | URL, and QUrlInfo stores information about a resource pointed to
|
---|
121 | by a URL, such as the file size and modification date. (Because
|
---|
122 | QUrl is used by QTextBrowser, it is part of the QtCore library and
|
---|
123 | not of QtNetwork.)
|
---|
124 |
|
---|
125 | See the \l QtNetwork module overview for more information.
|
---|
126 |
|
---|
127 | \section1 Example Code
|
---|
128 |
|
---|
129 | All the code snippets presented here are quoted from
|
---|
130 | self-contained, compilable examples located in Qt's \c
|
---|
131 | examples/network directory.
|
---|
132 |
|
---|
133 | \section2 TCP Client
|
---|
134 |
|
---|
135 | The first example illustrates how to write a TCP client using
|
---|
136 | QTcpSocket. The client talks to a fortune server that provides
|
---|
137 | fortune to the user. Here's how to set up the socket:
|
---|
138 |
|
---|
139 | \snippet examples/network/fortuneclient/client.cpp 1
|
---|
140 | \codeline
|
---|
141 | \snippet examples/network/fortuneclient/client.cpp 2
|
---|
142 | \snippet examples/network/fortuneclient/client.cpp 4
|
---|
143 |
|
---|
144 | When the user requests a new fortune, the client establishes a
|
---|
145 | connection to the server:
|
---|
146 |
|
---|
147 | \snippet examples/network/fortuneclient/client.cpp 7
|
---|
148 |
|
---|
149 | When the server answers, the following code is executed to read
|
---|
150 | the data from the socket:
|
---|
151 |
|
---|
152 | \snippet examples/network/fortuneclient/client.cpp 9
|
---|
|
---|