socket编程总结(六) / int socket(int domain, int type, int protocol)

来源:互联网 发布:怎样看淘宝好评率 编辑:程序博客网 时间:2024/06/04 08:39
         int socket(int domain, int type, int protocol)
  函数返回一个整型的socket描述符,供后面的使用,其中参数:
  domain:指明使用的协议族,值
AF_INET:用于网络通信
AF_UNIX:单一Unix系统中进程间通信
  type: 指明socket类型,值
SOCK_STREAM:流式,面向连接的比特流,顺序、可靠、双向,用于TCP通信
SOCK_DGRAM: 数据报式,无连接的,定长、不可靠  UDP通信
  protocol:由于指定了type,这里一般用“0”
  一个socket的建立:
  int fd = socket(AF_INET, SOCK_STREAM, 0)
==================

intsocket(int domain,int type, int protocol)

_________________________返回值:非负描述符 – 成功,-1 - 出错

其中:

family指明了协议族/域,通常AF_INET、AF_INET6、AF_LOCAL等;

type是套接口类型,主要SOCK_STREAM、SOCK_DGRAM、SOCK_RAW;

protocol一般取为0。成功时,返回一个小的非负整数值,与文件描述符类似。



1 0
原创粉丝点击