Return to Linux Life Edit

  ホーム · 全てのクラス · メインのクラス · 注釈付き · グループ別 · 関数一覧

Networkモジュール

ネットワークモジュールはネットワークプログラミングを簡単かつポータブルにおこなうことのできるクラスを提供する. 特定のアプリケーションレベルのプロトコルが実装された QHttp QFtp のような高レベルのクラスと、 QTcpSocket, QTcpServer, および QUdpSocketのような独自の TCP/IP クライアント サーバを書くことを可能にする低レベルのクラスの両方が用意されている.

この文書ではネットワークモジュールの主なクラスの概略について説明する. 詳細については個々のクラスのリファレンスを参照のこと.

QHttp と QFtp による HTTP と FTP クライアントの作成

HTTP (Hypertext Transfer Protocol) は主として HTML と XML ファイルのダウンロードに用いられるアプリケーションレベルのネットワークプロトコルである. たとえば、HTTP はインターネット上で注文情報を転送するために用いられている. 対照的に FTP (File Transfer Protocol) は、もっぱらリモートのディレクトリを閲覧しファイルを転送するために使われている.

多くの点で HTTP は FTP よりもシンプルなプロトコルである. FTP はネットワークコネクションを2本使う (コマンドを送信するのに1本、データを転送するためもう1本) のに対し、HTTP はたった一本しか使わない. HTTP はステートレスなプロトコルであり、要求と応答は常に自己充足的である. FTP プロトコルはステートを持ち、クライアントはファイルを転送する前にいくつかのコマンドを送信しなければならない.

実際、FTP クライアントが一つのコネクションを確立し、セッションを通してそれを保持しつづけるのに対して、HTTP クライアントは個々の要求のために個々のコネクションを使うのが普通である.

QHttp to QFtp のクラスでは、クライアント側における HTTP と FTP のサポートが提供される. 2つのプロトコルは同じ問題を解決するのに利用されるので、 QHttp QFtp のクラスには共通する多くの特徴がある:

QHttp QFtpの利用には二種類の代表的方法がある. 最も一般的な方法は,コマンドIDトラッキングして適切なシグナルへ接続し,あらゆるコマンド実行を行う方法である. 他の方法は,全コマンドを最初に計画し,全計画コマンドの実行が終った時点で発生する done()シグナルにのみ接続する方法である. 最初のアプローチはより多くの作業が必要であるが,個々のコマンド実行をより詳細にコントロールでき,コマンド実行の結果を受けて次のコマンドを実行することが出来る.ユーザに結果詳細を提供可能.

HTTP FTP の例では、HTTP と FTP クライアントの作りかたを説明している.

独自の HTTP または FTP サーバの作成については、低レベルのクラス QTcpSocket QTcpServerを使うことで可能である.

QTcpSocket と QTcpServer による TCP の使い方

TCP (Transmission Control Protocol) は HTTP および FTP を含む、データ転送に関する多くのインターネットプロトコルで利用される低レベルのネットワークプロトコルである. 信頼性の高い、ストリーム指向、コネクション指向の転送プロトコルである. 特にデータの継続的な伝達に適している

QTcpSocket クラスは TCP のインターフェースを提供する. QTcpSocket を利用することで POP3, SMTP, NNTP といった標準的なネットワークプロトコルからカスタムのプロトコルに至るまで実装することができる.

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 QTcpSocket::peerAddress() and QTcpSocket::peerPort(). At any time, the peer can close the connection, and data transfer will then stop immediately.

QTcpSocket works asynchronously and emits signals to report status changes and errors, just like QHttp and QFtp. It relies on the event loop to detect incoming data and to automatically flush outgoing data. You can write data to the socket using QTcpSocket::write(), and read data using QTcpSocket::read(). QTcpSocket represents two independent streams of data: one for reading and one for writing.

Since QTcpSocket inherits QIODevice, you can use it with QTextStream and QDataStream. When reading from a QTcpSocket, you must make sure that enough data is available by calling QTcpSocket::bytesAvailable() beforehand.

If you need to handle incoming TCP connections (e.g., in a server application), use the QTcpServer class. Call QTcpServer::listen() to set up the server, and connect to the QTcpServer::newConnection() signal, which is emitted once for every client that connects. In your slot, call QTcpServer::nextPendingConnection() to accept the connection and use the returned QTcpSocket to communicate with the client.

Although most of its functions work asynchronously, it's possible to use QTcpSocket synchronously (i.e., blocking). The trick is to call QTcpSocket's waitFor...() functions, which suspend the calling thread until a signal has been emitted. For example, after calling the non-blocking QTcpSocket::connectToHost() function, call QTcpSocket::waitForConnected() to block the thread until the connected() signal has been emitted.

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, QTcpSocket doesn't require an event loop.

The Fortune Client and Fortune Server examples show how to use QTcpSocket and QTcpServer to write TCP client-server applications. See also Blocking Fortune Client for an example on how to use a synchronous QTcpSocket in a separate thread (without using an event loop), and Threaded Fortune Server for an example of a multithreaded TCP server with one thread per active client.

QUdpSocketでUDPを使う

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.

The QUdpSocket class allows you to send and receive UDP datagrams. It inherits QAbstractSocket, and it therefore shares most of QTcpSocket's interface. The main difference is that QUdpSocket 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.

QUdpSocket 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 QHostAddress::Broadcast (255.255.255.255), or to your local network's broadcast address.

QUdpSocket::bind() prepares the socket for accepting incoming datagrams, much like QTcpServer::listen() for TCP servers. Whenever one or more datagrams arrive, QUdpSocket emits the readyRead() signal. Call QUdpSocket::readDatagram() to read the datagram.

The Broadcast Sender and Broadcast Receiver examples show how to write a UDP sender and a UDP receiver using Qt.

QHostInfoでホスト名を解決する

Before establishing a network connection, QTcpSocket and QUdpSocket 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.

QHostInfo provides a static function that lets you perform such a lookup yourself. By passing a host name to QHostInfo::lookupHost() with a host name, a QObject pointer, and a slot signature, QHostInfo 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.

QHostInfo also provides a static function called QHostInfo::fromName() 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.)


Copyright © 2005 Trolltech Trademarks
Qt 4.0.0