stl和boost给程序员提供了基础的语言和数据结构方面的支持。一些操作系统和网络方面的操作很少封装,Poco库可以提供这些功能。
优点:1,poco跨平台;2,文档丰富;3,对象结构清晰
类似java库索引一样的在线文档
windows上的编译:
修改buildwin.cmd设置openssl路径,笔者编译的这个库之依赖了一下openssl。
其他的,比如data{mysql,sqlite,odbc}等没有涉及,可以在compents文件里面删掉相应的4行。
rem Change OPENSSL_DIR to match your setup
set OPENSSL_DIR=C:\workspace\3rdparty\openssl-vc2008
set OPENSSL_INCLUDE=%OPENSSL_DIR%\include
set OPENSSL_LIB=%OPENSSL_DIR%\lib
set INCLUDE=%INCLUDE%;%OPENSSL_INCLUDE%
set LIB=%LIB%;%OPENSSL_LIB%
双击build_vs90.cmd开始编译,默认生成动态的发行库和调试库(d)。生成的二进制文件在bin目录和lib目录下,可是没有头文件?
笔者写了一个小的批处理提取所有的include出来,在build_vs90.cmd目录下双击执行可以在上层目录(避免冲突)找到PocoHead头文件。
@echo off
cd /d %~dp0
echo Current Path is: %cd%
set POCO_HEAD=..\PocoHead
if not exist %POCO_HEAD% (
md %POCO_HEAD%
) else (
echo %POCO_HEAD% already exist.
)
forfiles /s /m include /c "%comspec% /c echo @relpath && cd /d %~dp0 && xcopy /Y /e /s @path %POCO_HEAD%"
pause
@echo on
forfiles下载
操作文件:
网络通信:
线程互斥:
加解密:
一个udp收发例子:
// testlib.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Poco/HashFunction.h>
#include <Poco/Net/DatagramSocket.h>
#include <Poco/Thread.h>
#include <Poco/ThreadLocal.h>
#include <Poco/RWLock.h>
#include <Poco/Crypto/Crypto.h>
class SendRunnable : public Poco::Runnable
{
public:
SendRunnable(const Poco::Net::DatagramSocket & sender);
~SendRunnable();
public:
virtual void run();
private:
Poco::Net::DatagramSocket m_UdpSender;
};
SendRunnable::SendRunnable(const Poco::Net::DatagramSocket & sender)
: m_UdpSender(sender)
{
}
SendRunnable::~SendRunnable(){}
void SendRunnable::run()
{
int i = 0;
while(1)
{
char buf [100] = "";
sprintf(buf, "hello, receiver. seq: %d", i ++);
int buflen = strlen(buf) + 1; // to append to '\0'
Poco::Net::SocketAddress addr("127.0.0.1:4321");
int nr = m_UdpSender.sendTo(buf, buflen, addr, 0);
if (nr > 0)
printf("Send [%d] bytes msg [%s] to %s\r\n", nr, buf, addr.toString().c_str());
Poco::Thread::sleep(20);
}
return ;
}
class RecvRunnable : public Poco::Runnable
{
public:
RecvRunnable(const Poco::Net::DatagramSocket & receiver);
~RecvRunnable();
public:
virtual void run();
protected:
Poco::Net::DatagramSocket m_UdpReceiver;
};
RecvRunnable::RecvRunnable(const Poco::Net::DatagramSocket & receiver)
: m_UdpReceiver(receiver)
{
}
RecvRunnable::~RecvRunnable(){}
void RecvRunnable::run()
{
while(1)
{
char buf [100]; int buflen = 100;
Poco::Net::SocketAddress addr;
int nr = m_UdpReceiver.receiveFrom(buf, buflen, addr, 0);
if (nr > 0)
printf("Recv [%d] bytes msg [%s] from %s\r\n", nr, buf, addr.toString().c_str());
Poco::Thread::sleep(2000);
}
return ;
}
void target(void * arg)
{
while(1)
{
Poco::Thread::sleep(1000);
printf("%s(%d)\r\n", __FUNCTION__, __LINE__);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Poco::Net::DatagramSocket sender(
Poco::Net::SocketAddress("0.0.0.0:1234")
, true);
Poco::Net::DatagramSocket receiver(
Poco::Net::SocketAddress("0.0.0.0:4321")
, true);
// 用Runnable方式启动线程
RecvRunnable runner_rx(receiver);
SendRunnable runner_tx(sender);
Poco::Thread thread_tx;
Poco::Thread thread_rx;
thread_tx.start(runner_tx);
thread_rx.start(runner_rx);
// 用callable方式启动一个线程更简单
Poco::Thread * t1 = new Poco::Thread();
t1->start(target, NULL);
if (t1->isRunning())
printf("target is running.\r\n");
delete t1;
thread_tx.join();
return 0;
}
相关库:
poco头文件 lib导入库(win32) poco二进制dll(win32)