Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 调用send()会无误地关闭应用程序_C++_Posix_Centos7 - Fatal编程技术网

C++ 调用send()会无误地关闭应用程序

C++ 调用send()会无误地关闭应用程序,c++,posix,centos7,C++,Posix,Centos7,我有一个客户端应用程序通过tcp发送数据。在某些情况下,调用send返回而没有发送所有可用字节,下一个调用send将关闭应用程序而不会出现任何错误 调用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 straigh

我有一个客户端应用程序通过tcp发送数据。在某些情况下,调用send返回而没有发送所有可用字节,下一个调用send将关闭应用程序而不会出现任何错误

调用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以发送剩余的_字节,但该调用从未返回任何异常,也未捕获任何异常,应用程序将关闭。

一种可能是,您从send收到错误返回,但没有检查-1。这可能会使发送的总字节数为负数,这将使您的第二次发送进入无效内存。

请提供一个新的地址。如果调用send返回错误,则您发布的输出不是您显示的代码。如果您没有捕获到异常,您的程序将终止,那么在这种情况下出现的一条消息就是编译器对您很好,但不是强制要求。原始源代码包含许多文件,提供一个完整且最小的示例有点不兼容。异常已得到正确处理,但我不能包含导致异常处理程序的所有源代码。在mcve complete中,意味着有足够的代码来重现问题,而不是像post中的所有代码那样完整。至少您可以发布与输出匹配的代码,而不是未更新的代码和输出。创建mcve对于调试来说是非常有用的工具。如果无法隔离问题并生成mcve,则会出现更一般的问题如果对posix send的底层调用返回-1,则m_socket.send方法会引发异常。这些异常在顶级方法中的处理程序中捕获,该方法导致m_socket.send。