networkprotocol-example.html

来自「QT 下载资料仅供参考」· HTML 代码 · 共 365 行 · 第 1/2 页

HTML
365
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/examples/network/networkprotocol/networkprotocol.doc:5 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>A simple NNTP implementation</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: #ffffff; color: black; }--></style></head><body><table border="0" cellpadding="0" cellspacing="0" width="100%"><tr bgcolor="#E5E5E5"><td valign=center> <a href="index.html"><font color="#004faf">Home</font></a> | <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a> | <a href="functions.html"><font color="#004faf">Functions</font></a></td><td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>A simple NNTP implementation</h1> <p> <p> This example shows how to implement your own <a href="qnetworkprotocol.html">QNetworkProtocol</a>. Theprotocol that was chosen for this example is NTTP. Please note that thisimplementation is very simple since it is designed to be an example. Itshould not be used as a real NNTP implemention.<p> <hr><p> Header file (nntp.h):<p> <pre>/****************************************************************************** $Id:  qt/nntp.h   3.0.5   edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#ifndef NNTP_H#define NNTP_H#include &lt;<a href="qsocket-h.html">qsocket.h</a>&gt;#include &lt;<a href="qnetworkprotocol-h.html">qnetworkprotocol.h</a>&gt;class Nntp : public <a href="qnetworkprotocol.html">QNetworkProtocol</a>{    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>public:    Nntp();    virtual ~Nntp();    virtual int supportedOperations() const;protected:    virtual void operationListChildren( <a href="qnetworkoperation.html">QNetworkOperation</a> *op );    virtual void operationGet( <a href="qnetworkoperation.html">QNetworkOperation</a> *op );    <a href="qsocket.html">QSocket</a> *commandSocket;    bool connectionReady;    bool readGroups;    bool readArticle;private:    bool checkConnection( <a href="qnetworkoperation.html">QNetworkOperation</a> *op );    void close();    void parseGroups();    void parseArticle();protected slots:    void hostFound();    void connected();    void closed();    void readyRead();    void error( int );};#endif</pre><p> <hr><p> Implementation (nntp.cpp):<p> <pre>/****************************************************************************** $Id:  qt/nntp.cpp   3.0.5   edited Oct 12 2001 $**** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.**** This file is part of an example program for Qt.  This example** program may be used, distributed and modified without limitation.*******************************************************************************/#include "nntp.h"#include &lt;<a href="qurlinfo-h.html">qurlinfo.h</a>&gt;#include &lt;stdlib.h&gt;#include &lt;<a href="qurloperator-h.html">qurloperator.h</a>&gt;#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;#include &lt;<a href="qregexp-h.html">qregexp.h</a>&gt;<a name="f253"></a>Nntp::Nntp()    : <a href="qnetworkprotocol.html">QNetworkProtocol</a>(), connectionReady( FALSE ),      readGroups( FALSE ), readArticle( FALSE ){    // create the command socket and connect to its signals    commandSocket = new <a href="qsocket.html">QSocket</a>( this );<a name="x613"></a>    <a href="qobject.html#connect">connect</a>( commandSocket, SIGNAL( <a href="qsocket.html#hostFound">hostFound</a>() ),             this, SLOT( hostFound() ) );<a name="x610"></a>    <a href="qobject.html#connect">connect</a>( commandSocket, SIGNAL( <a href="qsocket.html#connected">connected</a>() ),             this, SLOT( connected() ) );<a name="x611"></a>    <a href="qobject.html#connect">connect</a>( commandSocket, SIGNAL( <a href="qsocket.html#connectionClosed">connectionClosed</a>() ),             this, SLOT( closed() ) );<a name="x616"></a>    <a href="qobject.html#connect">connect</a>( commandSocket, SIGNAL( <a href="qsocket.html#readyRead">readyRead</a>() ),             this, SLOT( readyRead() ) );<a name="x612"></a>    <a href="qobject.html#connect">connect</a>( commandSocket, SIGNAL( <a href="qsocket.html#error">error</a>( int ) ),             this, SLOT( error( int ) ) );}Nntp::~Nntp(){    close();    delete commandSocket;}<a name="x604"></a>void Nntp::<a href="qnetworkprotocol.html#operationListChildren">operationListChildren</a>( <a href="qnetworkoperation.html">QNetworkOperation</a> * ){    // create a command    <a href="qstring.html">QString</a> path = <a href="qnetworkprotocol.html#url">url</a>()-&gt;path(), cmd;<a name="x620"></a>    if ( path.<a href="qstring.html#isEmpty">isEmpty</a>() || path == "/" ) {        // if the path is empty or we are in the root dir,        // we want to read the list of available newsgroups        cmd = "list newsgroups\r\n";    } else if ( <a href="qnetworkprotocol.html#url">url</a>()-&gt;isDir() ) {        // if the path is a directory (in our case a news group)        // we want to list the articles of this group<a name="x623"></a>        path = path.<a href="qstring.html#replace">replace</a>( QRegExp( "/" ), "" );        cmd = "listgroup " + path + "\r\n";    } else        return;    // write the command to the socket<a name="x622"></a><a name="x621"></a><a name="x618"></a>    commandSocket-&gt;<a href="qsocket.html#writeBlock">writeBlock</a>( cmd.<a href="qstring.html#latin1">latin1</a>(), cmd.<a href="qstring.html#length">length</a>() );    readGroups = TRUE;}<a name="x603"></a>void Nntp::<a href="qnetworkprotocol.html#operationGet">operationGet</a>( <a href="qnetworkoperation.html">QNetworkOperation</a> *op ){    // get the dirPath of the URL (this is our news group)    // and the filename (which is the article we want to read)<a name="x601"></a>    <a href="qurl.html">QUrl</a> u( op-&gt;<a href="qnetworkoperation.html#arg">arg</a>( 0 ) );<a name="x625"></a><a name="x624"></a>    <a href="qstring.html">QString</a> dirPath = u.<a href="qurl.html#dirPath">dirPath</a>(), file = u.<a href="qurl.html#fileName">fileName</a>();    dirPath = dirPath.<a href="qstring.html#replace">replace</a>( QRegExp( "/" ), "" );    // go to the group in which the article is    <a href="qstring.html">QString</a> cmd;    cmd = "group " + dirPath + "\r\n";    commandSocket-&gt;<a href="qsocket.html#writeBlock">writeBlock</a>( cmd.<a href="qstring.html#latin1">latin1</a>(), cmd.<a href="qstring.html#length">length</a>() );    // read the head of the article    cmd = "article " + file + "\r\n";    commandSocket-&gt;<a href="qsocket.html#writeBlock">writeBlock</a>( cmd.<a href="qstring.html#latin1">latin1</a>(), cmd.<a href="qstring.html#length">length</a>() );    readArticle = TRUE;}<a name="x602"></a>bool Nntp::<a href="qnetworkprotocol.html#checkConnection">checkConnection</a>( <a href="qnetworkoperation.html">QNetworkOperation</a> * ){    // we are connected, return TRUE<a name="x599"></a>    if ( commandSocket-&gt;<a href="qiodevice.html#isOpen">isOpen</a>() &amp;&amp; connectionReady )        return TRUE;    // seems that there is no chance to connect

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?