Note: UNIX Network Programming
int socket (int family, int type, int protocol)
Return value:-1 is returned if an error occurs; otherwise the return value is
a descriptor referencing the socket.
int bind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen);
sockfd: socket descriptor from socket() return
my_addr: local address
addrlen： length of my_addr
listen()—在 socket 上监听连接
int listen(int s, int backlog);
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);
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);