Sockets are abstractions of communication endpoints
Create a socket
#include <sys/types.h><sys/socket.h>int sockets (intint int) protocol); return value: Success file (socket) descriptor, failed -1
Domain: The Protocol field, also known as the Protocol Family (family). Common protocol families are af_inet, Af_inet6, af_local (or Af_unix,unix domain sockets), Af_route, and so on. The protocol family determines the socket address type, must use the corresponding address in the communication, such as Af_inet decided to use the IPv4 address (32 bits) and the port number (16 bit) combination, Af_unix decided to use an absolute path name as the address. Type: Specifies the socket type. Common socket types are, Sock_stream, Sock_dgram, Sock_raw, Sock_packet, Sock_seqpacket, and so on (what are the types of sockets?). )。 Protocal: Therefore, the name of the idea is to specify the agreement. Commonly used protocols are, IPPROTO_TCP, IPPTOTO_UDP, IPPROTO_SCTP, IPPROTO_TIPC and so on they correspond to TCP transport protocol, UDP Transmission protocol, STCP Transmission protocol, TIPC transmission protocol respectively.
Socket communication is bidirectional. You can disable I/O for a socket
#include <sys/socket.h>int shutdown (intint how ); return value: Success 0, error 1 SOCKFD: Socket descriptor How: three kinds of shut_rd ( 0 ) Turn off read function on SOCKFD shut_wr (1) Turn off write function on sockfd shut_rdwr (2) turn off SOCKFD Read and write function
Functions for converting between processor byte order and network byte order
#include <arpa/inet.h>uint32_t htonl (uint32_t hostlong);
Return value: 32-bit integer uint16_t htons (uint16_t hostshort) expressed in network byte order;
Return value: 16-bit integer expressed in network byte order
uint32_t Ntohl (uint32_t netlong);
Return value: 32-bit integer represented in host byte order
uint16_t Ntohs (uint16_t netshort);
Return value: 16-bit integer represented in host byte order
h denotes host byte order, n denotes network byte order, l denotes long integer, s denotes short integer
Print out an address format that can be understood by a person rather than by a computer. Supports both IPV4 and IPV6 addresses
#include <arpa/inet.h>constchar *inet_ntop (intconstvoid Char *DST, socklen_t size); return value: Successful address string pointer, error NULLAF: Address family, support only Af_inet and AF_INET6SRC: Source address structure Pointer DST: receive converted string size: Holds the buffer size of the text string int inet_pton (intconstcharvoid *DST); Return value: Success 1, invalid format 0, error -1AF: address cluster, support only af_inet and Af_inet6src: Converted address string DST: Translated address structure pointer
Find host information for a given computer system
#include <netdb.h>struct hostent *gethostent (void); return value: Pointer returned successfully, error nullvoid sethostent (int stayopen);
Stayopen:true is TCP, otherwise UDPvoid endhostent (void);
Ability to use a similar set of interfaces to get network names and network numbers
#include <netdb.h>structint type); struct netent *getnetbyname (constChar *name); struct netent *getnetent (void); return value: Pointer returned successfully, error nullvoid setnetent (int stayopen); void endnetent (void);
Mapping between protocol name and protocol number
#include <netdb.h>struct protoent *getprottobyname (constChar *name); struct protoent *getprotobynumber (int proto); struct protoent *getprotoent (void); return value: Pointer returned successfully, error nullvoid setprotoent ( int stayopen); void endprotoent (void);
Services are represented by the port number portion of the address, and each service is supported by a unique, well-known port number. You can use Getservbyname to map a service name to a port number, use the function Getservbyport to map a port number to a service name, and use the function getservent order to scan the service database.
#include <netdb.h>struct servent *getservbyname (constcharconstChar *proto); struct servent *getservbyport (intconstChar *proto); struct servent *getservent (void); return value: Pointer returned successfully, error nullvoid setservent ( int stayopen); void endservent (void);
Map a host and a service name to an address
#include <sys/socket.h><netdb.h>int getaddrinfo (constchar ConstChar *service, conststruct addrinfo *hints, struct addrinfo * *res); return value: Success 0, error not 0 error code void freeaddrinfo (struct Addrinfo *res);
Addrinfo Structural Body
structaddrinfo{intAi_flags; intai_family;//Af_inet,af_inet6,unix etc intAi_socktype;//Stream,datagram,raw intAi_protocol;//ipproto_ip, Ipproto_ipv4, Ipproto_ipv6 etcsize_t Ai_addrlen;//Length of Ai_addr Char* AI_CANONNAME;//Full hostname structsockaddr* ai_addr;//Addr of host structaddrinfo*Ai_next;}
The error code needs to be called to convert the function into an error message
#include <sys/types.h><sys/socket.h><netdb.h>constChar * Gai_strerror (int errcode);
Convert an address to a hostname and a service name
#include <sys/socke.h><netdb.h>int getnameinfo (conststruct SOCKADDR *SA, socklen_t salen, Char *host, socklen_t Hostlen, Char int flags);
SOCKADDR Structural Body
struct sockaddr{ __sockaddr_common (sa_); /* Common data:address Family and length. Protocol Family */ char sa_data[]; /* Address data. Address + port number */};
Linux IPC sockets