Some socket Structures

Source: Internet
Author: User

I.SocketSome basic structures

1.32BitIPAddress:

Struct in_addr

{

Unsigned long s_addr;

}

2.TCP/IPThe format of the socket address used by the Internet communication domain:

Struct sockaddr_in

{

Short int sin_family ;//Indicates the address cluster,TCP/IPRequiredAf_inet

Unsigned short int sin_port ;//Indicates the port number.

Struct in_addr sin_addr ;//Indicates32BitIPAddress,In_addrStructure Representation

Unsigned char sin_zero [8]; //Indicates filling all0, Guarantee andSockaddrSame size

}

II.SocketSome auxiliary functions

1. Byte sequence conversion function:

· Htons: host to network byte order, short (unsigned) Integer

· Htonl: host to network byte order, long (unsigned) Integer

· Ntohs: Network to host byte order, short (unsigned) Integer

· Ntohl: Network to host byte order, long (unsigned) Integer

Memory method:

H indicates host, N indicates network, L indicates long, s indicates short

For example:

Num = htons (port_num );

Converts a port number named port_num to the byte sequence of the network and assigns the value to the num variable.

2. IP address conversion function:

· Inet _ ADDR ()

Converts an IP address string in dotted decimal notation to a 32-bit unsigned integer. This integer is already

Network byte order, no need to call htonl ()

· Inet _ ntoa ()

Converts a 32-bit unsigned integer into an IP address string in the dotted-decimal notation. This function accepts

An in_addr structure is used as a parameter to store the returned string in another static buffer.

The result of the last call will be changed the next time you call this function.

3. Functions for searching host information:

Struct hostent * gethostbyname (const char * Name );

Parameter: the host name to be resolved when you enter the parameter. It can also be a domain name.

Return Value: returns a pointer to the hostent structure that describes the host information.

Struct hostent

{

Char * h_name; // host name

Char ** h_aliases; // host alias

Int h_addrtype; // host address type

Int h_length; // address Length

Char ** h_addr_list; // storage host address Array

# Define h_addr h_addr_list [0] // backward compatible

};

For TCP/IP, the value of h_addrtype is equal to af_inet, and the value of h_length is 4 because the IP address is 4 bytes. H_addr_list is essentially a buffer for storing addresses.CodeTo use this buffer zone, you need to perform forced type conversion.

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.