Introduction to some functions related to sockets
1, read the current error value: Every time an error occurs, if you want to deal with specific problems, then you should call this function to get the error code.
int WSAGetLastError(void );
#define h_errno WSAGetLastError()
Error value please read Winsock2.h yourself.
2. Converts the host's unsigned Long value to the network byte order (32-bit): Why do you do this? Because different computers use different byte orders to store data. Therefore, any reference to the IP address and port number from the Winsock function and the IP address and port number passed to the Winsock function are organized in the order of the network.
u_long htonl(u_long hostlong);
举例:htonl(0)=0
htonl(80)= 1342177280
3, the unsigned long number from the network byte order to convert the bit host byte order, is the function of the above inverse function.
u_long ntohl(u_long netlong);
举例:ntohl(0)=0
ntohl(1342177280)= 80
4. Convert the unsigned short value of the host to network byte order (16-bit): Reason 2:
u_short htons(u_short hostshort);
举例:htonl(0)=0
htonl(80)= 20480
5, the unsigned short number from the network byte order to convert bit host byte order, is the function of the above inverse function.
u_short ntohs(u_short netshort);
举例:ntohs(0)=0
ntohsl(20480)= 80
6. The address of a IN_ADDR structure will be converted by point-splitting IP address, the definition of this structure see note (i), is actually a unsigned long value. The computer handles IP addresses internally but does not recognize data such as 192.1.8.84.
unsigned long inet_addr( const char FAR * cp );
举例:inet_addr("192.1.8.84")=1409810880
inet_addr("127.0.0.1")= 16777343
If an error occurs, the function returns the Inaddr_none value.
7, the network address conversion bit by point division of the IP address, is the function of the above inverse function.
char FAR * inet_ntoa( struct in_addr in );
举例:char * ipaddr=NULL;
char addr[20];
in_addr inaddr;
inaddr. s_addr=16777343;
ipaddr= inet_ntoa(inaddr);
strcpy(addr,ipaddr);
So the addr value becomes 127.0.0.1.
Be careful not to modify the return value or to perform a release action. If the function fails, a null value is returned.