boost asio库的学习

C++网络库 boost asio库 使用详解

asio网络模块io_context 类,它相当于程序与系统之间IO操作的中介,所有收发数据操作,都是通过提交给这个类。

buffer函数

struct{
    void* buf;
    s_size len;
}

这个函数作用就是构造结构体

boost::asioio_context,buffer
boost::asio::ipaddress (地址处理类,常用他的静态函数from_string,负责将十进制ip地址转化为网络字节序)
boost::asio::ip::tcpsocket,acceptor,endpoint (endpoint获取对方ip与端口,acceptor类负责收发数据)
boost::asio::ip::udpsocket,endpoint

TCP 实例

服务端(同步)

#include<iostream>
#include"boost/asio.hpp"
using namespace std;
using namespace boost;
using asio::ip::tcp;

int main(){
    asio::io_context io;  
    tcp::acceptor acptr(io,tcp::endpoint(tcp::v4(),6688));  //accpetor类就封装了一个监听socket,通过endpoint确定监听地址与端口。
    
    tcp::socket sock(io);
    acptr.accept(sock);
    cout<<"client:"<< sock.remote_endpoint().address()<<endl;  //remote_endpoint函数,获取客户端连接上来的终端,再调用address获取地址信息。
    try{
        while(true){
            char buf[0xFF];
            sock.receive(asio::buffer(buf));
            sock.send(asio::buffer(buf));
        }
    }
    catch(std::exception&e){
        cout<< e.what();
    }
    sock.close();
    :: system("pause");
}

客户端

#include<iostream>
#include"boost/asio.hpp"
using namespace std;
using namespace boost::asio;
int main(){
    io_context io;  //io_context 实例化
    
    ip::tcp::socket sock(io);   //服务端套接字
    
    sock.connect(ip::tcp::endpoint(ip::address::from_string("127.0.0.1"),688));  //from_string 把数字转化为网络字节序。
    
    char buf[0xFF];
    while(true){
        cin>>buf;
        sock.send(buffer(buf));
        memset(buf,0,sizeof(buf));
        sock.receive(buffer(buf));
        cout<<buf<<endl;
    }
    sock.close();
    ::system("pause");
}

服务端(异步)

#include<iostream>
#include"boost/asio.hpp"
#include"boost/bind.hpp"
using namespace std;
using namespace boost;
using asio::ip::tcp;
void sock_accept(tcp::socket* sockCli);
void sock_Recv(char* buf, tcp::socket *sockCli);
void sock_Send(char* buf, tcp::socket* sockCli);

int main() {
	cout << "server start ……" << endl;
	asio::io_context io;
	tcp::acceptor acptr(io, tcp::endpoint(tcp::v4(), 6688));

	tcp::socket *sock=new tcp::socket(io);
	acptr.async_accept(*sock, boost::bind(sock_accept, sock));  //异步连接 async_accept,bind函数,将要调用的函数和传入函数参数(对端sock传入)。
    //异步:不会卡在async_accept这里,它仅仅只是提交一个接受客户端连接的请求,等待系统执行完成后,调用对应的处理函数就行了。

	io.run();
}


void sock_Send(char* buf, tcp::socket* sockCli) {
	try {
		sockCli->async_receive(asio::buffer(buf, 0xFF), boost::bind(sock_Recv, buf, sockCli));
	}
	catch (std::exception& e) {
		cout << "";
		cout << e.what();
		delete sockCli;
		delete[] buf;
	}
}

void sock_accept(tcp::socket* sockCli) {
	char* buf = new char[0xFF];
	cout << "client ip:" << sockCli->remote_endpoint().address() << endl;
	sockCli->async_receive(asio::buffer(buf, 0xFF), boost::bind(&sock_Recv, buf, sockCli));
}

void sock_Recv(char* buf, tcp::socket* sockCli) {
	try {
		sockCli->async_send(asio::buffer(buf, 0xFF), boost::bind(sock_Send, buf, sockCli));
	}
	catch (std::exception& e) {
		cout << "";
		cout << e.what();
		delete sockCli;
		delete[] buf;
	}
}

UDP编程实例

服务器(同步)

#include<iostream>
#include"boost/asio.hpp"
#include"boost/bind.hpp"
using namespace std;
using namespace boost;
using asio::ip::udp;

int main() {
	cout << "server start ……" << endl;
	asio::io_context io;
	
	udp::socket sock(io, udp::endpoint(udp::v4(), 6688));  //多了一个要监听的端口

	char buf[0xFF];
	udp::endpoint cliPoint;
	while (1) {
		sock.receive_from(asio::buffer(buf), cliPoint);
		sock.send_to(asio::buffer(buf),cliPoint);
	}
}

客户端

#include<iostream>
#include"boost/asio.hpp"
using namespace std;
using namespace boost;
using boost::asio::ip::udp;


int main() {
	asio::io_context io;


	udp::socket sock(io);
	sock.open(asio::ip::udp::v4());  //open函数,使其使用IPv4
	udp::endpoint serPoint(asio::ip::address::from_string("127.0.0.1"),6688);

	while (1) {
		char buf[0xFF];
		cin >> buf;
		sock.send_to(asio::buffer(buf), serPoint);
		memset(buf, 0, 0xFF);
		sock.receive_from(asio::buffer(buf), serPoint);
		cout << buf << endl;
	}
	::system("pause");
}

服务器(异步)

#include<iostream>
#include"boost/asio.hpp"
#include"boost/bind.hpp"
using namespace std;
using namespace boost;
using asio::ip::udp;

void sock_recv(char* buf, udp::socket* sock, udp::endpoint* cliPoint);
void sock_send(char* buf, udp::socket* sock, udp::endpoint* cliPoint);


int main() {
	cout << "server start ……" << endl;
	asio::io_context io;
	
	udp::socket *sock=new udp::socket(io, udp::endpoint(udp::v4(), 6688));

	char *buf=new char[0xFF];
	udp::endpoint *cliPoint=new udp::endpoint;

	sock->async_receive_from(asio::buffer(buf,0xFF),*cliPoint,boost::bind(sock_recv,buf,sock,cliPoint));

	io.run();

}


void sock_send(char* buf, udp::socket* sock, udp::endpoint* cliPoint) {
	try
	{
		sock->async_receive_from(asio::buffer(buf, 0xFF), *cliPoint, boost::bind(sock_recv, buf, sock, cliPoint));
	}
	catch (const std::exception& e)
	{
		cout << e.what();
	}
	
}

void sock_recv(char* buf, udp::socket* sock, udp::endpoint* cliPoint) {
	try
	{
		sock->async_send_to(asio::buffer(buf, 0xFF), *cliPoint, boost::bind(sock_send, buf, sock, cliPoint));

	}
	catch (const std::exception& e)
	{
		cout << e.what();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值