Linux socket函数 编程手册 重点分析一

来源:互联网 发布:上海大数据培训哪里好 编辑:程序博客网 时间:2024/06/05 18:12

man socket:

头文件和函数原型:

#include <sys/types.h>          /* See NOTES */
 #include <sys/socket.h>
int socket(int domain, int type, int protocol);

domain参数指定通信域,具体取值定义类型都在<sys/socket.h>,现可用类型包括:

 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)

type参数指定套接字的类型,进一步确定通信特征:

SOCK_STREAM     Provides sequenced, reliable, two-way, connection-based byte streams.  An out-of-band data  trans‐
                       mission 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).


参数protocol通常是零,标示按给定的域和套接字类型选择默认协议。当对同一域和套接字类型支持多个协议时,可以使用protocol参数选择一个特定协议:IPPROTO_TCPIPPTOTO_UDPIPPROTO_SCTPIPPROTO_TIPC


接下去第二章实例分析。




0 0
原创粉丝点击