gpt4 book ai didi

sockets - 关闭对 SCTP 套接字的调用是阻塞还是非阻塞?

转载 作者:行者123 更新时间:2023-12-05 01:13:53 25 4
gpt4 key购买 nike

我遇到了一个问题,不确定我是否可以将其称为问题或只是理解上的差距。

我正在 SCTP 套接字 FD 上调用 close()(类似这样的东西:close(sctp_sock_fd);)。我期待这个关闭调用将在 SCTP SHUTDOWN 流程完成时返回,即它将在下面执行然后返回:

SHUTDWON (Source -> Peer)
SHUTDWON_ACK (Source <- Peer)
SHUTDOWN_COMPLETE (Source -> Peer)

但我看到它看起来像 close(sctp_sock_fd); 在调用后很快返回,并且 SCTP 关闭序列正在进行中。

如果它确实是一个非阻塞调用,那么有什么方法可以确保 SCTP 内核级别的正常关闭已经完成??

最佳答案

是的,默认情况下 close() 将在 SCTP 套接字上是非阻塞的,它只会启动关闭过程,而不是等待它完成。

您可以通过设置 SO_LINGER 套接字选项来更改它:

struct linger lin;
unsigned int len =sizeof(lin);
lin.l_onoff=1;
lin.l_linger=10;
setsockopt(socketfd,SOL_SOCKET, SO_LINGER,&lin, len);

使用此设置,close() 将最多阻塞 10 秒 来自 SCTP Socket API RFC :

8.1.4. SO_LINGER

An application can use this option to perform the SCTP ABORT
primitive. This option affects all associations related to the
socket.

The linger option structure is

struct linger {int l_onoff; /* option on/off /int l_linger; / linger time */ };

To enable the option, set l_onoff to 1. If the l_linger value isset to 0, calling close() is the same as the ABORT primitive. Ifthe value is set to a negative value, the setsockopt() call willreturn an error. If the value is set to a positive valuelinger_time, the close() can be blocked for at most linger_time.Please note that the time unit is in seconds, according to POSIX,but might be different on specific platforms. If the gracefulshutdown phase does not finish during this period, close() willreturn, but the graceful shutdown phase will continue in thesystem.

Note that this is a socket-level option, not an SCTP-level option.When using this option, an application must specify a level of
SOL_SOCKET in the call.

如果您使用的是 Linux,还有 these notes在源代码中。

关于sockets - 关闭对 SCTP 套接字的调用是阻塞还是非阻塞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34395565/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com