呼叫send()关闭申请的情况下没有错误

Calling send() closes the applicaiton without error

本文关键字:情况下 有错误 send 呼叫      更新时间:2023-10-16

我有一个客户端应用程序通过TCP发送数据。在某个时候,请致电发送()返回,而无需发送所有可用的bytes和下一个呼叫以发送申请即可发送申请。

调用send()的循环看起来像:

  // m_buf is an std::vector of size 65536
  auto total_bytes = fill_buffer(m_buf.data(),m_buf.size());
  while(total_bytes > 0){
    // m_socket is a straightforward wrapper around a socket descriptor that
    // throws if a call to send() returns an error.
    auto total_bytes_sent = m_socket.send(m_buf.data(),total_bytes);
    auto remaining_bytes  = total_bytes - total_bytes_sent;
    while(remaining_bytes > 0){
      total_bytes_sent += m_socket.send(m_buf.data()+total_bytes_sent,remaining_bytes);
      remaining_bytes   = total_bytes - total_bytes_sent;
    }
    total_bytes = fill_buffer(m_buf.data(),m_buf.size());
  }

我也有一些调试打印:

send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 65536
send: remaining_bytes 0
send: total_bytes 65536
send: total_bytes_sent 8127
send: remaining_bytes 57409
[computer@localhost test]$

在末尾,send()被召唤发送剩余的_bytes,但呼叫永远不会返回(也没有例外)关闭。

一种可能性是您从send()获得错误返回,但您没有检查-1。这可能会使total_bytes_sent成为负数,这将使您的第二个发送到无效的内存中。

相关文章: