异步asio udp server和client (单线程)

本文介绍使用Boost.Asio库实现异步UDP服务器和客户端的通信过程。通过定义asyn_asio_udp_server和asyn_asio_udp_client类,完成消息的发送与接收。服务器监听9000端口,客户端向该端口发送Iloveyou.消息,服务器接收到消息后原样返回。代码中详细展示了异步读写操作及错误处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. asyn_asio_udp_server.hpp

#ifndef ASYN_ASIO_UDP_SERVER_HPP_
#define ASYN_ASIO_UDP_SERVER_HPP_
#include <string.h>
#include <iostream>
#include <string>
#include "boost/asio.hpp"
#include "boost/bind.hpp"
#include "boost/noncopyable.hpp"
using namespace std;
using namespace boost::asio;
using namespace boost::posix_time;
class asyn_asio_udp_server : public boost::noncopyable {
public:
	asyn_asio_udp_server() : sock_(s_ios, ip::udp::endpoint(ip::udp::v4(), 9000)) {
	}
	virtual ~asyn_asio_udp_server() = default;
public:
	void start() {
		do_read();
		s_ios.run();
	}
private:
	void on_read(const boost::system::error_code &err, size_t bytes) {
		if (!err) {
			string msg(read_buf_, bytes);
			do_write(msg);
		}
	}
	void do_read() {
		sock_.async_receive_from(buffer(read_buf_), sender_end_point_, boost::bind(&asyn_asio_udp_server::on_read, this, _1, _2));
	}
	void on_write(const boost::system::error_code &err, size_t bytes) {
		do_read();
	}
	void do_write(const string &msg) {
		copy(begin(msg), end(msg), write_buf_);
		sock_.async_send_to(buffer(write_buf_, msg.size()), sender_end_point_, boost::bind(&asyn_asio_udp_server::on_write, this, _1, _2));
	}
private:
	static const size_t s_buff_size = 1024;
private:
	char write_buf_[s_buff_size];
	char read_buf_[s_buff_size];
	ip::udp::endpoint sender_end_point_;
	ip::udp::socket sock_;
public:
	static io_service s_ios;
};
io_service asyn_asio_udp_server::s_ios;
#endif /* ASYN_ASIO_UDP_SERVER_HPP_ */

2.asyn_asio_udp_client.hpp

#ifndef ASYN_ASIO_UDP_SERVER_HPP_
#define ASYN_ASIO_UDP_SERVER_HPP_
#include <string.h>
#include <iostream>
#include <string>
#include "boost/asio.hpp"
#include "boost/bind.hpp"
#include "boost/noncopyable.hpp"
using namespace std;
using namespace boost::asio;
using namespace boost::posix_time;
static const ip::udp::endpoint s_end_point(ip::address::from_string("127.0.0.1"), 9000);
class asyn_asio_udp_client : public boost::noncopyable {
public:
	asyn_asio_udp_client(const string &msg) : sock_(s_ios, ip::udp::endpoint(ip::udp::v4(), 0)), start_(false), message_(msg) {
	}
	virtual ~asyn_asio_udp_client() = default;
public:
	void start() {
		start_ = true;
		do_write(message_);
		s_ios.run();
	}
private:
	void on_read(const boost::system::error_code &err, size_t bytes) {
		if (!err) {
			string msg(read_buf_, bytes);
			cout << "client recv = " << msg << endl;
		}
	}
	void do_read() {
		sock_.async_receive_from(buffer(read_buf_), sender_end_point_, boost::bind(&asyn_asio_udp_client::on_read, this, _1, _2));
	}
	void on_write(const boost::system::error_code &err, size_t bytes) {
		do_read();
	}
	void do_write(const string &msg) {
		copy(begin(msg), end(msg), write_buf_);
		sock_.async_send_to(buffer(write_buf_, msg.size()), s_end_point, boost::bind(&asyn_asio_udp_client::on_write, this, _1, _2));
	}
private:
	static const size_t s_buff_size = 1024;
private:
	ip::udp::socket sock_;
	ip::udp::endpoint sender_end_point_;
	char read_buf_[s_buff_size];
	char write_buf_[s_buff_size];
	bool start_;
	string message_;
public:
	static io_service s_ios;
};
io_service asyn_asio_udp_client::s_ios;
#endif /* ASYN_ASIO_UDP_SERVER_HPP_ */

3.server.cpp

#include "asyn_asio_udp_server.hpp"
int main() {
	asyn_asio_udp_server server;
	server.start();

	return 0;
}

4.client.cpp

#include "asyn_asio_udp_client.hpp"
int main() {
	asyn_asio_udp_client client("I love you.");
	client.start();

	return 0;
}

5.make.sh

g++ -std=c++14 -g -o ServerTest asyn_asio_udp_server.hpp server.cpp -I /opt/boost_1_69_0 -pthread
g++ -std=c++14 -g -o ClientTest asyn_asio_udp_client.hpp client.cpp -I /opt/boost_1_69_0 -pthread

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值