Socket programming- addr_in struct operation

Source: Internet
Author: User
Tags htons

Sockaddr struct Sockaddr's defect: struct sockaddr is a general address structure, which aims to unify the expression of the address structure and interface functions so that different address structures can be bind (), connect () and other function calls; sa_data merges the target address and port information
Struct sockaddr {
Unsigned short sa_family;
Char sa_data [14];
};
Sa_family is the communication type, and the most common value is "af_inet"
Sa_data14 bytes, including the destination address and port information in the socket


Sockaddr_in struct: In in struct sockaddr_in represents the Internet, which is the network address. This is a common address structure and belongs to the af_inet address family.
The sockaddr_in struct solves the sockaddr defect and stores the port and ADDR separately in two variables.
Struct sockaddr_in {
Short int sin_family;
Unsigned short int sin_port;
Struct in_addr sin_addr;
Struct in_addr {
Unsigned long s_addr;
}

Unsigned char sin_zero [8];
}

Both sin_port and sin_addr must be nbo
Generally, visualized numbers are HBO (local byte order)

The initial value of sin_zero should be set to zero using the function bzero.
The following statement is generally used:
Struct sockaddr_in cliaddr;
Bzero (& cliaddr, sizeof (cliaddr ));


Basic configuration of sockaddr_in struct Variables
Struct sockaddr_in ina;

Bzero (& Ina, sizeof (INA ));

Ina. sin_family = af_inet;

Ina. sin_port = htons (23 );
Ina. sin_addr.s_addr = inet_addr ("132.241.5.10 ");


Relationship between sockaddr and sockaddr_in
Generally, after assigning values to the sockaddr_in variable, forcibly convert the type and pass in the function that uses sockaddr for parameters.
    • Sockaddr_in is used for socket definition and assignment.
    • Sockaddr is used for function parameters.

The most typical source and target node socket Definition
For the source, destination, and Source and Destination Address ports, two socket variables need to be created.
Bind the cliaddr source address and Source Port
Set the destination address and destination port for connect and sendto.
struct sockaddr_in servaddr, cliaddr;
create_socket (char * server_addr_string, unsigned int server_port)
{< br> source socket assignment
bzero (& cliaddr, sizeof (cliaddr);
cliaddr. sin_family = af_inet;
generally, the source address and port of TCP/UDP protocol are random.
cliaddr. sin_addr.s_addr = htons (inaddr_any);
cliaddr. sin_port = htons (0);
destination socket value assignment
bzero (& servaddr, sizeof (servaddr);
servaddr. sin_family = af_inet;
inet_aton (server_addr_string, & servaddr. sin_addr);
servaddr. sin_port = htons (server_port);
}


Network byte order (nbo)
Both sin_port and sin_addr of the struct must be nbo.

Host byte order HBO
Generally, visual numbers are HBO.

Nbo and HBO Conversion
Inet_addr () converts the string point format address to an unsigned long integer (unsigned long s_addr ;)
Inet_aton () converts the string point format address to nbo
Inet_ntoa () converts the nbo address into the string point format
Htons () "host to network short"
Htonl () "host to network long"
Ntohs () "network to host short"
Ntohl () "network to host long"
Htons () is commonly used. inet_addr () exactly corresponds to the port type and address type of the struct.

Three methods to assign an address to a socket
Inet_aton (server_addr_string, & myaddr. sin_addr );
Myaddr. sin_addr.s_addr = inet_addr ("132.241.5.10 ");
Inaddr_any
Myaddr. sin_addr.s_addr = htons (inaddr_any );
Myaddr. sin_addr.s_addr = inaddr_any;


Two methods to assign port values to socket
# Define myport 3490
Myaddr. sin_port = htons (myport );
0 (random port) switch to nbo casually
Myaddr. sin_port = htons (0 );
Myaddr. sin_port = 0;


Numbers such as htons/L and ntohs/l cannot be used for address translation. Because the addresses are in point format, the addresses can only be converted using numbers/strings, such as inet_aton and inet_ntoa;
The only htons that can be used for address translation is for inaddr_any.
Cliaddr. sin_addr.s_addr = htons (inaddr_any)

Differences between inet_addr () and inet_aton ()
    • Inet_addr () is the return value type.
Struct sockaddr_in ina;

Ina. sin_addr.s_addr = inet_addr ("132.241.5.10 ");
    • Inet_aton () is a parameter pointer type
Struct sockaddr_in ina;

Inet_aton ("132.241.5.10", & Ina. sin_addr );


Inet_ntoa converts the nbo address into the string point format
Parameter: struct variable. sinaddr
Return Value: String pointer
A1 = inet_ntoa (INA. sin_addr );
Printf ("address 1: % s \ n", A1 );
Address 1: 132.241.5.10


Inet_addr () defect:-1 must be detected.
Because the result of inet_addr () is an integer,-1 is returned when an error occurs.
Ina. sin_addr.s_addr is of the unsigned long type.
-1 is displayed as 111111111 in long short, which is consistent with the IP address 255.255.255.255! The broadcast address is mistaken!

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.