Winsock is a protocol-independent interface. Here we mainly introduce the widely used IP protocol, whose version is the current IPv4 protocol (not introduced for the time being ).
IPv4 addressing
In IPv4, the address of a computer is expressed in a 32-bit binary format, which is called an IP address. when a client uses TCP or UDP to communicate with the server, its IP address and communication port must be specified. when the server wants to listen to client requests, it must also specify the IP address and port. use in WinSockSet the information in the sockaddr_in structure:
Struct sockaddr_in {short sin_family; u_short sin_port; struct in_addr sin_addr; char sin_zero [8];};
Sin_family must beAf_inetSet the IP address family for Winsock.
Sin_port DefinitionTCP or UDP communication port. The port should be carefully selected. some well-known services occupy specific ports, such as FTP and HTTP
Sin_addr records the 4-byte address of IPv4, which is an unsigned long integer. b. c. d "indicates that each letter represents a number corresponding to each byte of the IP address (from left to right)
Sin_zero is only used to occupy byte SpaceSockaddr_in structure andThe sockaddr structure is the same.
FunctionInet_addr can convert an IP address in the format of "A. B. C. D" to a 32-bit unsigned long integer.
Unsigned long inet_addr (const char far * CP );
CP is an IP address in the string format that points to the end of null. It returns the IP address in the network character order.
Byte order
Processors of different computers use big-Endian or little-Endian to represent numbers. the IP addresses and ports must be represented in multiple bytes. The order of representation on different computers is called the host byte sequence. the representation of IP addresses and ports in the network should not be different. It must be expressed in the big-Endian format, which is called the network byte sequence.
The following Winsock API can convert the host byte sequence to the network byte sequence:
U_long htonl (u_long hostlong );
Int wsahtonl (socket S, u_long hostlong, u_long far * lpnetlong );
U_short htons (u_short hostshort );
Int wsahtons (socket S, u_short hostshort, u_short far * lpnetshort );
The next API converts the network byte sequence to the host byte sequence:
U_long ntohl (u_long netlong );
Int wsantohl (socket S, u_long netlong, u_long far * lphostlong );
U_short ntohs (u_short netshort );
Int wsantohs (socket S, u_short netshort, u_short far * lphostshort );
BelowCodeDescribes how to use the sockaddr_in structure to set the address and port:
Sockaddr_in internetaddr;
Int nportid = 5150;
Internetaddr. sin_family = af_inet;
// Convert the IP address represented by the string to an integer represented by four bytes and assign the value to sin_addr
Internetaddr. sin_addr.s_addr = inet_addr ("136.149.3.29 ");
// The nportid variable is stored in the host byte sequence. nportid is converted to the network byte sequence and assigned to sin_port.
Internetaddr. sin_port = htons (nportid );
The IP address is not so easy to remember. You can use a more friendly host name to replace the IP address. For example, www.somewebsite.com uses FTP to represent the file transfer protocol port number 21. related functions include getaddrinfo, getnameinfo, gethostbyaddr, gethostbyname, gethostname, getprotobyname, getprotobynumber, get-servbyname, and getservbyport. asynchronous versions of these functions include: wsaasyncgethostbyaddr, wsaasyncgethostbyname, wsaasyncgetprotobyname, wsaasyncgetprotobynumber, wsaasyncgetservby-name, and wsaasyncgetservbyport.