c++ boost库

1.下载地址提升下载量 (boost.org)icon-default.png?t=N7T8https://www.boost.org/users/download/2.解压安装

   双击bootstrap.bat文件3.MinGW安装g++,配置环境变量

4..\b2.exe toolset=gcc编译boost

5.Visual Studio配置boost,配置包和库文件

6.写一段代码验证是否配置成功 

#include <iostream>
#include<string>
#include<boost/lexical_cast.hpp>

int main()
{
    using namespace std;
    cout << "Enter you weight:";
    float weight;
    cin >> weight;
    string gain = "A 10% increate taises";
    string wt = boost::lexical_cast<string>(weight);
    gain = gain + wt + "to";
    weight = 1.1 * weight;
    gain = gain + boost::lexical_cast<string>(weight) + ".";
    cout << gain << endl;
    system("pause");
    return 0;
}

7.c++ socket网络编程

#include "endPoint.h"
#include<boost/asio.hpp>
#include<iostream>
using namespace boost;
int client_end_point() {
	std::string raw_ip_address = "127.4.8.1";
	unsigned short port_num = 3333;
	boost::system::error_code ec;
	asio::ip::address ip_address = asio::ip::address::from_string(raw_ip_address, ec);
	if (ec.value()!=0)
	{
		std::cout << "Failed to parse IP address. Error code =" << ec.value() << ".Message is" << ec.message();
		return ec.value();
	}
	asio::ip::tcp::endpoint ep(ip_address, port_num);
	return 0;
}
int server_end_point() {
	unsigned short port_num = 3333;
	asio::ip::address ip_adress = asio::ip::address_v6::any();
	asio::ip::tcp::endpoint ep(ip_adress, port_num);
	return 0;
}
int create_tcp_socket() {
	asio::io_context ioc;
	asio::ip::tcp protocol = asio::ip::tcp::v4();
	asio::ip::tcp::socket sock(ioc);
	boost::system::error_code ec;
	sock.open(protocol, ec);
	if (ec.value() != 0)
	{
		std::cout << "Failed to open the socket. Error code =" << ec.value() << ".Message is" << ec.message();
		return ec.value();
	}
	return 0;
}
int create_acceptor_socket() {
	asio::io_context ioc;
	asio::ip::tcp::acceptor acceptor(ioc, asio::ip::tcp::endpoint(asio::ip::tcp::v4(),3333));
	return 0;
}
int connect_to_end() {
	std::string raw_ip_address = "192.168.1.124";
	unsigned short port_num = 3333;
	try
	{
		asio::ip::tcp::endpoint ep(asio::ip::address::from_string(raw_ip_address), port_num);
		asio::io_context ios;
		asio::ip::tcp::socket sock(ios, ep.protocol());
		sock.connect(ep);
	}
	catch (system::system_error& e)
	{
		std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
		return e.code().value();
	}
}
int dns_connect_to_end() {
	std::string host = "lifc.club";
	std::string port_num = "3333";
	asio::io_context ios;
	asio::ip::tcp::resolver::query resolver_query(host, port_num, asio::ip::tcp::resolver::query::numeric_service);
	asio::ip::tcp::resolver resolver(ios);
	try
	{
		asio::ip::tcp::resolver::iterator it = resolver.resolve(resolver_query);
		asio::ip::tcp::socket sock(ios);
		asio::connect(sock, it);
	}
	catch (system::system_error& e)
	{
		std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
		return e.code().value();
	}
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值