发送和接收的数据大小不同

时间:2011-03-17 15:28:46

标签: php c++ sockets network-programming boost-asio

我在C ++中使用boost.asio和php客户端编写服务器。当我发送少量数据时,我得到了所有数据,但是当我发送长字符串时,我松开了大部分数据。

这是我从我的服务器发送数据的部分,它说我发送了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));

    }

这里我将发送我将要发送的字符串

string getMap(string name, string pass) {
            if (name == "admin" && pass == "123") {
                string response = "";
                ConvertTypes types;
                response = types.intToString(MAP_HEIGHT) + " ";
                response += types.intToString(MAP_WIDTH) + "\r\n";
                for (int i=0; i<MAP_HEIGHT;i++) {
                    for (int j=0;j<MAP_WIDTH;j++) {
                        response += types.intToString(
                                worldMap[i][j].getHeight()) + " ";
                        response += types.intToString(
                                worldMap[i][j].getIsTown()) + " ";
                        response += string (1, worldMap[i][j].getTetrain())
                                +"\r\n";
                    }
                }
                return response;
            } else {
                return "";
            }
        }

在php端我读取发送的数据,stream_get_meta_data说我只收到8183字节的数据。

print_r($this->socket->getStatus());

        for ($i=0; $i<$MAP_HEIGHT;$i++) {
            for ($j=0; $j<$MAP_WIDTH;$j++) {

                $this->response = $this->socket->readLine();
                $this->response = explode(' ', $this->response);
                echo "<p>";
                echo "$i $j <br>";
                print_r($this->response);
                echo '<br>';
                print_r($keyArray);
                $map[$i][$j] = array_combine($keyArray, $this->response);
                $this->response = $this->socket->readLine();
            } }

        }

2 个答案:

答案 0 :(得分:2)

您可以通过套接字发送一个大块,但接收方可能会获得几个较小的块,例如:

send    -> 10000 bytes

receive <- 3000 bytes
receive <- 2000 bytes
receive <- 4500 bytes
receive <-  500 bytes

这只是一个例子,TCP不保证发送和接收块的大小相同。

答案 1 :(得分:0)

我找到了答案。我是以不安全的方式从服务器发送数据。当async_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_;
};

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