您的位置:首页 > 运维架构 > Linux

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

2014-06-20 11:21 183 查看
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_TCP、IPPTOTO_UDP、IPPROTO_SCTP、IPPROTO_TIPC

接下去第二章实例分析。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux socket