C++ Socket发送大消息未完成:仅发143080字节后失败
Socket发送截断问题
我编写了一个C++函数send_full_msg,尝试发送205453字节的消息,但该函数仅发送143080字节后,send()调用连续多次返回-1,最终触发超时退出。Python测试客户端通过循环接收,仅收到143080字节,无法获取完整消息。尝试禁用Socket发送超时、调整发送块大小(8000/16000字节)均未解决问题;且无论消息大小如何(如207801字节),始终仅能发送143080字节。相关代码、运行日志及函数调用方式如下:
禁用超时代码
setsockopt(sock_fd, SOL_SOCKET,SO_SNDTIMEO,(const char*)&time_val_struct,sizeof(time_val_struct));
C++发送函数
size_t network::send_full_msg(int sock_fd, char *write_buf, size_t write_buf_length, std::chrono::microseconds timeout) { debug_log log("network_interface", "send_full_msg"); try{ log.msg("Preparing to send an entire message through. msg size is...." + to_string(write_buf_length)); const size_t chunk_size = 16000; //每次发送16000字节 fcntl(sock_fd, F_SETFL, O_NONBLOCK); //设置socket为非阻塞模式 log.msg("Set socket non blocking..." + to_string(write_buf_length)); struct timeval time_val_struct; time_val_struct.tv_sec = 0; time_val_struct.tv_usec = 0; setsockopt(sock_fd, SOL_SOCKET,SO_SNDTIMEO,(const char*)&time_val_struct,sizeof(time_val_struct)); log.msg("Turned off socket timeout..." + to_string(write_buf_length)); size_t pos_in_buf = 0; //起始位置,每次发送后递增 ssize_t size_sent = 0; //send调用返回的发送字节数 int num_neg_count=0; auto start_time = chrono::high_resolution_clock::now(); while (pos_in_buf < write_buf_length) { auto current_time = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::microseconds>(current_time - start_time); if (duration > timeout || num_neg_count>3) //超时或连续3次发送失败则退出 { log.msg("Timeout exceeded"); break; } size_t remaining_buf_size = write_buf_length - pos_in_buf; //剩余待发送字节数 size_t bytes_to_write = remaining_buf_size > chunk_size ? chunk_size : remaining_buf_size; //本次要发送的字节数 size_sent = send(sock_fd, write_buf+pos_in_buf, bytes_to_write, 0); log.msg("Sent bytes..." + to_string(size_sent)); log.msg("Pos in buf..." + to_string(pos_in_buf)); log.msg("Bytes to write..." + to_string(bytes_to_write)); if (size_sent < 0) { num_neg_count++; //连续失败计数加1 this_thread::sleep_for(chrono::microseconds(100)); //等待100微秒后重试 continue; }else{ num_neg_count = 0; //重置失败计数 pos_in_buf += size_sent; } } log.msg("Total data length sent was: " + to_string(pos_in_buf)); if(pos_in_buf == 0) return -1; //无数据发送,返回错误 return pos_in_buf; //返回实际发送的总字节数 }catch(exception &e){ cout << " Exception in network socket " << e.what() << endl; return -1; } }
运行日志
[ network_interface/send_full_msg ] Preparing to send an entire message through. msg size is....205453 [ network_interface/send_full_msg ] Set socket non blocking...205453 [ network_interface/send_full_msg ] Turned off socket timeout [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...0 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...16000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...32000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...48000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...64000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...80000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...96000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...16000 [ network_interface/send_full_msg ] Pos in buf...112000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...15080 [ network_interface/send_full_msg ] Pos in buf...128000 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...-1 [ network_interface/send_full_msg ] Pos in buf...143080 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...-1 [ network_interface/send_full_msg ] Pos in buf...143080 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...-1 [ network_interface/send_full_msg ] Pos in buf...143080 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Sent bytes...-1 [ network_interface/send_full_msg ] Pos in buf...143080 [ network_interface/send_full_msg ] Bytes to write...16000 [ network_interface/send_full_msg ] Timeout exceeded [ network_interface/send_full_msg ] Total data length sent was: 143080
函数调用方式
send_full_msg(client_fd, const_char<char*>(response.c_str()), response.length(), chrono::microseconds((int)5e6);
内容的提问来源于stack exchange,提问作者Dwight Kulkarni
相关产品推荐
相关产品推荐

