问题描述
的原型是setsockopt的:
The prototype for setsockopt is:
INT的setsockopt(INT插座,INT水平,诠释OPTION_NAME,常量无效*请将option_value中,socklen_t option_len);
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len);
有以下正确的?这是不?
Are the following all correct ? Which are not ?
一)。
INT BUFFSIZE = 50000;
setsockopt的(S,SOL_SOCKET,SO_RCVBUF,(字符*)及BUFFSIZE,sizeof的(BUFFSIZE));
a.)int buffsize = 50000;setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&buffsize, sizeof(buffsize));
湾)
INT BUFFSIZE = 50000;
setsockopt的(S,SOL_SOCKET,SO_RCVBUF,(无效*)及BUFFSIZE,sizeof的(BUFFSIZE));
b.)int buffsize = 50000;setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *)&buffsize, sizeof(buffsize));
℃。)
字符* BUFFSIZE =50000;
setsockopt的(S,SOL_SOCKET,SO_RCVBUF,BUFFSIZE,strlen的(BUFFSIZE));
c.)char *buffsize = "50000";setsockopt(s, SOL_SOCKET, SO_RCVBUF, buffsize, strlen(buffsize));
Ð)。
setsockopt的(S,SOL_SOCKET,SO_RCVBUF,50000,5);
d.)setsockopt(s, SOL_SOCKET, SO_RCVBUF, "50000", 5);
推荐答案
的 SO_RCVBUF
选项被定义为具有的参数类型 INT
,所以(c)和(d)是不正确的。
The SO_RCVBUF
option is defined as having a parameter type of int
, so (c) and (d) are not correct.
的
由于C会自动转换成一个为int *
到常量无效*
,不需要造型:
Because C will automatically convert an int *
to const void *
, no cast is required:
int buffsize = 50000;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, &buffsize, sizeof(buffsize));
不过,由于的char *
和无效*
也将被自动转换,(一)和(b)还应该工作。
However, because char *
and void *
will also be automatically converted, (a) and (b) should also work.
这篇关于setsockopt的(SYS / socket.h中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!