socket

一. AF 和 PF

    作为宏定义,两者对应的数值完全相同,区别只存在于文字上:

        AF: Address  Family

        PF: Protocol Family

    特别说明,在 Unix/Linux 系统的不同版本中,这两者存在微小差别,对于 BSD 是 AF,对于 POSIX 是 PF。

    注: 理论上,创建 socket 是指定协议,应用 PF_xxxx,而设置地址应用 AF_xxxx。但 man socket 出现的都是 AF,故建议统一用 AF。


二. socket

        int socket(int domain, int type, int protocol);

    creates an endpoint for communication and returns a descriptor.

    The domain argument specifies a communication domain; this selects the protocol family which will be used for communication. These families are defined in <sys/socket.h>. The currently understood formats include:

       Name                Purpose                          Man page
       AF_UNIX, AF_LOCAL   Local communication              unix(7)
       AF_INET             IPv4 Internet protocols          ip(7)
       AF_INET6            IPv6 Internet protocols          ipv6(7)
       AF_IPX              IPX - Novell protocols
       AF_NETLINK          Kernel user interface device     netlink(7)
       AF_X25              ITU-T X.25 / ISO-8208 protocol   x25(7)
       AF_AX25             Amateur radio AX.25 protocol
       AF_ATMPVC           Access to raw ATM PVCs
       AF_APPLETALK        Appletalk                        ddp(7)
       AF_PACKET           Low level packet interface       packet(7)

    The socket has the indicated type, which specifies the communication semantics.  Currently defined types are:

        SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based byte streams.  An out-of-band data  transmission  mechanism may be supported.
        SOCK_DGRAM      Supports datagrams (connectionless, unreliable messages of a fixed maximum length).
        SOCK_SEQPACKET  Provides  a  sequenced,  reliable,  two-way  connection-based data transmission path for datagrams of fixed maximum length; a consumer is required to read an entire packet with each input system call.
        SOCK_RAW        Provides raw network protocol access.
        SOCK_RDM        Provides a reliable datagram layer that does not guarantee ordering.
        SOCK_PACKET     Obsolete and should not be used in new programs; see packet(7).
    Some socket types may not be implemented by all protocol families; for example, SOCK_SEQPACKET is not implemented for AF_INET.
    Since Linux 2.6.27, the type argument serves a second purpose: in addition to specifying a socket type, it may include the  bitwise OR of any of the following values, to modify the behavior of socket():
       SOCK_NONBLOCK   Set  the  O_NONBLOCK  file  status  flag  on  the  new open file description.  Using this flag saves extra calls to fcntl(2) to achieve the same result.
       SOCK_CLOEXEC    Set the close-on-exec (FD_CLOEXEC) flag on the new file descriptor.  See the description of the O_CLOEXEC  flag  in open(2) for reasons why this may be useful. 当子进程 exec 一个新的程序时,调用进程中打开的文件描述符仍然保持打开,但设置了执行即关 FD_CLOEXEC 的文件描述字除外。

    The protocol specifies a particular protocol to be used with the socket.  Normally only a single protocol exists to support a par‐ticular socket type within a given protocol family, in which case protocol can be specified as 0.  However,  it  is  possible  that many protocols may exist, in which case a particular protocol must be specified in this manner.  The protocol number to use is spe‐cific to the “communication domain” in which communication is to take place; see protocols(5).  See getprotoent(3) on  how  to  map protocol name strings to protocol numbers.
    Sockets of type SOCK_STREAM are full-duplex byte streams, similar to pipes(PS: PIPES are not full-duplex).  They do not preserve record boundaries.  A stream socket must be in a connected state before any data may be sent or received on it.  A connection to another socket is created  with a  connect(2)  call.   Once  connected, data may be transferred using read(2) and write(2) calls or some variant of the send(2) and recv(2) calls.  When a session has been completed a close(2) may be  performed.   Out-of-band  data  may  also  be  transmitted  as described in send(2) and received as described in recv(2).

    Q: what's the differences between read/write and send/recv? 

    Ahttp://blog.csdn.net/deng529828/article/details/6245254


