#include <sys/socket.h>--------------------------------------------------------------------------------- 1. int socket (int domain, int type, int protocol) socket:return fd domain:af_inet (IPv4), af_inet 6 (IPV6) type:sock_dgram (UDP), Sock_stream (TCP), Sock_ram (IP) protocol:0 (usually)---------- ----------------------------------------------------------------------- 2. int shutdown (int sockfd, int how) &NBSP;HOW:SHUT_RD (read end) Shut_rdwr (read end)------------------------------- --------------------------------------------------3. Network/host byte order network: (Tcp/ip:big-endian) host: ( Little-endian:x86, x86-64, Linux, FreeBSD, #include <arpa/inet.h> or #include <netin Et/in.h> (old) htonl:host-to-network long (32bit) htons:host to network short (16bit) &N Bsp;ntohl:network to host Long (32bit)   ntohs:network to host short (16bit)------------------------------------------------------------------------- --------4. Socket address Format#include <netinet/in.h> struct sockaddr { sa_family-t sa_family; char sa_data[]; ... }; struct sockaddr_in { sa_ family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; }; struct in_addr { in_addr_t s_addr; }; example: struct sockaddr_in addr;socklen_t addrlen = sizeof (struct sockaddr_in);addr.sin_family = af_inet;ADDR.SIN_ADDR.S_ADDR = inet_addr ("127.0.0.1");Addr.sin_port = htons (Dtls_local_port);Connect (socket_fd, (struct sockaddr*) &addr, Addrlen);-------------------------------------------------------- -------------------------5. Bind (TCP/UDP server side requires bind) int bind (int sockfd, const struct SOCKADDR *addr, socklen_t len), 1) port number must be greater than 1024, otherwise super User Rights 2) only the server needs bind, the client socket bind does not make sense 3) if Connect/listen is called, but there is no bind, the system will pick one to bind it to Socketint Initserver (int type, const struct SOCKADDR *addr, socklen_t alen, int qlen) { int fd; INT err = 0 ; if (FD = socket (addr->sa_family, type, 0)) <0) return-1; if ( Bind (FD, addr, Alen) < 0) { err = errno; Goto errout;   ; } if (type = = Sock_stream | | type = sock_seqpacket) { if (Listen (FD, Qlen) < 0) { err = errno; Goto errout; }&NBSP; } return fd;errout: Close (FD); errno = err; return-1;} ---------------------------------------------------------------------------------6.connect Connection-oriented sockets connect with connect, no socket is required, but you can also use the function connect for a disconnected Network service (SOCK_DGRAM), if you call connect on a SOCK_DGRAM socket, The destination address of all sending messages is set to the address specified in the Connect call so that no address is required for each transmission of the message, and only messages from the specified address are received. 1) int connect (int sockfd, const struct SOCKADDR *addr, socklen_t len); example: #define MAXSLEEP 128int Connec T_retry (int sockfd, const struct SOCKADDR *addr, socklen_t Alen) { int nsec; for (nsec=1; nsec <= Maxsleep; Nsec <<= 1) { if (Connect (sockfd, addr, alen) = = 0) {return 0; Connection ACCECPT}if (nsec <= MAXSLEEP/2)Sleep (nsec); }Return (-1);} &NBSP;2) int Listen (int sockfd, int backlog) Backlog: Indicates the number of connection requests that the process is queued to server call Listen declare can accept connection request 3) accept int The Accept (int sockfd, struct sockaddr *restrict addr, socklen_t *restrict len); Server calls accepts the connection, if no connection request arrives, Accept will block until a request arrives. If SOCKFD is a non-blocking mode, the Accept return-1 accept returns an FD that is the same as the SOCKFD socket type and address family. The original socket continues to remain listen---------------------------------------------------------------------------------9. Send/receive data 0) read/write data from fd 1) ssize_t Send (int sockfd, const void *buf, size_t NBYTS, int fla GS); socket must be connected when using send, either ignore the destination address, or design the destination address at Connect 2) ssize_t sendto (int sockfd, const void *BUF, size_t Nbyts, int flags, const struct sockaddr *destaddr, socklen-t Destlen); for a non-connected socket, sendto 3) ssize_t sendmsg (int sockfd, const struct MSGHDR *msg, int flags); &NB Sp can specify multiple buffers to transmit datassize_t recv (int sockfd, void *buf, size_t nbytes, int flags); Connection-oriented) ssize_t recvfrom (int sockfd, void *restrict buf, size_t len, int flags, struct sockaddr * Restrict addr, socklen_t *restrict Addrlen);Can be used with no connection socket, if addr is empty, Recvfrom equals recv ) ssize_t recvmsg (int sockfd, struct MSGHDR *msg, int flags); ------ --------------------------------------------------------------------------- 10. Socket option int setsockopt (int sockfd, int level, int option, const void *val, socklen_t len); int getsockopt ( int SOCKFD, int level, int option, const void *val, socklen_t * Restrict LENP); ------------------------- -------------------------------------------------------- 11. Asynchronous fcntl (FD, F_SETFL, O_nonblock); 1) select int Select (int Nfds, fd_set *readfds, Fd_set *writefds, Fd_set *exceptfds, struct timeval *time Disadvantages: 1). Nfds is limited to 1024 &NBSP;2 only). Know that there are events coming, but also to traverse which FD trigger 3). You cannot dynamically modify the Fdset, or turn off a socket 4), and need to maintain a data structure to hold a large number of FD. This allows the user space and kernel space to replicate overhead when passing the structure: (select so old, do we still need it?) ) 1). Better compatible with old systems 2). The Select calculation time-out can reach nanosecond accuracy, while poll, Epoll can only reach millisecondsof precision. Client/server need not be so high, but the embedded system is used. In fact, if you write a no more than 200 socket programs, select and poll, Epool performance is no different. &NBSP;2) poll int poll (struct POLLFD *fds,nfds_t nfds, int timeout), disadvantage: 1). Know that there are events coming, but also to traverse which FD trigger &NBSP;2). Cannot dynamically modify Fdset, or turn off a socket advantage: 1) No FD number limit Usage Scenarios: 1) Multi-platform support, not just Linux, you don't want to use libevent 2) No more than 1000 sockets 3) more than 1000 sockets, but sockets are short-lived &NBSP;4) Your application is not designed the means That it changes the events while another thread was waiting for them 3) Epool (Epoll was Linux only) int Epoll_wai T (int epfd, struct epoll_event * events, int maxevents, int timeout); 1) Your application R UNS a thread poll which handles many network connections by a handful of threads. Single threaded applications using Epool No loss, no better than pool 2) at least 1000 socket,socket 3) Epoll is the Linux kernel for processing large batch file descriptorsThe improved poll is the enhanced version of the Linux Select/poll IO interface, which significantly increases the system CPU utilization of the program in a small number of active concurrent connections. Another reason is that when getting an event, it does not have to traverse the entire set of descriptors that are being listened to , as long as it traverses the set of descriptors that are added to the ready queue by an asynchronous wake-up of kernel IO events. Epoll provides edge triggering (edge triggered) in addition to the level triggered of select/poll io events, which makes it possible for user-space programs to cache IO states, Reduce epoll_wait/epoll_pwait calls to improve application efficiency.
Socket Learning Notes