简体   繁体   English

Boost Client无法从服务器接收消息

[英]Boost Client not able to receive message from server

I am new to boost library so need some help in modifying this function to get a return message from server. 我是新来提升库的人,因此需要一些帮助来修改此函数以从服务器获取返回消息。

if i remove the second portion of the code the server is able to read message from the client. 如果我删除了代码的第二部分,则服务器能够从客户端读取消息。

But when i try to read the return message from the server(code in the second portion) the server gives error "Cannot read from socket" 但是,当我尝试从服务器(第二部分中的代码)读取返回消息时,服务器给出错误“无法从套接字读取”

void send_something(std::string host, int port, std::string message)
{
    boost::asio::io_service ios;

    boost::asio::ip::tcp::endpoint
    endpoint(boost::asio::ip::address::from_string(host), port);
    boost::asio::ip::tcp::socket socket(ios);
    socket.connect(endpoint);
    boost::array<char, 128> buf;
    std::copy(message.begin(),message.end(),buf.begin());
    boost::system::error_code error;
    socket.write_some(boost::asio::buffer(buf, message.size()), error);
 //////////////////////////////////////////////////////////////////////////// 
////////////////////////////////////////////////////////////////////////////////////////////////       
  size_t len = socket.read_some(boost::asio::buffer(buf), error);
  if (error == boost::asio::error::eof)
   {
 //   break;
    }// Connection closed cleanly by peer.
 else if (error)
 {
    throw boost::system::system_error(error); // Some other error.
 }

  std::cout.write(buf.data(), len);
 }  

Here is the code that does what you want. 这是执行您想要的代码。 Note that, I have not implemented any framing for receiving the data from the server, so it may or may not happen that you receive the complete data. 请注意,我尚未实现用于接收来自服务器的数据的任何框架,因此您可能会或可能不会接收到完整的数据。

In case you want to receive the complete message from the server, you can loop around read_some just like how it's done for write_some till your read message size is equal to what you were expecting. 如果你想从服务器接收完整的消息,您可以围绕环read_some就像它是如何对所做write_some直到你读的邮件大小等于您所期望的东西。

There are other flavours of reading as well like read_until by which you let asio collect the response until it sees the specified pattern for eg an new line('\\n' or \\r\\n). 还有其他阅读方式,例如read_until ,您可以让asio收集响应,直到它看到指定的模式(例如换行('\\ n'或\\ r \\ n))为止。

#include <iostream>
#include <array>
#include <string>
#include <system_error>
#include <asio.hpp>


void send_something(std::string host, int port, std::string message)
{
    asio::io_service ios;

    asio::ip::tcp::endpoint
    endpoint(asio::ip::address::from_string(host), port);
    asio::ip::tcp::socket socket(ios);
    socket.connect(endpoint);

    std::error_code error;
    size_t len = 0;
    // Write the complete message
    while (len < message.length()) {
      len += socket.write_some(asio::buffer(message), error);
    }

    std::array<char, 128> buf;
    socket.read_some(asio::buffer(buf), error);

    if (error == asio::error::eof)
    {
      std::cout << "Connection closed by server\n";
    }
    else if (error)
    {
      std::cout << "ERROR in connection" << std::endl;
      return;
    }

    std::cout << "Received: " << buf.data() << std::endl;
}  


int main() {
  send_something("127.0.0.1", 7869, "Hello!");
  return 0;
}

TEST:: 测试::

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket as sock
>>> sd = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
>>> sd
<socket._socketobject object at 0x1088d2c20>
>>> sd.bind(("localhost", 7869))
>>> sd.listen(2)
>>> c, a = sd.accept()
>>> c
<socket._socketobject object at 0x1088d2c90>
>>> buf = c.recv(512)
>>> buf
'Hello!'
>>>
>>> c.send("Cool!")
5

MacBook-Pro:asio_test amuralid$ g++ -g -std=c++14 -I ~/asio-master/asio/include/ -o client_read_so client_read_so.cc -pthread

MacBook-Pro:asio_test amuralid$ ./client_read_so
    Received: Cool!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM