Socket-related functions

Source: Internet
Author: User
1. Read the current error value: When an error occurs, if you want to handle the problem, you should call this function to obtain the error code.

      int  WSAGetLastError(void );      #define h_errno   WSAGetLastError()

Read winsock2.h for the error value.

2. Convert the host's unsigned long value to the network byte sequence (32 bits): Why? Because different computers use different bytes to store data. Therefore, any reference from the Winsock function to the IP address and port number and the IP address and port number sent to the Winsock function are organized in the network order.

U_long htonl (u_long hostlong); example: htonl (0) = 0 htonl (80) = 1342177280

3. Converting the unsigned long number from the byte sequence of the network to the host byte sequence is the inverse function of the above function.

U_long ntohl (u_long netlong); example: ntohl (0) = 0 ntohl (1342177280) = 80

4. Convert the host's unsigned short value to the network byte order (16 bits) for the same reason as 2:

U_short htons (u_short hostshort); example: htonl (0) = 0 htonl (80) = 20480

5. Converting the unsigned short number from the network byte order to the host byte order is the inverse function of the above function.

U_short ntohs (u_short netshort); example: ntohs (0) = 0 ntohsl (20480) = 80

6. Convert the IP address separated by vertices into an in_addr structure address. for the definition of this structure, see note (1), which is actually an unsigned long value. The computer processes IP addresses internally, but does not know data such as 192.1.8.84.

Unsigned long inet_addr (const char far * CP); example: 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. Convert the network address into IP addresses separated by vertices, which is the inverse function of the above function.

Char far * inet_ntoa (struct in_addr in); example: char * ipaddr = NULL; char ADDR [20]; in_addr inaddr; inaddr. s_addr = 16777343; ipaddr = inet_ntoa (inaddr); strcpy (ADDR, ipaddr );

In this way, the ADDR value is changed to 127.0.0.1.
Do not modify the return value or release it. If the function fails, the return value is null.

8. Obtain the local address structure of the socket:

Int getsockname (socket S, struct sockaddr far * Name, int far * namelen); s indicates that the socket name is the address value obtained after the function call. namelen indicates the buffer size.

9. Obtain the end address structure connected to the socket:

Int getpeername (socket S, struct sockaddr far * Name, int far * namelen); s indicates that socket name is the end address value obtained after function call. namelen indicates the buffer size.

10. Obtain the computer name:

Int gethostname (char far * Name, int namelen); Name Is the buffer where the computer name is stored; namelen is the buffer size usage: Char szname [255]; memset (szname, 0,255 ); if (gethostname (szname, 255) = socket_error) {// error handling} returned value: sznmae = "Xiaojin"

11. Obtain the host address based on the computer name:

Struct hostent far * gethostbyname (const char far * Name); name indicates the computer name. Usage: hostent * Host; char * IP; host = gethostbyname ("Xiaojin"); If (host-> h_addr_list [0]) {struct in_addr ADDR; memmove (& ADDR, host-> h_addr_list [0], 4); // obtain the standard IP address = Inet _ ntoa (ADDR);} The returned value is: hostent-> h_name = "Xiaojin" hostent-> h_addrtype = 2 // af_inet hostent-> length = 4 IP = "127.0.0.1"

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.