poco库应用框架

Net

1. http框架

1.1 http下载文件

#include "StdAfx.h"
#include <Winsock2.h>
#include <stdio.h>
#include "Poco/URIStreamOpener.h"
#include "Poco/StreamCopier.h"
#include "Poco/Path.h"
#include "Poco/URI.h"
#include "Poco/Exception.h"
#include "Poco/Net/HTTPStreamFactory.h"
#include "Poco/Net/FTPStreamFactory.h"
#include "Poco/UUID.h"
#include "Poco/UUIDGenerator.h"
#include <memory>
#include <iostream>
#include <string>
#include <fstream>

#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;
using Poco::URIStreamOpener;
using Poco::StreamCopier;
using Poco::Path;
using Poco::URI;
using Poco::Exception;
using Poco::Net::HTTPStreamFactory;
using Poco::Net::FTPStreamFactory;
using Poco::UUID;
using Poco::UUIDGenerator;
using namespace std;
using std::ostream;

vector<std::string> vs; 
//清除每一行末尾的 多余字符
void clean_string(char *str)
{
char *start = str - 1;
char *end = str;
char *p = str;
while(*p)
{
switch(*p)
{
case ' ':
case '\r':
case '\n':
{
if(start + 1==p)
start = p;
}
break;
default:
break;
}
++p;
}
//现在来到了字符串的尾部 反向向前
--p;
++start;
if(*start == 0)
{
//已经到字符串的末尾了 
*str = 0 ;
return;
}
end = p + 1;
while(p > start)
{
 
switch(*p)
{
case ' ':
case '\r':
case '\n':
{
if(end - 1 == p)
end = p;
}
break;
default:
break;
}
--p;
}
memmove(str,start,end-start);
*(str + (int)end - (int)start) = 0;
}


//获取文件下载列表
int downList(URI uri){


try
{
//URI uri(argv[0]);
//URI uri("http://192.168.30.109:8080/logapp/downlist.txt");
//URI uri("http://192.168.55.109:8080/logapp/downlist.zip");
std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));
//StreamCopier::copyStream(*pStr.get(), std::cout);
 
char str[100]= {0};

while(!pStr.get()->eof()) {
if(pStr.get()->getline(str, 100)){

std::cout<< str << endl;
std::cout<< "2222" << endl;
//std::string strV= str;
clean_string(str);
vs.push_back(str);


}
}
 
}
catch (Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
//system("pause");
return 1;
}
return 0;
}
//下载文件
int downFile(vector<std::string> vs){
try
{
//std:string xx = "http://192.168.30.109:8080/logapp/downlist.zip";//
//string xx2 = vs[0];
//printf("%s", xx2.c_str());
//int s = strcmp(xx.c_str(), xx2.c_str());
//URI uri(xx2);
//URI uri2("http://192.168.30.109:8080/logapp/downlist.zip");
for(vector<int>::size_type i = 0; i<vs.size() ; i++){
URI uri(vs[i]);
std::string filePath = uri.getPath();
string filename = filePath.substr(8);
std::auto_ptr<std::istream> pStr(URIStreamOpener::defaultOpener().open(uri));
//ofstream ds("D:\\adfile.zip");
UUIDGenerator *uuidG = new UUIDGenerator();
UUID uuid = uuidG->createOne();
ofstream ds("D:\\"+uuid.toString()+".zip");
//FILE *file = fopen(to "wb");
if(ds!= NULL){
//fprintf( file, "Hello %s\n", *pStr.get() );
StreamCopier::copyStream(*pStr.get(),ds);
}
}

//StreamCopier::copyStream(*pStr.get(), std::cout);



/*std::string str ;
if(getline( *pStr.get(), str )){
std::cout<< str << endl;
} */
}
catch (Exception& exc)
{
std::cerr << exc.displayText() << std::endl;
//system("pause");
return 1;
}
}


