Make writing a habit together! This is the 13th day of my participation in the “Gold Digging Day New Plan · April More Text Challenge”. Click here for more details.

1. socket

#include <sys/types.h> /* See NOTES */ 
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Copy the code
  • domain:
  • AF_INET This is most of the protocols used to generate sockets, which use TCP or UDP for transmission and IPv4 addresses
  • AF_INET6 is similar but uses aN IPv6 address
  • The AF_UNIX local protocol, used on Unix and Linux systems, is usually used when the client and server are on the same machine
  • type:
  • SOCK_STREAM is a protocol for sequential, reliable, data-complete byte stream based connections. This is the most used socket type, and this socket uses TCP for transmission.
  • The SOCK_DGRAM protocol is a connectionless, fixed-length transport call. The protocol is unreliable and uses UDP to make its connections. The SOCK_SEQPACKET protocol is a two-line, reliable connection that sends fixed-length packets for transmission. The package must be accepted in its entirety before it can be read.
  • SOCK_RAW This socket type provides single network access. This socket type uses the ICMP public protocol. (Ping and traceroute use this protocol.)
  • The SOCK_RDM type is rarely used and is not implemented on most operating systems. It is provided for the data link layer and does not guarantee the number
  • Packet order Protocol:
  • 0 Default Protocol
  • The return value:
  • Returns a new file descriptor on success, -1 on failure, and sets errno

Socket () opens a network communication port and, if successful, returns a file descriptor just like open(). Applications can read/write data over the network as if they were reading or writing a file, or -1 if the socket() call fails. For IPv4, the domain parameter is specified as AF_INET. For TCP, the type parameter is specified as SOCK_STREAM, which represents a stream-oriented transport protocol. In the case of UDP, the type parameter is specified as SOCK_DGRAM, which lists the datagram oriented transport protocols. The protocol parameter is omitted and can be set to 0.

2. bind

#include <sys/types.h> /* See NOTES */ 
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Copy the code
  • Sockfd: indicates the socket file descriptor
  • Addr: constructs the IP address and port number
  • The addrlen: sizeof (addr) length
  • The return value:
  • Returns 0 on success, -1 on failure, and sets errno

The network address and port number monitored by the server program are usually fixed. The client program can initiate a connection to the server after learning the address and port number of the server program. Therefore, the server needs to call bind to bind a fixed network address and port number.

Bind () binds the parameters sockfd and addr together so that sockfd, the file descriptor used for network communication, listens for the address and port number described by addr. Struct sockaddr * (struct sockaddr *, struct sockaddr *, struct sockaddr *, struct sockaddr *, struct sockaddr *, struct sockaddr *, struct sockaddr *, struct sockaddr *, struct sockaddr *) Such as:

struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr)); 
servaddr.sin_family = AF_INET; 
servaddr.sin_addr.s_addr = htonl(INADDR_ANY); 
servaddr.sin_port = htons(8000);
Copy the code

First, clear the entire structure, then set the address type to AF_INET and the network address to INADDR_ANY. This macro represents any local IP address, because the server may have multiple network cards, and each network card may be bound to multiple IP addresses, so the setting can listen on all IP addresses. The IP address is not determined until the connection is established with a client. Port number is 8000.

3. listen

#include <sys/types.h> /* See NOTES */ 
#include <sys/socket.h>
int listen(int sockfd, int backlog);
Copy the code
  • Sockfd: indicates the socket file descriptor
  • Backlog: The total number of links queued for the 3-handshake queue and those just queued for the 3-handshake queue

Review the system default backlog

 cat /proc/sys/net/ipv4/tcp_max_syn_backlog
Copy the code

A typical server program can serve multiple clients at the same time. When a client initiates a connection, the server calls Accept () to return and accept the connection. Listen () declares that the SOckFD is listening and allows up to a backlog of clients to be entertained. Any additional connection requests are ignored. Listen () returns 0 on success and -1 on failure.

4. accept

#include <sys/types.h> /* See NOTES */ 
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
Copy the code
  • Sockdf: indicates the socket file descriptor
  • Addr: indicates the outgoing parameter that returns the IP address and port number of the linked client
  • Addrlen: passes in the outgoing argument (value-result), sizeof(addr) size, and returns the sizeof the address structure that was actually received
  • Return value: Returns a new socket file descriptor for communicating with clients on success, -1 on failure, and sets errno

After the three-way handshake is complete, the server calls Accept () to accept the connection. If the server calls Accept () without a client connection request, it blocks and waits until a client connects. Addr is an outgoing argument, and accept() returns the address and port number of the outgoing client. The addrlen argument is a value-result argument that is passed in the length of addr provided by the caller to avoid buffer overflow problems, and passed out the actual length of the guest address structure (which may not fill the caller’s buffer). If NULL is passed to the addr parameter, the client address is not cared for.

Our server application structure looks like this:

while (1) { cliaddr_len = sizeof(cliaddr); connfd = accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len); n = read(connfd, buf, MAXLINE); . close(connfd); }Copy the code

The entire loop is a while loop that processes one client connection at a time. Since cliaddr_len is an incoming and outgoing argument, the initial value should be reassigned before each call to Accept (). The accept() parameter, Listenfd, is the file descriptor previously listened on, and the accept() return value is another file descriptor, connfd. Then, the client communicates with this connfd. Finally, connfd is closed and the connection is disconnected, but listenfd is not closed. Returning to the beginning of the loop, Listenfd is still used as the accept parameter. Accept () returns a file descriptor on success, -1 on error.

5. connect

#include <sys/types.h> /* See NOTES */ 
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
Copy the code
  • Sockdf: indicates the socket file descriptor
  • Addr: Specifies the server address information, including the IP address and port number
  • Addrlen: Pass in the argument, sizeof(addr) size
  • Return value: 0 on success, -1 on failure, set errno

The client calls connect() to connect to the server. Connect takes the same form as bind, except that bind takes its own address while connect takes the address of the other party. Connect () returns 0 on success, -1 on error.

6. Summary

This article introduces network socket functions: socket, bind, listen, Accept, connect, etc.