首页 > 代码库 > Poco库网络模块例子解析1-------字典查询

Poco库网络模块例子解析1-------字典查询

Poco的网络模块在Poco::Net名字空间下定义   下面是字典例子解析

#include "Poco/Net/StreamSocket.h"   //流式套接字#include "Poco/Net/SocketStream.h"   //套接字流#include "Poco/Net/SocketAddress.h"   //套接字地址#include "Poco/StreamCopier.h"  //流复制器#include "Poco/Path.h"   //路径解析器#include "Poco/Exception.h"  //异常#include <iostream>using Poco::Net::StreamSocket;using Poco::Net::SocketStream;using Poco::Net::SocketAddress;using Poco::StreamCopier;using Poco::Path;using Poco::Exception;//从dict.org查找词语int main(int argc, char** argv){    const std::string HOST("dict.org");    //字典查询服务器    const unsigned short PORT = 2628; //端口        if (argc != 2)    {        Path p(argv[0]); //解析路径        std::cout << "usage: " << p.getBaseName() << " <term>" << std::endl;  //获取BaseUrl        std::cout << "       looks up <term> in dict.org and prints the results" << std::endl;        return 1;    }    std::string term(argv[1]); //要查询的单词 作为 运行参数1        try    {        SocketAddress sa(HOST, PORT); //定义一个套接字 地址对象  类似winsock的 sockaddr_in        StreamSocket sock(sa);  //创建 流式套接字        SocketStream str(sock); //创建套接字流用于进行 输入输出 类似于winsock recv send                str << "DEFINE ! " << term << "\r\n" << std::flush;  //发送查询命令        str << "QUIT\r\n" << std::flush;  //查询命令结束        sock.shutdownSend(); //关闭发送方的链接
/// Writes all bytes readable from istr to ostr, using an internal buffer.
        /// Returns the number of bytes copied. StreamCopier::copyStream(str, std::cout); //拷贝所有输入流到 输出流上 也就是说把 查询返回的结果 输出到std::cout }
catch (Exception& exc) { std::cerr << exc.displayText() << std::endl; //异常处理 return 1; } return 0;}

 

Poco库网络模块例子解析1-------字典查询