int main(int argc, char** argv)
{
URI uri("http://192.168.30.109:8080/logapp/downlist.txt");
HTTPStreamFactory::registerFactory();
FTPStreamFactory::registerFactory();

if (argc != 2)
{
Path p(argv[0]);
std::cerr << "usage: " << p.getBaseName() << " <uri>" << std::endl;
std::cerr << "       Download <uri> to standard output." << std::endl;
std::cerr << "       Works with http, ftp and file URIs." << std::endl;
//system("pause");
//return 1;

downList( uri);
downFile( vs);
system("pause");
return 0;
}



2. tcpserver框架

客户端下载:

#include "StdAfx.h"
#include <Winsock2.h>
#include <stdio.h>
#include <Poco/String.h>
#include <Poco/Zip/ZipStream.h>
#include <Poco/Path.h>
#include <Poco/DateTime.h>
#include <iostream > 
#include <fstream > 
#include <Poco/Zip/Decompress.h>
#include <Poco/UnicodeConverter.h>
#include <Poco/Net/Net.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTMLForm.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/StreamCopier.h>
#include <Poco/String.h>
#include <Poco/UnicodeConverter.h>
#include <Poco/Net/SocketStream.h>
#include <windows.h> 
#include <stdio.h> 
#include "json/json.h"
#include <vector>
#include <io.h>
using Poco::trim;
using Poco::trimLeft;
using Poco::trimRight;
using Poco::trimRightInPlace;
using Poco::Zip::ZipInputStream;
using Poco::PathSyntaxException;
using std::ifstream;
using Poco::Zip::Decompress;
using Poco::UnicodeConverter;
using namespace Poco::Net;
using namespace std;
using  Poco::StreamCopier;

using Poco::Net::SocketAddress;
using Poco::Net::StreamSocket;

#define BUFFER_SIZE 1024

int UTF82GBK(const char *szUtf8,char *szGbk,int Len);
//void sendBytes(StreamSocket socket,char buffer[] );
//void sendBytes(StreamSocket socket,char buffer[] ,char buffer1[BUFFER_SIZE]);
//void readBytes(StreamSocket socket,char buffer[]);
#pragma pack (4)
 struct PInfo{
char type;
int filelength;
int  readystatus;
char filename[1024] ;
} ;

vector<std::string> getList( vector<std::string> files){
Poco::Net::SocketAddress sa("www.ssd.com",9911);
Poco::Net::StreamSocket socket(sa);
struct PInfo pi = {0,0,0,0};
socket.sendBytes((char*)&pi,sizeof(pi));
char buf[1024];
int len = socket.receiveBytes(buf, 1024);
PInfo* recvPi = (PInfo*)buf;
char* strjson = recvPi->filename;
Json::Reader reader;
Json::Value root;

if(reader.parse(strjson,root)){
 
int pathnum = root["path"].size();  // 得到"files"的数组个数  
for(int i = 0; i < pathnum; i++)  // 遍历数组  
{  
files.push_back(root["path"][i].asString());

}
}

socket.close();
return files;
}

void getFile(vector<std::string> files){

for(int i = 0; i < files.size(); i++)  // 遍历数组  
{  
 
Poco::Net::SocketAddress sa("www.ssd.com",9911);
Poco::Net::StreamSocket socket(sa);  
PInfo pi;

sprintf(pi.filename,"%s",files[i].c_str());
pi.type =  1;

pi.filelength = 0;
pi.readystatus = 0;
 
socket.sendBytes((char*)&pi,sizeof(pi));

std::string  filepath = "D:\\sample1\\"+files[i];
//std::string  filepath = "D:\\xx.zip";
const char *fpath = filepath.c_str();
FILE *fp = fopen(fpath,"wb");

if(!fp){
printf("打开文件失败!!!");
return ;
}else{
printf("文件已经打开,等待传输...\n");
}
char data[1024];
int j = 0 ;
int totallen = 0;
//int len = socket.receiveBytes(data, 1024);
// PInfo* recvPi = (PInfo*)(data);
// //std::cout << "" << pi->filelength << std::endl;
// int filelength = recvPi->filelength;
// if(filelength>0){

/*PInfo sendPi ;
sendPi.filelength = filelength;
sendPi.readystatus = 1;
sendPi.type = 1;
sprintf(sendPi.filename,"%s",files[i].c_str());
char buf[sizeof(sendPi)];
memcpy(buf, &sendPi, sizeof(buf));
socket.sendBytes(buf,sizeof(buf));*/

while(1){
memset(data,0,1024);  
int len = socket.receiveBytes(data, 1024);
fwrite(data,1,len,fp);
j++;
printf("%d\r\n", len);
totallen=totallen+len;
if(len<=0){  
fclose(fp);
break;
}
/*if(totallen==filelength){

break;
}*/

}
//}
printf("retlen=%d \n",totallen);
socket.close();  
//printf("retlen=%d i=%d\n",totallen,i);
//printf("写入的文件内容为:\n %s\n",data);
}
}
int main(){
char buffer[BUFFER_SIZE];
vector<std::string> files;
//files.push_back("71e1eaba-c894-11e4-b9c7-94de80fb7973.zip");
files = getList(files);
getFile( files);

//WSACleanup();
//system("pause");
return 0;
}


服务器:

#include "StdAfx.h"
#include "json/json.h"
#include <vector>
#include <io.h>
#include "Poco/Net/TCPServer.h"
#include "Poco/Net/TCPServerConnection.h"
#include "Poco/Net/TCPServerConnectionFactory.h"
#include "Poco/Net/TCPServerParams.h"
#include "Poco/Net/StreamSocket.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/ServerSocketImpl.h"
#include "Poco/Timestamp.h"
#include "Poco/DateTimeFormatter.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/Exception.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include <iostream>
#include <io.h>
#include <Windows.h>
#include <WinReg.h>
#include <stdio.h>
 
using Poco::Net::ServerSocket;
using Poco::Net::StreamSocket;
using Poco::Net::TCPServerConnection;
using Poco::Net::TCPServerConnectionFactory;
using Poco::Net::TCPServer;
using Poco::Timestamp;
using Poco::DateTimeFormatter;
using Poco::DateTimeFormat;
using Poco::Util::ServerApplication;
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
using namespace std;

#pragma pack (4)
struct PInfo{
char type;
int filelength;
int  readystatus;
char filename[1024];
};
 
class TimeServerConnection: public TCPServerConnection
/// This class handles all client connections.
///
/// A string with the current date and time is sent back to the client.
{
public:
TimeServerConnection(const StreamSocket& s, const std::string& format): 
TCPServerConnection(s),
_format(format)
{
}
int  GetFileSize(const char *filepath,FILE *file){
//string path = "D:\\m.zip";
//const char *fpath = path.c_str();
//FILE* file = fopen(filepath, "rb");
int size;
if (file)
{
size = filelength(fileno(file));
std::cout<<size<<std::endl;
fclose(file);
}
 
return size ;
}
void getFiles(string path,vector<string>& files){
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo))!=-1){

do  
{  
//如果是目录,迭代之  
//如果不是,加入列表  
if((fileinfo.attrib &  _A_SUBDIR))  
{  
if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)  
getFiles( p.assign(path).append("\\").append(fileinfo.name), files );  
}  
else  
{  
//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  
files.push_back(fileinfo.name);  
}  
}while(_findnext(hFile, &fileinfo)  == 0);  
_findclose(hFile); 
}
}
void sendList() {


char *filePath = "D:\\sample";

vector<std::string> files;
getFiles(filePath,files);
 
int size = files.size();
//const char* urljson ="{\"url1\":\"71e286fa-c894-11e4-b9c7-94de80fb7973.zip\"\"url1\":\"71e286fa-c894-11e4-b9c7-94de80fb7973.zip\",\"url1\":\"71e286fa-c894-11e4-b9c7-94de80fb7973.zip\"}";
//{path:[\"a.zip\", \"b.zip\"]}
//char* urljson = "{path:[";
std::string strjson = "{\"path\":[";
for(int i=0;i<size;i++){
cout<<files[i].c_str()<<endl;
strjson +="\"";
strjson += files[i].c_str();
strjson +="\"";
if(i<(size-1)){
strjson +=",";
}
}
strjson +="]}";

PInfo pi;
memset(pi.filename, 1024, 0);
pi.type = 0;
sprintf(pi.filename, "%s", strjson.c_str());
char buf[sizeof(pi)];
socket().sendBytes((char*)&pi, sizeof(pi));


return;
}


