异步asio udp server和client (单线程)
  MJlmRDrYd0Ow 2023年11月02日 95 0


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

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

上一篇: Z字形变换 下一篇: 一种存储表结构方法
  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
MJlmRDrYd0Ow
最新推荐 更多