Related operations of socket
1. Creating a socket: Everything in UNIX is a file socket system call to create a socketint socket (int domain, int type, int protocol);domain: tells the system which underlying protocol family to use type: specifies the service type 1.1 The service types are SOCK_STREAM and SOCK_UGRAM1. The kernel starts at 2.16.17, and the type can be followed by the logical AND parameters SOCK_NONBLOCK and SOCK_CLOEXEC to indicate whether the socket is non-blocking 1.3 protocol parameters Under the first two parameters, select a specific protocol invocation result: Returns a socket file descriptor, failure returns-1 and sets errno2. Named socket creation, we only specified the address family, but did not specify the specific socket address to use to speak a socket and socket address binding called socket naming server needs to name socket, the client does not need 2.1int bind (int sockfd,const struct sockaddr * my_addr,socklen_t addrlen);
bind successfully returns 0, fails returns-1 and sets errno
2.2 Common failures are EACCES: the bound address is protected and only superusers can access it.
EADDRINUSE: bound address in use
3. listening socket,
After creation and naming, it cannot be directly accessed by clients. A listening queue needs to be created to store pending client connections.
3.1 int listen(int sockfd, int backlog);
The backlog parameter indicates the maximum length of the kernel listening queue
After that, customer connections will no longer be accepted
The error message is ECONNREFUSED.
listen singing returns 0, failure returns-1 and errno is set
Full connections are only backlog+1
4. accept connections
int accept(int sockfd,struct sockaddr addr,socklen_t addrlen)
Question: What happens if a client requests a network anomaly or a drop?
Answer: accept only takes connections from the listening queue, regardless of the state of the connection, and does not care about network changes