?? network.html
字號(hào):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - Qt Network Module</title><style type="text/css"><!--h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }a:link { color: #004faf; text-decoration: none }a:visited { color: #672967; text-decoration: none }body { background: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Qt Network Module</h1><br clear="all">The network module offers classes to make network programming easierand portable. Basically there are three sets of classes, first verybasic classes like <a href="qsocket.html">QSocket</a>, <a href="qserversocket.html">QServerSocket</a>, <a href="qdns.html">QDns</a>, etc. whichallow to work in a portable way with TCP/IP sockets. In addition,there are classes like <a href="qnetworkprotocol.html">QNetworkProtocol</a>, <a href="qnetworkoperation.html">QNetworkOperation</a> inthe Qt base library, which provide an abstract layer for implementingnetwork protocols and <code>QUrlOperator</code> which operates on such networkprotocols. Finally the third set of network classes are the passiveones, namely <code>QUrl</code> and <code>QUrlInfo</code> which do URL parsing and similarstuff.<p>The first set of classes (<a href="qsocket.html">QSocket</a>, <a href="qserversocket.html">QServerSocket</a>, <a href="qdns.html">QDns</a>, <a href="qftp.html">QFtp</a>, etc.)are included in the "network" module of Qt.<p>The QSocket classes are not directly related to the QNetwork classes,but QSocket should and will be used for implementing networkprotocols, which are directly related to the QNetworkclasses. E.g. the QFtp class (implementation of the FTP protocol) usesQSockets. But QSockets don't need to be used for protocolimplementations, e.g. QLocalFs (which is an implementation of thelocal filesystem as network protocol) uses QDir and no QSocket. UsingQNetworkProtocols you can implement everything which fits into ahierarchical structure and can be accessed using URLs. This could bee.g. a protocol which can read pictures from a digital camera using aserial connection.<p><h2>Working Network Protocol independent with QUrlOperator andQNetworkOperation</h2><p>To just use existing network protocol implementations and operate onURLs using them is quite easy. E.g. downloading a file from an FTPserver to the local filesystem can be done with following code:<p><pre> <a href="qurloperator.html">QUrlOperator</a> op; op.<a href="qurloperator.html#3a9681">copy</a>( "ftp://ftp.trolltech.com/qt/source/qt-2.1.0.tar.gz", "file:/tmp", FALSE );</pre><p>And that's all! Of course an implementation of the FTP protocol has to be availableand registered for doing that. More information on that later.<p>You can also do stuff like creating directories, removing files, renaming, etc. E.g. tocreate a folder on a private FTP account do<p><pre> <a href="qurloperator.html">QUrlOperator</a> op( "ftp://username:password@host.domain.no/home/username" ); op.<a href="qurloperator.html#9867b2">mkdir</a>( "New Directory" );</pre><p>That's it again. To see all available operations, look at the <code>QUrlOperator</code> classdocumentation.<p>Now as everything works asynchronous, the function call for an operation returnsbefore the operation has been processed. So you don't get a return value whichtells you something about failure or success. The return value always is a pointerto a <code>QNetworkOperation.</code><p>In this <code>QNetworkOperation</code> all information about the operation is stored.There is e.g. a method of <code>QNetworkOperation</code> which returns the state ofthis operation. Using that you can find out all the time in which state the operationcurrently is. Also you get the arguments you passed to the <code>QUrlOperator</code>method, the type of the operation and some more stuff from this <code>QNetworkOperation</code>object. For more details see the class documentation of <code>QNetworkOperation.</code><p>Now, later you get signals emitted by the <code>QUrlOperator,</code> which inform you about the processof the operations. As you can call many methods which operate on an URL of one<code>QUrlOperator,</code> it queues up all these operations. So you can't know which operationthe <code>QUrlOperator</code> just processes. Because of this you get in each signal as the last argumenta pointer to the <code>QNetworkOperation</code> object which is just processed and from which thissignal comes.<p>Some of these operations send a <code>start()</code> signal at the beginning (depending if it makessense or not), then some of them send some signals during processing the operation,and <b>all</b> operations send a <code>finished()</code> signal after they are done. Now, finishedcould mean that the operation has been successfully finished or that it failed. To findthat out you can use the <code>QNetworkOperation</code> pointer you got with the <code>finished()</code>signal. If <code>QNetworkOperation::state()</code> equals <code>QNetworkProtocol::StDone</code> theoperation finished successful, if it is <code>QNetworkProtocol::StFailed</code> the operation failed.<p>Now, a slot which you connected to the <code>QUrlOperator::finished(</code> QNetworkOperation * )signal could look like this<p><pre>void MyClass::slotOperationFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){ switch ( op-><a href="qnetworkoperation.html#54b287">operation</a>() ) { case QNetworkProtocol::OpMkdir: { if ( op-><a href="qnetworkoperation.html#0f94ff">state</a>() == QNetworkProtocol::StFailed ) <a href="qapplication.html#72e78c">qDebug</a>( "Couldn't create directory %s", op-><a href="qnetworkoperation.html#e142fb">arg</a>( 0 ).latin1() ); else <a href="qapplication.html#72e78c">qDebug</a>( "Successfully created directory %s", op-><a href="qnetworkoperation.html#e142fb">arg</a>( 0 ).latin1() ); } break; // ... and so on }}</pre><p>As mentioned before, some operations send other signals too. Let's takethe list children operation as an example (e.g. read the directory of adirectory on a FTP server):<p><pre><a href="qurloperator.html">QUrlOperator</a> op;MyClass::MyClass() : <a href="qobject.html">QObject</a>(), op( "ftp://ftp.trolltech.com" ){ connect( &op, SIGNAL( newChildren( const QValueList<<a href="qurlinfo.html">QUrlInfo</a>> &, QNetworkOperation * ) ), this, SLOT( slotInsertEntries( const QValueList<<a href="qurlinfo.html">QUrlInfo</a>> &, QNetworkOperation * ) ) ); connect( &op, SIGNAL( start( <a href="qnetworkoperation.html">QNetworkOperation</a> * ) ), this, SLOT( slotStart( <a href="qnetworkoperation.html">QNetworkOperation</a> *) ) ); connect( &op, SIGNAL( finished( <a href="qnetworkoperation.html">QNetworkOperation</a> * ) ), this, SLOT( slotFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *) ) );}void MyClass::slotInsertEntries( const QValueList<<a href="qurlinfo.html">QUrlInfo</a>> &info, QNetworkOperation * ){ <a href="qvaluelist.html">QValueList</a><<a href="qurlinfo.html">QUrlInfo</a>>::ConstIterator it = info.begin(); for ( ; it != info.end(); ++it ) { const QUrlInfo &inf = *it; <a href="qapplication.html#72e78c">qDebug</a>( "Name: %s, Size: %d, Last Modified: %s", inf.name().latin1(), inf.size(), inf.lastModified().toString().latin1() ); }}void MyClass::slotStart( <a href="qnetworkoperation.html">QNetworkOperation</a> * ){ <a href="qapplication.html#72e78c">qDebug</a>( "Start reading '%s'", op.<a href="qurl.html#c78524">toString</a>().latin1() );}void MyClass::slotFinished( <a href="qnetworkoperation.html">QNetworkOperation</a> *operation ){ if ( operation-><a href="qnetworkoperation.html#54b287">operation</a>() == QNetworkProtocol::OpListChildren ) { if ( operation-><a href="qnetworkoperation.html#0f94ff">state</a>() == QNetworkProtocol::StFailed ) <a href="qapplication.html#72e78c">qDebug</a>( "Couldn't read '%s'! Following error occurred: %s", op.<a href="qurl.html#c78524">toString</a>().latin1(), operation-><a href="qnetworkoperation.html#bf6b66">protocolDetail</a>().latin1() ); else <a href="qapplication.html#72e78c">qDebug</a>( "Finished reading '%s'!", op.<a href="qurl.html#c78524">toString</a>().latin1() ); }}</pre><p>These examples explained now how to use the <code>QUrlOperator</code> and <code>QNetworkOperations.</code>The network extension will contain some good examples for this too.<p><h2>Implementing your own Network Protocol</h2><p><code>QNetworkProtocol</code> provides a base class for implementationsof network protocols and an architecture to a dynamic registrationand unregistration of network protocols. If you use this architectureyou also don't need to care about asynchronous programming, as thearchitecture hides this and does all the work for you.<p>Limitation: As it is quite hard to design a base class fornetwork protocols which satisfies all network protocols,the architecture described here is designed to work with all kindsof hierarchical structures, like filesystems. So everything which canbe interpreted as hierarchical structure and accessed via URLs,can be implemented as network protocol and easily used in Qt. Thisis not limited to filesystems only!<p>To implement a network protocol create a class derived from<code>QNetworkProtocol.</code><p>Other classes will use this network protocol implementationto operate on it. So you should reimplement following protected members<p><pre> void QNetworkProtocol::operationListChildren( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ); void QNetworkProtocol::operationMkDir( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ); void QNetworkProtocol::operationRemove( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ); void QNetworkProtocol::operationRename( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ); void QNetworkProtocol::operationGet( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ); void QNetworkProtocol::operationPut( <a href="qnetworkoperation.html">QNetworkOperation</a> *op );</pre><p>Some words about how to reimplement these methods: You always get a pointer toa <code>QNetworkOperation</code> as argument. This pointer holds all information aboutthe operation in the current state. If you start processing such an operation,set the state to <code>QNetworkProtocol::StInProgress.</code> If you finished processingthe operation, set the state to <code>QNetworkProtocol::StDone</code> if it wassuccessful or <code>QNetworkProtocol::StFailed</code> if an error occurred. If anerror occurred you have to set an error code (see <code>QNetworkOperation::setErrorCode()</code> )and if you know some details (e.g. an error message) you can also set thismessage to the operation pointer (see <code>QNetworkOperation::setProtocolDetail()</code> ).Also you get all information (type, arguments, etc.) of the operationfrom this <code>QNetworkOperation</code> pointer. For details about which argumentsyou can get and set look at the class documentation of <code>QNetworkOperation.</code><p>If you reimplement such an operation method, it's also very importantto emit the correct signals at the correct time: In general always emitat the end of an operation (when you either successfully finished processingthe operation or and error occurred) the <code>finished()</code> signal withthe network operation as argument. The whole network architecturerelies on correctly emitted <code>finished()</code> signals! So be careful with that!Then there are some more special signals which are specific to operations:<ul> <li>Emit in <code>operationListChildren:</code> <ul>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -