zoukankan      html  css  js  c++  java
  • UNP学习笔记(第七章 套接字选项)

    有多种方法获取和设置影响套接字的选项:

    1.getsockopt和setsockopt函数

    2.fcntl函数

    3.ioctl函数

    getsockopt和setsockopt函数

    这两个函数仅用于套接字

    #include <sys/socket.h>
    int getsockopt(int sockfd,int level,int option,void *restrict optval,socklen_t restrict optlen);
    int setsockopt(int sockfd,int level,int option,const void *optval,socklen_t optlen);
                                                              //均返回:若成功则为0,若出错则为-1

    getsocksopt把以获取的当前值存放在*optval中。*optval的大小由最后一个参数指定。

    下面三个图汇总了可由getsockopt或由setsockopt设置的选项。

      

     

    套接字选择粗分为两大基本类型:

    一是启用或禁止某个特性的二元选项(标志选项)

    二是取得并返回我们可以设置或检查特定值的选项(值选项)

    图中标有“标志”的列指出一个选项是否为标志选项。当给这些标志选项调用getsockopt函数时,*optval是一个整数:0代表相应选项被禁止,不为0代表相应选项被启用。

     

    fcntl函数

    下图汇总了由fcntl、ioctl和路由套接字执行的不同操作

    fcntl函数提供了与网络编程相关的如下特性

    1.非阻塞式I/O。通过使用F_SETFL命令设置O_NONBLOCK文件状态标志,将套接字设置为非阻塞式

    2.信号驱动式I/O。通过使用F_SETFL命令设置O_ASYNC文件状态标志,把一个套接字设置成一旦其状态发生改变,内核就产生一个SIGIO信号

    3.F_SETOWN命令允许我们指定用于接收SIGIO和SIGURG信号的套接字属主(进程ID或进程组ID)

    #include <fcntl.h>
    int fcntl(int fd,int cmd,.../* int arg */);
                //返回:若成功则取决于cmd,若出错则为-1

    关于fcntl函数可以查看以前apue的学习笔记  http://www.cnblogs.com/runnyu/p/4633456.html

  • 相关阅读:
    leetcode 131. Palindrome Partitioning
    leetcode 526. Beautiful Arrangement
    poj 1852 Ants
    leetcode 1219. Path with Maximum Gold
    leetcode 66. Plus One
    leetcode 43. Multiply Strings
    pytorch中torch.narrow()函数
    pytorch中的torch.repeat()函数与numpy.tile()
    leetcode 1051. Height Checker
    leetcode 561. Array Partition I
  • 原文地址:https://www.cnblogs.com/runnyu/p/4660815.html
Copyright © 2011-2022 走看看