C++实用库Poco

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;
}




相关库:

openssl-vs2008-dll(预编译好了)


poco头文件     lib导入库(win32)    poco二进制dll(win32)



  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Poco C是一个流行的C++开发,可以帮助开发者快速构建高性能、可扩展的网络应用程序和服务。它提供了丰富的功能和工具,简化了开发过程,提高了开发效率。 Poco C有以下主要特点: 1. 网络和通信功能:Poco C提供了强大的网络和通信功能,包括HTTP、SMTP、FTP、DNS等协议的支持。开发者可以方便地创建和管理网络连接、发送和接收数据等操作。 2. 数据访问接口:Poco C提供了统一的数据访问接口,支持主流数据系统如MySQL、PostgreSQL、Oracle等。通过Poco C,开发者可以轻松进行数据操作,如查询、插入、更新等。 3. 多线程和并发:Poco C提供了强大的多线程和并发支持,可以帮助开发者设计和管理高效的多线程应用程序。它提供了线程、锁、条件变量等基本的多线程工具,并提供了各种高级的并发机制,如任务管道、线程池等。 4. 输入输出和文件系统:Poco C提供了丰富的输入输出和文件系统功能,开发者可以通过Poco C轻松地进行文件操作、流操作、日志记录等。 5. 安全性和加密:Poco C提供了各种安全性和加密功能的支持,包括SSL/TLS、SHA、MD5等。它可以帮助开发者保护数据安全,防止信息泄露。 总之,Poco C是一个功能强大且易于使用的C++开发,为开发者提供了丰富的功能和工具,以加快应用程序的开发过程,同时提高应用程序的性能和稳定性。无论是开发网络应用还是系统程序,Poco C都是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值