socket()-创建通信端点
int socket (int family, int type, int protocol)
Argument
Family: AF_INET
AF_INET6
Type: SOCK_STREAM
SOCK_DGRAM
SOCK_RAW
Protocol: 0
Return value:-1 is returned if an error occurs; otherwise the return value is
a descriptor referencing the socket.
bind()-把一个名字绑定到 socket
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
Argument:
sockfd: socket descriptor from socket() return
my_addr: local address
addrlen: length of my_addr
listen()—在 socket 上监听连接
int listen(int s, int backlog);
Argument:
s: is the TCP socket (already bound to an address)
backlog:the number of incoming connections the kernel should be able to keep track of.
accept()--accept a connection on a socket
int accept(int s, struct sockaddr *addr, socklen_t *addrlen);
Argument
s: socket descript that is the passive mode TCP socket
addr: is a pointer to allocated space
addrlen: is a value-result argument, must be set to the size of cliaddr, will be set on return to be the number of bytes in cliaddr set by the call to accept
accept() is called by a TCP server to return the next completed connection from the completed connection queue which is created by listen system call. After accept returns a new socket descriptor ,I/O can be done using the read() and write() system calls.
If no pending connections are present on the queue, and the socket is not marked as non-blocking, accept blocks the caller until a connection is present. If the socket is marked non-blocking and no pending connections are present on the queue, accept returns EAGAIN.
connect()--initiate a connection on a socket
int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);