setsockopt (sys/socket.h)
setsockopt 的原型是:
int setsockopt(int socket, int level, int option_name, const void *option_value, socklen_t option_len);
以下都正确吗?哪些不是?
a.)
int buffsize = 50000;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&buffsize, sizeof(buffsize));
b.)
int buffsize = 50000;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *)&buffsize, sizeof(buffsize));
c.)
char *buffsize = "50000";
setsockopt(s, SOL_SOCKET, SO_RCVBUF, buffsize, strlen(buffsize));
d.)
setsockopt(s, SOL_SOCKET, SO_RCVBUF, "50000", 5);
The prototype for setsockopt is:
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?
a.)
int buffsize = 50000;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&buffsize, sizeof(buffsize));
b.)
int buffsize = 50000;
setsockopt(s, SOL_SOCKET, SO_RCVBUF, (void *)&buffsize, sizeof(buffsize));
c.)
char *buffsize = "50000";
setsockopt(s, SOL_SOCKET, SO_RCVBUF, buffsize, strlen(buffsize));
d.)
setsockopt(s, SOL_SOCKET, SO_RCVBUF, "50000", 5);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SO_RCVBUF
选项被定义为参数类型为int
,因此 (c) 和 (d) 不正确。因为 C 会自动将
int *
转换为const void *
,所以不需要强制转换:但是,因为
char *< /code> 和
void *
也会自动转换,(a) 和 (b) 也应该可以。The
SO_RCVBUF
option is defined as having a parameter type ofint
, so (c) and (d) are not correct.Because C will automatically convert an
int *
toconst void *
, no cast is required:However, because
char *
andvoid *
will also be automatically converted, (a) and (b) should also work.