asio socket同步读写

同步写write_some

boost::asio提供了几种同步写的api,write_some可以每次向指定的空间写入固定的字节数,如果写缓冲区满了,就只写一部分,返回写入的字节数。

  1. void wirte_to_socket(asio::ip::tcp::socket& sock) {
  2. std::string buf = "Hello World!";
  3. std::size_t total_bytes_written = 0;
  4. //循环发送
  5. //write_some返回每次写入的字节数
  6. //total_bytes_written是已经发送的字节数。
  7. //每次发送buf.length()- total_bytes_written)字节数据
  8. while (total_bytes_written != buf.length()) {
  9. total_bytes_written += sock.write_some(
  10. asio::buffer(buf.c_str()+total_bytes_written,
  11. buf.length()- total_bytes_written));
  12. }
  13. }

同步写send

write_some使用起来比较麻烦,需要多次调用,asio提供了send函数。send函数会一次性将buffer中的内容发送给对端,如果有部分字节因为发送缓冲区满无法发送,则阻塞等待,直到发送缓冲区可用,则继续发送完成。

  1. int send_data_by_send(){
  2. std::string raw_ip_address = "127.0.0.1";
  3. unsigned short port_num = 3333;
  4. try {
  5. asio::ip::tcp::endpoint
  6. ep(asio::ip::address::from_string(raw_ip_address),
  7. port_num);
  8. asio::io_service ios;
  9. // Step 1. Allocating and opening the socket.
  10. asio::ip::tcp::socket sock(ios, ep.protocol());
  11. sock.connect(ep);
  12. std::string buf = "Hello World!";
  13. int send_length = sock.send(asio::buffer(buf.c_str(), buf.length()));
  14. if (send_length <= 0) {
  15. cout << "send failed" << endl;
  16. return 0;
  17. }
  18. }
  19. catch (system::system_error& e) {
  20. std::cout << "Error occured! Error code = " << e.code()
  21. << ". Message: " << e.what();
  22. return e.code().value();
  23. }
  24. return 0;
  25. }

同步写write

类似send方法,asio还提供了一个write函数,可以一次性将所有数据发送给对端,如果发送缓冲区满了则阻塞,直到发送缓冲区可用,将数据发送完成。

  1. int send_data_by_wirte() {
  2. std::string raw_ip_address = "127.0.0.1";
  3. unsigned short port_num = 3333;
  4. try {
  5. asio::ip::tcp::endpoint
  6. ep(asio::ip::address::from_string(raw_ip_address),
  7. port_num);
  8. asio::io_service ios;
  9. // Step 1. Allocating and opening the socket.
  10. asio::ip::tcp::socket sock(ios, ep.protocol());
  11. sock.connect(ep);
  12. std::string buf = "Hello World!";
  13. int send_length = asio::write(sock, asio::buffer(buf.c_str(), buf.length()));
  14. if (send_length <= 0) {
  15. cout << "send failed" << endl;
  16. return 0;
  17. }
  18. }
  19. catch (system::system_error& e) {
  20. std::cout << "Error occured! Error code = " << e.code()
  21. << ". Message: " << e.what();
  22. return e.code().value();
  23. }
  24. return 0;
  25. }

同步读read_some

同步读和同步写类似,提供了读取指定字节数的接口read_some

  1. std::string read_from_socket(asio::ip::tcp::socket& sock) {
  2. const unsigned char MESSAGE_SIZE = 7;
  3. char buf[MESSAGE_SIZE];
  4. std::size_t total_bytes_read = 0;
  5. while (total_bytes_read != MESSAGE_SIZE) {
  6. total_bytes_read += sock.read_some(
  7. asio::buffer(buf + total_bytes_read,
  8. MESSAGE_SIZE - total_bytes_read));
  9. }
  10. return std::string(buf, total_bytes_read);
  11. }
  12. int read_data_by_read_some() {
  13. std::string raw_ip_address = "127.0.0.1";
  14. unsigned short port_num = 3333;
  15. try {
  16. asio::ip::tcp::endpoint
  17. ep(asio::ip::address::from_string(raw_ip_address),
  18. port_num);
  19. asio::io_service ios;
  20. asio::ip::tcp::socket sock(ios, ep.protocol());
  21. sock.connect(ep);
  22. read_from_socket(sock);
  23. }
  24. catch (system::system_error& e) {
  25. std::cout << "Error occured! Error code = " << e.code()
  26. << ". Message: " << e.what();
  27. return e.code().value();
  28. }
  29. return 0;
  30. }

