Edited using VS2008 as the editor. As follows:
/* Value-result parameter (Value-result), when the process passes the kernel a value when it returns, the kernel tells the process a result from the process to the kernel (socklen_t Addrlen): Bind, Connect, sendto from the kernel to the process (socklen_t * Addrlen): Accept, Recvfrom, GetSockName, Getpeername big-end byte sequence, the big end mode: High and low, and small-side mode (SPARC, Linux for the terminal mode, other to be verified) small-endian byte-order, Small-end mode: jagged, high address storage high byte, low address save low address. (The specific processor uses which mode, according to the manual.) The General IA architecture is a small-end pattern. Others to be verified)-----Note: The above definitions in the architecture are not accurate, and you should find out that both processors, or operating system-determined byte sequences are called host bytes. Network information transmission uses the network byte order, the Internet protocol uses the big-endian byte order as the network byte order. * * #include <netinet/in.h>/* h-host, to, N-network, S-short,l-long * * uint16_t htons (uint16_t); uint32_t htonl (uint32_t); uint16_t Ntohs (uint16_t); uint32_t Ntohl (uint32_t); uint16_t--unsigned 16-bit integer, other analogical inference can be #include <arpa/inet.h>/** below three functions, applied in processing IPV4 dotted decimal string **/int Inet_aton (const char * Strptr,struct in_addr *addrptr)//Converts the C string referred to in StrPtr to a 32-bit network byte order and is stored by a pointer addrptr. Success 1, failure-1. The function is not written to the official document int_addr_t inet_addr (const char *strptr);//Translate ditto, there are problems IP 255.255.255.255 can not handle, there are potential problems. has been abandoned, not mentioned. Char *inet_ntoa (struct in_addr inaddr); In contrast to the inet_addr conversion, the return value resides in static memory, he is not reentrant function #include <arpa/inet.h>/** The following two functions, Used in processing IPV4 and Ipv6,p-presentation expression, n-numeric value **/int Inet_pton (int family,const char *strptr,void *addrptr);//Convert strptr string, addrptr store binary result const char *inet_ntop (int family,const void *addrptr,char *strptr,size_t len)//numeric format converted to expression format, Len is the size numeric format of the target storage cell: in_addr{}, in6_addr{} expression format: Dotted decimal ipv4,128 bit IPV6 equivalent code Inet_pton (af_inet,str,&foo.sin_addr); <=>foo.sin_addr.s_addr = INET_ADDR (str); Char Str[inet_addrstrlen]; ptr = Inet_ntop (af_inet,&foo.sin_addr,str,sizeof (str)); <=>inet_ntoa (FOO.SIN_ADDR); #include <sys/socket.h>/* Family: Protocol family (Protocol domain/address family), Af_xxx,eg, Af_inet (IPV4), Af_inet6 (IPV6), af_local (Af_unix, UNIX domain protocol, af_route (routing sockets), Af_key (Key sockets) Socktype: Socket type, eg,sock_packet (Linux Data Link layer) SOCK_STREAM (byte socket, TCP), Sock_ Dgram (datagram sockets, UDP), Sock_seqpacket (ordered packet sockets, SCTP), Sock_raw (original socket, IP) Protocol: protocol type constant, eg, 0 (Select the system defaults for the given family and socktype combinations, most commonly), IPPROTO_TCP (TCP Transport Protocol), IPPROTO_UDP (UDP Transport Protocol), IPPROTO_SCTP (SCTP Transfer Protocol)/INT socket (int family,int socktype,int protocol); /* Excitation of the three-way handshake, generally always and client-initiated, its exceptional situation 1.TCP client did not receive the server to the SYN section of the ACK section, timeout retransmission syn pointsSection. Finally did not receive ACK, then returned etimedout error, 2.TCP customer received the RST section, indicating that the server on the corresponding port no process waiting, that is, no service. Hard error (hard error) returns econnrefused error. The RST section is a decomposition that is sent when a TCP error occurs. The condition is one of the following: Syn has arrived for a port with a destination, but the port does not have a server on which it is listening; TCP wants to cancel an existing connection; TCP receives section 3 on a connection that does not exist at all. A client-issued SYN section causes a Destination unreachable ICMP error, soft error, in the intermediate router. Returns a Ehostunreach or Enetunreach error. * * int connect (int sockfd,const struct sockaddr *servaddr,socklen_t addrlen); /* Assign a socket to a local protocol address, when a client or server does not call bind, when calling connect or listen, the kernel chooses a temporary port for the socket, and the server generally specifies its well-known port, but the RPC server usually chooses a temporary port from the kernel IPV4 Address: Inaddr_any IPV6 Address: in6addr_any inaddr_any_init/int bind (int. sockfd,struct sockaddr *myaddr,socklen_t Addrlen); /* The socket function is assumed to be an active socket when it is created, and the LISTEN call converts the disconnected socket to a passive socket; TCP Status: Closed-->listen backlog: The maximum number of connections the kernel should queue for the corresponding socket. The kernel maintains two queues for listening sockets and does not complete the connection queue: No three-step handshake, in SYN_RCVD state completed connection queue: three-step handshake has been completed, in established state two teams Liezhi and not more than backlog, Backlog there are subtle areas that need to be studied. * * int Listen (int sockfd,int backlog); /* Returns the next completed connection from the completed queue header, the queue is an empty process sleep (assuming socket bit blocking) SOCKFD is called a listening socket, and the return value is called a connected socket/int accept (int sockfd,struct sockaddr *CLIADDR , socklen_t *addrlen); int close (int sockfd);//Turn off socket/*SOCKADDR_STORAGE address structure can host any socket address structure supported by the system */int getsockname (int sockfd,struct sockaddr * localaddr,socklen_t *addrlen); Returns the local protocol address int getpeername (int sockfd,struct sockaddr *peeraddr,socklen_t associated with the SOCKFD socket) *addrlen)//Returns the foreign protocol address associated with the SOCKFD socket