打断我在GNU/Linux下用C编程的多线程服务器这种怪异的行为。发送数据时,最终会被SIGPIPE中断。我设法忽略send()中的信号,并在每次操作之后处理errno,因为它。的send()总是由EPIPE
因此,它有两种单独的发送方法,一种是一次发送大量数据(或者至少尝试发送),另一种是发送近似相似的数量并将其切片成小块。最后,我试着用它来保持它发送数据。
do
{
total_bytes_sent += send(client_sd, output_buf + total_bytes_sent,
output_buf_len - total_bytes_sent, MSG_NOSIGNAL);
}
while ((total_bytes_sent < output_buf_len) && (errno != EPIPE));
这丑陋的代码做的工作在某些情况下,但并非总是如此。
我很确定这不是硬件或ISP问题,因为这台服务器运行在六台欧洲服务器上,四台在德国,两台在法国。
任何想法?
在此先感谢。
编辑1:是的,我注意到这段代码是蹩脚的(谢谢杰伊)。我最初的意思是,只要客户切断通信,这段代码就会给我一个EPIPE。
编辑2:我试着用一个单一的send()和它随机给了我同样的错误。这很奇怪,因为我无法发送大数据块。我试图扩大发送缓冲区,但没有工作。
EDIT 3:按照要求,这是一个较大的代码块。
data_buf_len = cur_stream->iframe_offset[cur_stream->iframe_num - 1] - first_offset;
data_buf = cur_stream->data;
output_buf = compose_reply(send_params, data_buf, data_buf_len, &output_buf_len);
/* Obviously, time measuring is *highly* unaccurate, only for
* design consistency purposes (it should return something).
* */
clock_gettime(CLOCK_REALTIME, &start_time);
total_bytes_sent = send(client_sd, output_buf, output_buf_len, MSG_NOSIGNAL);
clock_gettime(CLOCK_REALTIME, &stop_time);
spent_time = (((int64_t)stop_time.tv_sec * NANOSEC_IN_SEC) +
(int64_t)stop_time.tv_nsec) - (((int64_t)start_time.tv_sec * NANOSEC_IN_SEC) +
(int64_t)start_time.tv_nsec);
free(output_buf);
unload_video(cur_video);
if (total_bytes_sent < 0)
{
log_message(MESSAGE, __func__, IMSG_VIDEOSTOP, cur_video->path);
log_message(MESSAGE, __func__, IMSG_VIDEOSTOP, NULL);
}
/* Hope it will not serve >2147483647 seconds (~68 years) of video... */
return ((int)spent_time);
只有一个带有大缓冲区的send()调用。还有另一个例子,太大而不能放在这里,它将每个缓冲区分成更小的块,并为每个块调用send()。
即使客户端没有切断通信,您是否确定它会提供EPIPE? – Jay 2010-04-08 14:40:39
很确定,是的,因为我试着用EPIPE条件先退出。 – 2010-04-09 09:08:27
你可以发布更多的代码吗? – Jay 2010-04-09 09:22:46