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. C++ 类的继承封装和多态

    喜欢(588) 浏览(1372)
  2. Linux环境搭建和编码

    喜欢(594) 浏览(2372)
  3. 解密定时器的实现细节

    喜欢(566) 浏览(1165)
  4. windows环境搭建和vscode配置

    喜欢(587) 浏览(1323)
  5. slice介绍和使用

    喜欢(521) 浏览(1329)

最新评论

  1. C++ 类的拷贝构造、赋值运算、单例模式 secondtonone1:本文实现了线程安全的单例模式,介绍了拷贝构造和拷贝赋值的区别和联系,以及如何构造单例类,对于通用单例类如何构造可以使用模板,这个之后的章节回来介绍
  2. 堆排序 secondtonone1:堆排序非常实用,定时器就是这个原理制作的。
  3. 解决博客回复区被脚本注入的问题 secondtonone1:走到现在我忽然明白一个道理,无论工作也好生活也罢,最重要的是开心,即使一份安稳的工作不能给我带来事业上的积累也要合理的舍弃,所以我还是想去做喜欢的方向。
  4. 双链表实现LRU算法 secondtonone1:双链表插入和删除节点是本篇的难点,多多练习即可。
  5. golang 函数介绍 secondtonone1:函数是go中的一等公民,作为新兴语言,go摒弃了面向对象的一些糟粕,采取接口方式编程,而接口方式编程都是基于函数的,参数为interface,进而达到泛型作用,比如sort排序,只需要传入的参数满足sort所需interface的规定即可,需实现Len, Swap, Less三个方法,只要实现了这三个方法都可以用来做sort排序的参数。

个人公众号

个人微信