boost网络编程

1.服务端建立链接

int accept_new_connection() {
	//服务端
	const int BACKLOG_SIZE = 30;
	unsigned short port_num = 3333;
	//服务器先生成端点
	asio::ip::tcp::endpoint ep(asio::ip::address_v4::any(), port_num);
	asio::io_context ios;
	try
	{
		//创建接收器
		asio::ip::tcp::acceptor acceptor(ios, ep.protocol());
		//绑定端点
		acceptor.bind(ep);
		//监听
		acceptor.listen(BACKLOG_SIZE);
		asio::ip::tcp::socket sock(ios);
		//accept返回新的链接
		acceptor.accept(sock);
	}
	catch (system::system_error& e)
	{
       std::cout << "Failed to accept_new_connection. Error code =" << e.code() << ".Message is" << e.what();
		return e.code().value();
	}
       return 0;
}

 2.客户端建立链接

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();
	}
return 0;
}

3.读写数据
 

void write_to_socket(asio::ip::tcp::socket& sock) {
	std::string buf = "Hello World";
	std::size_t total_bytes_writter = 0;
	while (total_bytes_writter!=buf.length())
	{
		total_bytes_writter += sock.write_some(asio::buffer(buf.c_str() + total_bytes_writter, 
			buf.length() - total_bytes_writter));
	}
}
int send_data_by_write_some() {
	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);
		//write_to_socket(sock);
		/*std::string buf = "Hello World";
		int send_lenth = sock.send(asio::buffer(buf.c_str(), buf.length()));
		if (send_lenth<=0)
		{
			return 0;
		}*/
		std::string buf = "Hello World";
		int send_lenth = asio::write(sock,asio::buffer(buf.c_str(),buf.length()));
		if (send_lenth <= 0)
		{
			return 0;
		}
	}
	catch (system::system_error& e)
	{
		std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
		return e.code().value();
	}
	return 0;
}
std::string read_from_socket(asio::ip::tcp::socket& sock) {
	const unsigned char MESSAGE_SIZE = 7;
	char buf[MESSAGE_SIZE];
	std::size_t total_bytes_read = 0;
	while (total_bytes_read!= MESSAGE_SIZE)
	{
		total_bytes_read += sock.read_some(asio::buffer(buf + total_bytes_read, MESSAGE_SIZE - total_bytes_read));
	}
	return std::string(buf, total_bytes_read);
}
int read_data_by_read_some() {
	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);
		//read_from_socket(sock);
		const unsigned char MESSAGE_SIZE = 7;
		char buf_receive[MESSAGE_SIZE];
		/*int send_lenth = sock.receive(asio::buffer(buf_receive, MESSAGE_SIZE));
		if (send_lenth<=0)
		{
			return -1;
		}*/
		int send_lenth = asio::read(sock, asio::buffer(buf_receive, MESSAGE_SIZE));
		if (send_lenth <= 0)
		{
			return 0;
		}
	}
	catch (system::system_error& e)
	{
		std::cout << "Failed to connect the socket. Error code =" << e.code() << ".Message is" << e.what();
		return e.code().value();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值