您的位置:首页 > 其它

socket()

2016-07-17 13:30 399 查看
#include <sys/types.h>

#include <sys/socket.h>

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

返回值:

On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errno is set appropriately.

描述:
socket() 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.

domain,即协议域,又称为协议族(family)。常用的协议族有,AF_INET、AF_INET6、AF_LOCAL(或称AF_UNIX,Unix域socket)、AF_ROUTE等等。协议族决定了socket的地址类型,在通信中必须采用对应的地址,如AF_INET决定了要用ipv4地址(32位的)与端口号(16位的)的组合、AF_UNIX决定了要用一个绝对路径名作为地址。http://blog.csdn.net/sandware/article/details/40923491

type,指定socket类型。常用的socket类型有,SOCK_STREAM、SOCK_DGRAM、SOCK_RAW、SOCK_PACKET、SOCK_SEQPACKET等等

protocol,故名思意,就是指定协议。常用的协议有,IPPROTO_TCP、IPPTOTO_UDP、IPPROTO_SCTP、IPPROTO_TIPC等,它们分别对应TCP传输协议、UDP传输协议、STCP传输协议、TIPC传输协议。

注意:并不是上面的type和protocol可以随意组合的,如SOCK_STREAM不可以跟IPPROTO_UDP组合。当protocol为0时,会自动选择type类型对应的默认协议
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: