我正在使用 recvmsg 和 sendmsg 通过异步 STREAM 套接字发送数据。要传输的数据量相当大,介于 15 MB 和 30 MB 之间。
我无法理解为什么数据到达时已损坏。发送消息和接收功能被 errno EINTR 和 EAGAIN 中断。
我使用的功能是:
int receive_result_with_extra(int fd, struct syscall_result * result, int extra_size, char * buf) {
struct iovec io[2];
struct msghdr msg;
int transfered=0, temp=0;
const int total = SIZE_RESULT + extra_size;
CLEAN_MSG(&msg);
memset(io, 0, sizeof(io));
// result header
io[1].iov_len=SIZE_RESULT;
io[1].iov_base=result;
// result buffer
io[0].iov_len = extra_size;
io[0].iov_base = buf;
// iov struct
msg.msg_iov=io;
msg.msg_iovlen=2;
do {
temp = recvmsg(fd,&msg, 0);
if ( temp < 0 && (errno==EAGAIN || errno == EINTR || errno == EWOULDBLOCK))
continue;
else if ( temp < 0 )
die("Error receiveing data recvmsg (receive_result_with_extra)");
transfered += temp;
} while(transfered < total);
if ( transfered < 0)
die("recvmsg (receive_result_with_extra)");
assert(transfered == total);
return transfered;
}
int send_result_with_extra(int fd, struct syscall_result * result, int extra_size, char * buf) {
struct iovec io[2];
struct msghdr msg;
int transfered=0, temp=0;
const int total = SIZE_RESULT + extra_size;
CLEAN_MSG(&msg);
memset(io, 0, sizeof(io));
// result header
io[1].iov_len=SIZE_RESULT;
io[1].iov_base=result;
// result buffer
io[0].iov_len = extra_size;
io[0].iov_base = buf;
// iov struct
msg.msg_iov=io;
msg.msg_iovlen=2;
do {
temp=sendmsg(fd,&msg, 0);
if ( temp < 0 && (errno == EINTR || errno == EAGAIN || errno == EWOULDBLOCK))
continue;
else if ( temp < 0)
die("Failed sending data ( send_result_with_extra)");
transfered += temp;
} while( transfered < total);
if ( transfered < 0)
die("recvmsg (fstat handler)");
assert(transfered == total);
return transfered;
}