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) 浏览(2023)
  2. C++ 类的继承封装和多态

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

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

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

    喜欢(587) 浏览(1760)

最新评论

  1. Linux环境搭建和编码 恋恋风辰:Linux环境下go的安装比较简单,可以不用设置GOPATH环境变量,后期我们学习go mod 之后就拜托了go文件目录的限制了。
  2. C++ 类的拷贝构造、赋值运算、单例模式 secondtonone1:好的,已修复。
  3. 双链表实现LRU算法 secondtonone1:双链表插入和删除节点是本篇的难点,多多练习即可。
  4. 线程安全的无锁栈 secondtonone1:谢谢支持,如果pop的次数大于push的次数是会让线程处于重试的,这个是测试用例,必须满足push和pop的次数相同,实际情况不会这么使用。栈的设计没有问题。
  5. 再谈单例模式 secondtonone1:是的,C++11以后返回局部static变量对象能保证线程安全了。

个人公众号

个人微信