Winsock Study notes (2)

Source: Internet
Author: User
Tags error code port number

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.