Socket API详解(2)

3、侦听客户端的连接

int listen (int socket, int n)

Description:

The listen function enables the socket socket to accept connections, thus making it a server socket. 

Arguments:
(1)socket
(2)n:指定等待连接的队列长度

The argument n specifies the length of the queue for pending connections. When the queue fills, new 
 clients attempting to connect fail with ECONNREFUSED until the server calls accept to accept a 
 connection from the queue. 

Return:

The listen function returns 0 on success and -1 on failure. The following errno error conditions are 
 defined for this function: 


EBADF 	 The argument socket is not a valid file descriptor. 


	- ENOTSOCK The argument socket is not a socket. 


	- EOPNOTSUPP The socket socket does not support this operation.

4、接收客户端的连接

int accept (int socket, struct sockaddr *addr, socklen_t *length_ptr)

Description:

This function is used to accept a connection request on the server socket socket.

Arguments:

The addr and length-ptr arguments are used to return information about the name of the client 
 socket that initiated the connection. , for information about the format of the information. 

(1)socket
(2)addr:客户端的IP地址
(3)length_ptr:

Return:返回一个新的sockfd;

Accepting a connection does not make socket part of the connection. Instead, it creates a new 
 socket which becomes connected. The normal return value of accept is the file descriptor for the 
 new socket. 

After accept, the original socket socket remains open and unconnected, and continues listening 
 until you close it. You can accept further connections with socket by calling accept again. 

If an error occurs, accept returns -1. The following errno error conditions are defined for this 
 function: 


EBADF 	 The socket argument is not a valid file descriptor. 


	- ENOTSOCK The descriptor socket argument is not a socket. 


	- EOPNOTSUPP The descriptor socket does not support this operation. 


	- EWOULDBLOCK socket has nonblocking mode set, and there are no pending 
	 connections immediately available. 

(5)客户端发起连接

int connect (int socket, struct sockaddr *addr, socklen_t length)

Description & Arguments:根据addr指定的服务器IP地址,发起连接

The connect function initiates a connection from the socket with file descriptor socket to the socket 
 whose address is specified by the addr and length arguments. (This socket is typically on another 
 machine, and it must be already set up as a server.) , for information about how these arguments are 
 interpreted. 

Normally, connect waits until the server responds to the request before it returns. You can set 
 nonblocking mode on the socket socket to make connect return immediately without waiting for the 
 response. , for information about nonblocking mode. 

Return:

The normal return value from connect is 0. If an error occurs, connect returns -1. The following errno 
 error conditions are defined for this function: 


EBADF 	 The socket socket is not a valid file descriptor. 


	- ENOTSOCK File descriptor socket is not a socket. 


	- EADDRNOTAVAIL The specified address is not available on the remote machine. 


	- EAFNOSUPPORT The namespace of the addr is not supported by this socket. 


	- EISCONN The socket socket is already connected. 


	- ETIMEDOUT The attempt to establish the connection timed out. 


	- ECONNREFUSED The server has actively refused to establish the connection. 


	- ENETUNREACH The network of the given addr isn't reachable from this host. 


	- EADDRINUSE The socket address of the given addr is already in use. 


	- EINPROGRESS The socket socket is non-blocking and the connection could not be 
	 established immediately. You can determine when the connection is completely established with 
	 select; . Another connect call on the same socket, before the connection is completely established, will 
	 fail with EALREADY. 


	- EALREADY The socket socket is non-blocking and already has a pending connection in 
	 progress (see EINPROGRESS above). 

5、关闭连接

int close (int filedes)

Description & Arguments:

The function close closes the file descriptor filedes. Closing a file has the following consequences: 


	- The file descriptor is deallocated. 


	- Any record locks owned by the process on the file are unlocked. 


	- When all file descriptors associated with a pipe or FIFO have been closed, any unread 
	 data is discarded. 

Return:

The normal return value from close is 0; a value of -1 is returned in case of failure. The following 
 errno error conditions are defined for this function: 


EBADF 	 The filedes argument is not a valid file descriptor. 


	- EINTR The close call was interrupted by a signal. . Here is an example of how to handle 
	  EINTR properly: 


         TEMP_FAILURE_RETRY (close (desc));

         
	- ENOSPC 
	- x EIO 
	- x EDQUOT When the file is accessed by NFS, these errors from write can sometimes 
	 not be detected until close. , for details on their meaning. 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值