• Unix Network Programming Episode 56


    int main(int argc, char **argv)
    {
    	int fd;
    	socklen_t len;
    
    • 1
    • 2
    • 3
    • 4

    main function to check all socket options

    static char strres[128];
    
    static char *sock_str_flag(union val *ptr, int len)
    {
        if(len!=sizeof(int))
            snprintf(strres, sizeof(strres), "size (%d) not sizeof(int)", len);
        else
            snprintf(strres, sizeof(strres), "%s", (ptr->i_val==)?"off": "on");
        return strres;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    sock_str_flag function: convert flag option to a string

    Socket States

    Some socket options have timing considerations about when to set or fetch the option versus the state of the socket. We mention these with the affected options.
    The following socket options are inherited by a connected TCP socket from the listening socket (pp. 462–463 of TCPv2): SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_LINGER, SO_OOBINLINE, SO_RCVBUF, SO_RCVLOWAT, SO_SNDBUF, SO_SNDLOWAT, TCP_MAXSEG, and TCP_NODELAY. This is important with TCP because the connected socket is not returned to a server by accept until the three-way handshake is completed by the TCP layer.

    Generic Socket Options

    We start with a discussion of the generic socket options. These options are protocol-independent (that is, they are handled by the protocol-independent code within the kernel, not by one particular protocol module such as IPv4), but some of the options apply to only certain types of sockets. For example, even though the SO_BROADCAST socket option is called “generic,” it applies only to datagram sockets.

    SO_BROADCAST Socket Option

    This option enables or disables the ability of the process to send broadcast messages. Broadcasting is supported for only datagram sockets and only on networks that support the concept of a broadcast message (e.g., Ethernet, token ring, etc.). You cannot broadcast on a point-to-point link or any connection-based transport protocol such as SCTP or TCP.

    SO_DEBUG Socket Option

    This option is supported only by TCP. When enabled for a TCP socket, the kernel keeps track of detailed information about all the packets sent or received by TCP for the socket. These are kept in a circular buffer within the kernel that can be examined with the trpt program. Pages 916–920 of TCPv2 provide additional details and an example that uses this option.

    SO_DONTROUTE Socket Option

    This option specifies that outgoing packets are to bypass the normal routing mechanisms of the underlying protocol. For example, with IPv4, the packet is directed to the appropriate local interface, as specified by the network and subnet portions of the destination address. If the local interface cannot be determined from the destination address (e.g., the destination is not on the other end of a point-to-point link, or is not on a shared network), ENETUNREACH is returned.

  • 相关阅读:
    k8s-Pod基础
    C++ - 8月31日 - 约瑟夫环问题
    Acrel-2000系列监控系统在亚运手球比赛馆建设10kV供配电工程中的应用
    C复习-标准函数库:数值计算+字符串转换+日期+信号处理+locale
    ElementUI实现在下拉列表里面进行搜索
    【质量】代码质量评价标准
    高分辨率遥感卫星影像在交通方面的应用及高分二号影像获取
    【代码精读】中断路由代码导读:当cpu运行在TEE来了一个Group0中断
    JSON(JavaScript Object Notation)
    代码中大量的套娃式ifelse优化方案
  • 原文地址:https://blog.csdn.net/myfather103/article/details/81868829