同步读receive

可以一次性同步接收对方发送的数据

  1. int read_data_by_receive() {
  2. std::string raw_ip_address = "127.0.0.1";
  3. unsigned short port_num = 3333;
  4. try {
  5. asio::ip::tcp::endpoint
  6. ep(asio::ip::address::from_string(raw_ip_address),
  7. port_num);
  8. asio::io_service ios;
  9. asio::ip::tcp::socket sock(ios, ep.protocol());
  10. sock.connect(ep);
  11. const unsigned char BUFF_SIZE = 7;
  12. char buffer_receive[BUFF_SIZE];
  13. int receive_length = sock.receive(asio::buffer(buffer_receive, BUFF_SIZE));
  14. if (receive_length <= 0) {
  15. cout << "receive failed" << endl;
  16. }
  17. }
  18. catch (system::system_error& e) {
  19. std::cout << "Error occured! Error code = " << e.code()
  20. << ". Message: " << e.what();
  21. return e.code().value();
  22. }
  23. return 0;
  24. }

同步读read

可以一次性同步读取对方发送的数据

  1. int read_data_by_read() {
  2. std::string raw_ip_address = "127.0.0.1";
  3. unsigned short port_num = 3333;
  4. try {
  5. asio::ip::tcp::endpoint
  6. ep(asio::ip::address::from_string(raw_ip_address),
  7. port_num);
  8. asio::io_service ios;
  9. asio::ip::tcp::socket sock(ios, ep.protocol());
  10. sock.connect(ep);
  11. const unsigned char BUFF_SIZE = 7;
  12. char buffer_receive[BUFF_SIZE];
  13. int receive_length = asio::read(sock, asio::buffer(buffer_receive, BUFF_SIZE));
  14. if (receive_length <= 0) {
  15. cout << "receive failed" << endl;
  16. }
  17. }
  18. catch (system::system_error& e) {
  19. std::cout << "Error occured! Error code = " << e.code()
  20. << ". Message: " << e.what();
  21. return e.code().value();
  22. }
  23. return 0;
  24. }

读取直到指定字符

我们可以一直读取,直到读取指定字符结束

  1. std::string read_data_by_until(asio::ip::tcp::socket& sock) {
  2. asio::streambuf buf;
  3. // Synchronously read data from the socket until
  4. // '\n' symbol is encountered.
  5. asio::read_until(sock, buf, '\n');
  6. std::string message;
  7. // Because buffer 'buf' may contain some other data
  8. // after '\n' symbol, we have to parse the buffer and
  9. // extract only symbols before the delimiter.
  10. std::istream input_stream(&buf);
  11. std::getline(input_stream, message);
  12. return message;
  13. }

源码链接

https://gitee.com/secondtonone1/boostasio-learn

热门评论

热门文章

  1. 解密定时器的实现细节

    喜欢(566) 浏览(1822)
  2. C++ 类的继承封装和多态

    喜欢(588) 浏览(2536)
  3. slice介绍和使用

    喜欢(521) 浏览(1697)
  4. Linux环境搭建和编码

    喜欢(594) 浏览(5445)
  5. windows环境搭建和vscode配置

    喜欢(587) 浏览(1708)

最新评论

  1. 基于锁实现线程安全队列和栈容器 secondtonone1:我是博主,你认真学习的样子的很可爱,哈哈,我画的是链表由空变成1个的情况。其余情况和你思考的类似,只不过我用了一个无效节点表示tail的指向,最初head和tail指向的都是这个节点。
  2. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。
  3. 利用内存模型优化无锁栈 卡西莫多的礼物:感谢博主指点,好人一生平安o(* ̄▽ ̄*)ブ
  4. 类和对象 陈宇航:支持!!!!
  5. 泛型算法的定制操作 secondtonone1:lambda和bind是C11新增的利器,善于利用这两个机制可以极大地提升编程安全性和效率。

个人公众号

个人微信