void sendFile(int n,  char buf[1024]){
if (n > 0){
PInfo* pi = (PInfo*)(buf);
std::cout << "" << socket().address().toString() << std::endl;
 
//socket().sendBytes(buf, n);
std::cout << "" << socket().peerAddress().toString() << std::endl;
std::cout << "" << pi->filelength << std::endl;
std::cout << "" << pi->readystatus << std::endl;

std::string  filepath = "D:\\sample";
std::string  path;
path.assign(filepath).append("\\").append(pi->filename);
const char *fpath = path.c_str();
//if( pi->readystatus==0){
//
// //FILE* fp = fopen(fpath, "rb");
// //int  length = GetFileSize(fpath,fp);
// //  
// //std::cout << "" << length << std::endl;
// //std::cout << "" << pi->filelength << std::endl;
// //PInfo filesizepi;
// //filesizepi.readystatus = 0;
// //filesizepi.filelength = length;
// //memset(filesizepi.filename, 0, 1024);
// //filesizepi.type = 0;
// //
// socket().sendBytes((char*)&pi,sizeof(pi));
// //socket().sendBytes((char*)&filesizepi,sizeof(PInfo));
//  
//}else 
//if( pi->readystatus==1){ 
FILE* fp = fopen(fpath, "rb");
if(!fp){

printf("error!");
return ;
}else{

printf("文件已经打开,等待传输...\n");
}
char data[1024] = {0};
int len = 0;
int i =0;

while((len=fread(data,1,1024,fp))>0){

socket().sendBytes(data, len);
i++;
printf("i=%d len=%d\n",i,len);

}
socket().close();



//}


}else{
// delete this;
return;
}
}
void run()
{
Application& app = Application::instance();
app.logger().information("Request from " + this->socket().peerAddress().toString());
try
{
/* Timestamp now;
std::string dt(DateTimeFormatter::format(now, _format));
dt.append("\r\n");
socket().sendBytes(dt.data(), (int) dt.length()); */

 int n = 0;
 char buf[10240];
//char buf[]="";



while(1){
 
  n = socket().receiveBytes(buf, 10240 );
struct PInfo* pi = (struct PInfo*)(buf);
 //std::cout << pi->filelength << pi->readystatus << std::endl;
 //获取列表;
//const char* str ="{\"uploadid\":\"1234\",\"code\":100,\"msg\":\"this is the json test\",\"files\":\"xxx.zip\"}";
if(pi->type==0){
sendList();
}else {
sendFile(n,buf);
}
  //sendFile(n,buf);


 

}

}catch (Poco::Exception& exc)
{
app.logger().log(exc);
}
}

