我想知道...如果您创建一个具有简单套接字参数的函数并在该函数中执行基本指令,例如为该套接字设置不同的选项,那么实际的区别是什么 (setsockopt()
) 并且在功能存在之后它仍将是选项?或者我应该将该参数指针指向该套接字,以便保留将发生在该套接字上的实际更改。
sctp_enable_events( int socket, int ev_mask )
{
struct sctp_event_subscribe ev;
bzero(&ev, sizeof(ev));
if (ev_mask & SCTP_SNDRCV_INFO_EV)
ev.sctp_data_io_event = 1;
/*code */
if (setsockopt(socket,
IPPROTO_SCTP,
SCTP_EVENTS,
SCTP_SET_EVENTS,
(const char*)&ev,
sizeof(ev)) != 0 ) {
fprintf(where,
"sctp_enable_event: could not set sctp events errno %d\n",
errno);
fflush(where);
exit(1);
}
}
还是这样?
sctp_enable_events( int *socket, int ev_mask, struct sctp_event_subscribe *ev )
{
if (ev_mask & SCTP_SNDRCV_INFO_EV)
ev->sctp_data_io_event = 1;
/*code */
if (setsockopt(*socket,
IPPROTO_SCTP,
SCTP_EVENTS,
SCTP_SET_EVENTS,
ev,
sizeof(*ev)) != 0 ) {
fprintf(where,
"sctp_enable_event: could not set sctp events errno %d\n",
errno);
fflush(where);
exit(1);
}
}
我知道通过将指针传递给 struct、int、char 等。您可以在函数执行后修改值,如果没有指针,修改将仅保留在该函数的局部,但不会全局更改。
但是如何使用setsockopt
函数呢?
最佳答案
setsockopt
函数无法说明您是如何得出它的参数的。因此,它不能采取不同的行动。你是否写:
f(1);
或
int x = 1;
int* xPtr = &x;
f(*xPtr);
无法被 f 检测到。
这个问题真的和setsockopt
无关。我建议您加强关于指针和值的 C 知识。如果没有这种理解,您将在 C 语言中犯很多错误。
关于c - 本地套接字选项集与指向套接字选项集的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668826/