如何在非阻塞函数中将这个 write_all 转为通过 UDP 写入字节?
How to turn this write_all in non blocking function to write bytes through UDP?
我在 C 中有这个函数,我想用它在非阻塞模式下通过我的 udp 套接字写入字节。我应该完成“TO BE DONE”部分。任何人都可以帮助我了解该怎么做吗?回顾那里的写入功能是否有意义?
ssize_t nonblocking_write_all(int fd, const void *ptr, size_t n)
{
size_t n_left = n;
while (n_left > 0) {
ssize_t n_written = write(fd, ptr, n_left);
if (n_written < 0) {
/*** TO BE DONE START ***/
/*** TO BE DONE END ***/
if (n_left == n)
return -1; /* nothing has been written */
else
break; /* we have written something */
} else if (n_written == 0) {
continue;
}
n_left -= n_written;
ptr += n_written;
}
assert(n - n_left >= 0);
return n - n_left;
}
这有效:n_written=nonblocking_write_all(fd,ptr,n_left);
我在 C 中有这个函数,我想用它在非阻塞模式下通过我的 udp 套接字写入字节。我应该完成“TO BE DONE”部分。任何人都可以帮助我了解该怎么做吗?回顾那里的写入功能是否有意义?
ssize_t nonblocking_write_all(int fd, const void *ptr, size_t n)
{
size_t n_left = n;
while (n_left > 0) {
ssize_t n_written = write(fd, ptr, n_left);
if (n_written < 0) {
/*** TO BE DONE START ***/
/*** TO BE DONE END ***/
if (n_left == n)
return -1; /* nothing has been written */
else
break; /* we have written something */
} else if (n_written == 0) {
continue;
}
n_left -= n_written;
ptr += n_written;
}
assert(n - n_left >= 0);
return n - n_left;
}
这有效:n_written=nonblocking_write_all(fd,ptr,n_left);