private:
std::string _format;
};

class TimeServerConnectionFactory: public TCPServerConnectionFactory
/// A factory for TimeServerConnection.
{
public:
TimeServerConnectionFactory(const std::string& format):
_format(format)
{
}

TCPServerConnection* createConnection(const StreamSocket& socket)
{
return new TimeServerConnection(socket, _format);
}


private:
std::string _format;
};

class TimeServer: public Poco::Util::ServerApplication
 
{
public:
TimeServer(): _helpRequested(false)
{
}

~TimeServer()
{
}

protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
ServerApplication::initialize(self);
}

void uninitialize()
{
ServerApplication::uninitialize();
}

void defineOptions(OptionSet& options)
{
ServerApplication::defineOptions(options);

options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false));
}


void handleOption(const std::string& name, const std::string& value)
{
ServerApplication::handleOption(name, value);


if (name == "help")
_helpRequested = true;
}

void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A server application that serves the current date and time.");
helpFormatter.format(std::cout);
}

int main(const std::vector<std::string>& args)
{
if (_helpRequested)
{
displayHelp();
}
else
{
// get parameters from configuration file
unsigned short port = (unsigned short) config().getInt("TimeServer.port", 9911);
std::string format(config().getString("TimeServer.format", DateTimeFormat::ISO8601_FORMAT));

// set-up a server socket
ServerSocket svs(port);
// set-up a TCPServer instance
TCPServer srv(new TimeServerConnectionFactory(format), svs);
// start the TCPServer
srv.start();
// wait for CTRL-C or kill
waitForTerminationRequest();
// Stop the TCPServer
srv.stop();
}
return Application::EXIT_OK;
}

private:
bool _helpRequested;
};
int main(int argc, char** argv)
{
//写入注册表,开机自启动 
HKEY hKey; 
//找到系统的启动项 
//注意把项目支持的字符集改成支持多字符集
//选中解决方案管理器中的项目,右键->属性->配置属性->常规,在右面字符集一栏单击一下字符集编辑输入区域,在下拉框中选择“使用多字节字符集”即可 
LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; 
//打开启动项Key 
long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey); 
if(lRet == ERROR_SUCCESS) 

char pFileName[MAX_PATH] = {0}; 
//得到程序自身的全路径 
//DWORD dwRet = GetModuleFileName(NULL,  pFileName, MAX_PATH); 
DWORD dwRet = GetModuleFileNameA(NULL,  pFileName, MAX_PATH); 
//添加一个子Key,并设置值
// 下面的"getip"是应用程序名字(不加后缀.exe)
lRet = RegSetValueEx(hKey,  "tcpserverpoco", 0, REG_SZ, (BYTE *)pFileName, dwRet);


//关闭注册表 
RegCloseKey(hKey); 
if(lRet != ERROR_SUCCESS) 

//AfxMessageBox("系统参数错误,不能随系统启动"); 
system("pause");

}
TimeServer app;
return app.run(argc, argv);
}


void getFiles(string path,vector<string>& files){
long hFile = 0;
struct _finddata_t fileinfo;
string p;
if((hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo))!=-1){

do  
{  
//如果是目录,迭代之  
//如果不是,加入列表  
if((fileinfo.attrib &  _A_SUBDIR))  
{  
if(strcmp(fileinfo.name,".") != 0  &&  strcmp(fileinfo.name,"..") != 0)  
getFiles( p.assign(path).append("\\").append(fileinfo.name), files );  
}  
else  
{  
//files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  
files.push_back(fileinfo.name);  
}  
}while(_findnext(hFile, &fileinfo)  == 0);  
_findclose(hFile); 
}
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值