boost::asio 学习草稿

时间:2022-09-08 22:12:27

 

http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/


可以多个线程拥有io_service service; service.run();
但是不能一个线程运行多个service.run();


Boost.Asio c++ 网络编程翻译(1)

http://blog.csdn.net/mmoaay/article/details/39371939

 

// Server.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <windows.h>

using namespace boost::asio;

const size_t defaultLoopTime = 10000;
const size_t defaultSendLength = 1024 * 10 * 50;
const short int defaultPort = 8001;

/**************************************************************
技术博客
http://www.cnblogs.com/itdef/

技术交流群
群号码:324164944

欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/

int main()
{
	io_service service;
	ip::tcp::acceptor acceptor(service, ip::tcp::endpoint(ip::tcp::v4(), defaultPort));

	boost::shared_ptr<char> msgPtr(new  char[defaultSendLength]);
	for (size_t i = 0; i < defaultSendLength; i++)
	{
		msgPtr.get()[i] = "0123456789abcdef"[i % 16];
	}
	ip::tcp::socket sock(service);
	acceptor.accept(sock);

	try {
		for (int i = 0; i < defaultLoopTime; i++)
		{
			boost::system::error_code  ec;
			size_t bytes = write(sock, buffer(msgPtr.get(), defaultSendLength), ec);
			assert(bytes == defaultSendLength);
			if (ec == boost::asio::error::eof)
				break; // Connection closed cleanly by peer.
			else if (ec)
				throw boost::system::system_error(ec); // Some other error.
		}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}
	
	

	system("pause");
    return 0;
}

  

// Client.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include <iostream>

#include <windows.h>

/**************************************************************
技术博客
http://www.cnblogs.com/itdef/

技术交流群
群号码:324164944

欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
**************************************************************/

using namespace boost::asio;
using namespace std;

const size_t defaultLoopTime = 10000;
const size_t defaultSendLength = 1024 * 10 * 50;
const short int defaultPort = 8001;


int main()
{
	io_service service;
	ip::tcp::endpoint ep(ip::address::from_string("192.168.3.79"), defaultPort);
	ip::tcp::socket sock(service);
	sock.connect(ep);
	time_t timer1;
	time(&timer1);
	
	try {
		for (size_t i = 0; i < defaultLoopTime; i++)
		{
			boost::shared_ptr<char> msgPtr(new  char[defaultSendLength]);
			boost::system::error_code  ec;
			size_t bytes = read(sock, buffer(msgPtr.get(), defaultSendLength), ec);
			assert(bytes == defaultSendLength);
			if (ec == boost::asio::error::eof)
				break; // Connection closed cleanly by peer.
			else if (ec)
				throw boost::system::system_error(ec); // Some other error.
		}
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
		return -1;
	}

	time_t timer2;
	time(&timer2);
	double elapsed =  difftime(timer2, timer1);
	std::cout << "time elapsed: "<< elapsed << "seconds." << std::endl;
	double total_mb = 1.0 * defaultSendLength*defaultLoopTime / 1024 / 1024;

	std::cout << total_mb/ elapsed << "Mb/s." << std::endl;
	
	system("pause");
    return 0;
}