发送和接收的数据不为';大小不一样 我有服务器,用Boosi.aso和PHP客户端编写C++。当我发送少量数据时,我得到了所有的数据,但当我发送长字符串时,我丢失了大部分数据

发送和接收的数据不为';大小不一样 我有服务器,用Boosi.aso和PHP客户端编写C++。当我发送少量数据时,我得到了所有的数据,但当我发送长字符串时,我丢失了大部分数据,php,c++,sockets,network-programming,boost-asio,Php,C++,Sockets,Network Programming,Boost Asio,这是我从服务器发送数据的部分,它说我已经发送了65536字节 void handle_write(const boost::system::error_code& /*error*/, size_t size/*bytes_transferred*/) { cout <<size<<endl; } void handler_read(const boost::system::error_code&,

这是我从服务器发送数据的部分,它说我已经发送了65536字节

void handle_write(const boost::system::error_code& /*error*/,
            size_t size/*bytes_transferred*/) {
        cout <<size<<endl;
    }
    void handler_read(const boost::system::error_code&, std::size_t size) {
        istream is(&buffer);
        string myString;
        getline(is, myString);

        Manager myManager(myString);
        string response = myManager.getResponse();
        boost::asio::async_write(socket_,
                boost::asio::buffer(response),
                boost::bind(&tcp_connection::handle_write, shared_from_this(),
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));

    }
void handle\u write(const boost::system::error\u code&/*error*/,,
大小\u t大小/*字节\u传输*/){
cout反应);
回声“
”; 打印(键盘阵列); $map[$i][$j]=array\u combine($keyArray,$this->response); $this->response=$this->socket->readLine(); } } }
您可以通过套接字发送一个较大的数据块,但接收端可能会收到几个较小的数据块,例如:

send    -> 10000 bytes

receive <- 3000 bytes
receive <- 2000 bytes
receive <- 4500 bytes
receive <-  500 bytes
send->10000字节

我找到了答案。我正在以不安全的方式从服务器发送数据。当
async\u write
将控制权交给其他对象时,其余数据丢失

您必须将字符串传递给此类:

class shared_const_buffer {
public:
  // Construct from a std::string.
  explicit shared_const_buffer(const std::string& data)
    : data_(new std::vector<char>(data.begin(), data.end())),
      buffer_(boost::asio::buffer(*data_))
  {
  }

  // Implement the ConstBufferSequence requirements.
  typedef boost::asio::const_buffer value_type;
  typedef const boost::asio::const_buffer* const_iterator;
  const boost::asio::const_buffer* begin() const { return &buffer_; }
  const boost::asio::const_buffer* end() const { return &buffer_ + 1; }

private:
  boost::shared_ptr<std::vector<char> > data_;
  boost::asio::const_buffer buffer_;
};
类共享常量缓冲区{
公众:
//从std::字符串构造。
显式共享常量缓冲区(常量标准::字符串和数据)
:data(新std::vector(data.begin(),data.end()),
缓冲区(boost::asio::buffer(*数据)
{
}
//执行顺序要求。
typedef boost::asio::const_buffer value_type;
typedef const boost::asio::const_buffer*const_迭代器;
const boost::asio::const_buffer*begin()const{return&buffer}
const boost::asio::const_buffer*end()const{return&buffer_u+1;}
私人:
boost::共享的ptr数据;
boost::asio::const_buffer_;
};

并发送此缓冲区而不是原始字符串。这样你就不会丢失数据。

我不懂PHP,但是不能保证网络数据包都会在一个块中发送,如果你稍等片刻,然后再次尝试读取,是否有更多信息可用?+1服务器端对
send
的一次调用并不等于客户端对
recv
的一次调用。这是一个需要理解的重要概念。
send    -> 10000 bytes

receive <- 3000 bytes
receive <- 2000 bytes
receive <- 4500 bytes
receive <-  500 bytes
class shared_const_buffer {
public:
  // Construct from a std::string.
  explicit shared_const_buffer(const std::string& data)
    : data_(new std::vector<char>(data.begin(), data.end())),
      buffer_(boost::asio::buffer(*data_))
  {
  }

  // Implement the ConstBufferSequence requirements.
  typedef boost::asio::const_buffer value_type;
  typedef const boost::asio::const_buffer* const_iterator;
  const boost::asio::const_buffer* begin() const { return &buffer_; }
  const boost::asio::const_buffer* end() const { return &buffer_ + 1; }

private:
  boost::shared_ptr<std::vector<char> > data_;
  boost::asio::const_buffer buffer_;
};