50 likes | 280 Views
Socket ( int domain , int type , int protocol ) 每个套接口都事一个数据通信通道的一段。在两个进程通过套接口建立连接后,他们 就使用套接口描述字来从套接口读取数据,并向套接口中写数据。 domain AF_UNIX Unix 内部协议 AF_INET ARPA 网际协议 ( 最常使用的选项) AF_ISO 国际标准组织协议 AF_NS Xerox 网络系统协议 when domain = AF_INET type
E N D
Socket(int domain,int type,int protocol) 每个套接口都事一个数据通信通道的一段。在两个进程通过套接口建立连接后,他们 就使用套接口描述字来从套接口读取数据,并向套接口中写数据。 domain AF_UNIX Unix内部协议 AF_INET ARPA网际协议(最常使用的选项) AF_ISO 国际标准组织协议 AF_NS Xerox网络系统协议 when domain=AF_INET type SOCK_STREAM 提供一个可靠的顺序的双向连接(最常使用) SOCK_DGRAM 无连接,不可靠数据报服务 SOCK_RAW SOCK_SEQPACKET SOCK_RDM protocol 协议号 =0
Bind(int socket,struct sockaddr_in * my_addr, int my_addr_length) bind将一个进程和一个套接口联系起来,常由服务器进程调用。给套接口赋予一个地址 struct sockaddr_in { sa_family_t sin_family; /* Address family */ unsigned short int sin_port; /* Port number */ struct in_addr sin_addr; /* Internet address */ /* Pad to size of `struct sockaddr'. */ unsigned char __pad[__SOCK_SIZE__ - sizeof(short int) - sizeof(unsigned short int) - sizeof(struct in_addr)]; }; /* Internet address. */ struct in_addr { __u32 s_addr; };
Listen(int socket, int input_queue_size) 在套接口被创建并和进程联系在一起之后,服务器进程可以调用listen函数来监听 接入的套接口连接。 第二个参数指定接入的队列大小。通常服务器程序会调用系统函数fork产生一个自 身的副本来处理接入的套接口调用。
Connect(int socket, struct sockaddr *server_address, int server_address_length) recv(int socket, void * buf, int buf_len, unsigned int flags) flags 可能的值: MSG_OOB This flag requests receipt of out-of-band data that would not be received in the normal data stream. Some protocols place expedited data at the head of the normal data queue, and thus this flag cannot be used with such protocols. MSG_PEEK This flag causes the receive operation to return data from the beginning of the receive queue without removing that data from the queue. Thus, a subsequent receive call will return the same data. MSG_WAITALL This flag requests that the operation block until the full request is satisfied. However, the call may still return less data than requested if a signal is caught, an error or disconnect occurs, or the next data to be received is of a different type than that returned.
Send(int socket, const void * message_data, int message_data_length unsigned int flags) int accept(int s, struct sockaddr *addr, socklen_t *addrlen) DESCRIPTION The accept function is used with connection-based socket types (SOCK_STREAM, SOCK_SEQPACKET and SOCK_RDM). It extracts the first connection request on the queue of pending connections, creates a new connected socket with mostly the same properties as s, and allocates a new file descriptor for the socket, which is returned. The newly created socket is no longer in the listening state. The original socket s is unaffected by this call. The argument s is a socket that has been created with socket(2), bound to a local address with bind(2), and is listening for connections after a listen(2).