Ank*_*rVj 6 c sockets linux gcc
我读的man页面,我的理解是,如果write()失败,并设置errno到EAGAIN或者EINTR,我可以执行write()了,所以我想出了下面的代码:
ret = 0;
while(ret != count) {
write_count = write(connFD, (char *)buf + ret, count);
while (write_count < 0) {
switch(errno) {
case EINTR:
case EAGAIN:
write_count = write(connFD, (char *)buf + ret, count -ret);
break;
default:
printf("\n The value of ret is : %d\n", ret);
printf("\n The error number is : %d\n", errno);
ASSERT(0);
}
}
ret += write_count;
}
Run Code Online (Sandbox Code Playgroud)
我正在表演read()和write()插座,read()并按上述方式处理.我正在使用Linux,带gcc编译器.
你有一点"不要重复自己"的问题 - 不需要两个单独的调用write,也不需要两个嵌套循环.
我的正常循环看起来像这样:
for (int n = 0; n < count; ) {
int ret = write(fd, (char *)buf + n, count - n);
if (ret < 0) {
if (errno == EINTR || errno == EAGAIN) continue; // try again
perror("write");
break;
} else {
n += ret;
}
}
// if (n < count) here some error occurred
Run Code Online (Sandbox Code Playgroud)