Socket_Internet namespace, socket_internet
Currently, Internet supports two address formats: 1, IPv4 (32-bit address format) 2, and IPv6 (128-bit address format ). The IPv4 namespace is PF_INET, And the IPv6 namespace is PF_INET6.
#incldue <sys/socket.h>int PF_INETint PF_INET6
The socket address in the Internet namespace has the following two components:
1. Address of the machine to be connected: There are several Representation Methods: 1. Internet Address Formats; 2. Host Addresses; 3. Host Names
2. Port Number
Both the address and port number must follow a standard format called the network byte sequence.
Internet Socket Address Formats:
# Include <netinet/in. h> struct sockaddr_in {sa_family_t sin_family, // specifies the address family of the socket address used. AF_INET struct in_addr sin_addr in the Internet, // host address (IP address, or host name) unsigned short int sin_port // port is good} struct destination {destination sin6_family, struct in6_addr sin6_addr, unint32_t sin6_flowinfo, // unimplemented part ...... Unit16_t sin6_port ,}
Host addresses and host name:
A host address is a common IP address, such as an IPv4 address 128.52.46.32. IPv6 address
5f03: 1200: 836f: c100: 1.
Data Type of the host address: IPv4 address occupies 32 bits. It is best to use unit32_t or struct in_addr.
IPv6 occupies 128 bits, and struct in6_addr is used.
# Include <netinet/in. h> struct in_addr {uint32_t s_addr;} uint32_t INADDR_LOOPBACK // the specified address 127.0.0.1uint32 _ t INADDR_ANY // represents any access address. Uint32_t INADDR_BROADCAST // broadcast address uint32_t INADDR_NONE // identify error struct in6_addr struct in6_addr in6addr_loopback // indicates the Ipv6 loop address :: 1IN6ADDR_LOOPBACK_INIT // equivalent to struct in6_addr in6addr_any // IPV6 constant address '::'. Indicates any access address, IN6ADDR_ANY_INIT // equivalent
Each computer can have one or more host names, such as www.gnu.org.
Two macros are provided for the port number.
#include <netinet/in.h>int IPPORT_RESERVEDint IPPORT_USERRESERVED
The port number is smaller than IPPORT_RESERVED and is retained to a specific application or protocol. If the port number is greater than IPPORT_USERRESERVED, it is used for display and is not automatically allocated.
If no IP address is specified for your socket, the system automatically applies for a port between IPPORT_RESERVED and IPPORT_USERRESERVED.