三. raw socket

    利用 raw socket,可以在用户空间实现新的 IPv4 协议。raw socket 收发的报文不包含链路头,即硬件地址。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Select在Socket编程中还是比较重要的,可是对于初学Socket的人来说都不太爱用Select写程序,他们只是习惯写诸如connect、accept、recv或recvfrom这样的阻塞程序(所谓阻塞方式block,顾名思义,就是进程或是线程执行到这些函数时必须等待某个事件的发生,如果事件没有发生,进程或线程就被阻塞,函数不能立即返回)。可是使用Select就可以完成非阻塞(所谓非阻塞方式non-block,就是进程或线程执行此函数时不必非要等待事件的发生,一旦执行肯定返回,以返回值的不同来反映函数的执行情况,如果事件发生则与阻塞方式相同,若事件没有发生则返回一个代码来告知事件未发生,而进程或线程继续执行,所以效率较高)方式工作的程序,它能够监视我们需要监视的文件描述符的变化情况——读写或是异常。下面详细介绍一下! Select的函数格式(我所说的是Unix系统下的伯克利socket编程,和windows下的有区别,一会儿说明): int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 先说明两个结构体: 第一,struct fd_set可以理解为一个集合,这个集合中存放的是文件描述符(file descriptor),即文件句柄,这可以是我们所说的普通意义的文件,当然Unix下任何设备、管道、FIFO等都是文件形式,全部包括在内,所以毫无疑问一个socket就是一个文件,socket句柄就是一个文件描述符。fd_set集合可以通过一些宏由人为来操作,比如清空集合FD_ZERO(fd_set *),将一个给定的文件描述符加入集合之中FD_SET(int ,fd_set *),将一个给定的文件描述符从集合中删除FD_CLR(int ,fd_set*),检查集合中指定的文件描述符是否可以读写FD_ISSET(int ,fd_set* )。一会儿举例说明。 第二,struct timeval是一个大家常用的结构,用来代表时间值,有两个成员,一个是秒数,另一个是毫秒数。 具体解释select的参数: int maxfdp是一个整数值,是指集合中所有文件描述符的范围,即所有文件描述符的最大值加1,不能错!在Windows中这个参数的值无所谓,可以设置不正确。 fd_set *readfds是指向fd_set结构的指针,这个集合中应该包括文件描述符,我们是要监视这些文件描述符的读变化的,即我们关心是否可以从这些文件中读取数据了,如果这个集合中有一个文件可读,select就会返回一个大于0的值,表示有文件可读,如果没有可读的文件,则根据timeout参数再判断是否超时,若超出timeout的时间,select返回0,若发生错误返回负值。可以传入NULL值,表示不关心任何文件的读变化。 fd_set *writefds是指向fd_set结构的指针,这个集合中应该包括文件描述符,我们是要监视这些文件描述符的写变化的,即我们关心是否可以向这些文件中写入数据了,如果这个集合中有一个文件可写,select就会返回一个大于0的值,表示有文件可写,如果没有可写的文件,则根据timeout参数再判断是否超时,若超出timeout的时间,select返回0,若发生错误返回负值。可以传入NULL值,表示不关心任何文件的写变化。 fd_set *errorfds同上面两个参数的意图,用来监视文件错误异常。 struct timeval* timeout是select的超时时间,这个参数至关重要,它可以使select处于三种状态,第一,若将NULL以形参传入,即不传入时间结构,就是将select置于阻塞状态,一定等到监视文件描述符集合中某个文件描述符发生变化为止;第二,若将时间值设为0秒0毫秒,就变成一个纯粹的非阻塞函数,不管文件描述符是否有变化,都立刻返回继续执行,文件无变化返回0,有变化返回一个正值;第三,timeout的值大于0,这就是等待的超时时间,即select在timeout时间内阻塞,超时时间之内有事件到来就返回了,否则在超时后不管怎样一定返回,返回值同上述。 返回值: 负值:select错误 正值:某些文件可读写或出错 0:等待超时,没有可读写或错误的文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值