C语言 SOL_SOCKET 有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21515946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:43:38  来源:igfitidea点击:

What is SOL_SOCKET used for?

csocketsnetwork-programming

提问by Blake

As it is stated in the Linux manpage

正如 Linuxman页面中所述

Use this constant as the level argument to getsockoptor setsockoptto manipulate the socket-level options described in this section

使用此常量作为级别参数getsockoptsetsockopt操作本节中描述的套接字级别选项

But I don't get this explanation. What is the purpose of SOL_SOCKET? What does it do?

但我不明白这个解释。的目的是SOL_SOCKET什么?它有什么作用?

采纳答案by demo.b

SOL_SOCKETis the socket layer itself. It is used for options that are protocol independent.

SOL_SOCKET是套接字层本身。它用于与协议无关的选项。

You can read more here.

您可以在此处阅读更多内容。

回答by jspacek

When retrieving a socket option, or setting it, you specify the option name as well as the level. When level = SOL_SOCKET, the item will be searched for in the socket itself.

在检索或设置套接字选项时,您需要指定选项名称和级别。当 level = 时SOL_SOCKET,将在套接字本身中搜索该项目。

For example, suppose we want to set the socket option to reuse the address to 1 (on/true), we pass in the "level" SOL_SOCKETand the value we want it set to.

例如,假设我们想将套接字选项设置为重用地址为 1(on/true),我们传入“级别”SOL_SOCKET和我们希望它设置的值。

int value = 1;    
setsockopt(mysocket, SOL_SOCKET, SO_REUSEADDR, &value, sizeof(value));

This will set the SO_REUSEADDRin my socket to 1.

这会将SO_REUSEADDR我的套接字中的设置为 1。

I was stuck on this myself, the documentation is extremely cryptic. Slightly more detailed documentation here: http://pubs.opengroup.org/onlinepubs/7908799/xns/getsockopt.html

我自己被困在这个问题上,文档非常神秘。这里有更详细的文档:http: //pubs.opengroup.org/onlinepubs/7908799/xns/getsockopt.html