"Linux Advanced Programming" (13th) Linux Socket Network Programming basics

Source: Internet
Author: User

IP Address definition:

struct in_addr{    __u32 s_addr;};

in_addr_t inet_addr (__const char * __cp) : Converts a dotted decimal IP address string to a 32-bit IP address (Network storage order).

in_addr_t inet_network (__const char * __cp) : Converts a dotted decimal IP address string to a 32-bit IP address (host byte order).

char * inet_ntoa (struct in_addr_in) : Converts the 32-bit network byte order IP address into dotted decimal notation.

int Inet_aton (__const char *__cp, struct in_addr *__inp) : Converts a dotted decimal IP address string to a 32-bit IP address (network byte order). The second parameter is the transform result address. Successful return 0. Same function as the first function.

#include <arpa/inet.h>#include<netinet/inch.h>#include<stdio.h>#include<string.h>#include<sys/socket.h>intMainintargcChar*argv[])    {in_addr_t net; structin_addr net_addr, ret; NET= Inet_addr ("192.168.68.128"); Net_addr.s_addr=net; //convert dotted decimal to IP in networked storage orderprintf"inet_addr (192.168.68.128) = 0x%x\n", Inet_addr ("192.168.68.128")); //convert dotted decimal to host storage sequence IPprintf"inet_network (192.168.68.128) = 0x%x\n", Inet_network ("192.168.68.128")); //convert IP in networked storage order to dotted decimalprintf"Inet_ntoa (NET) =%s\n", Inet_ntoa (NET_ADDR)); Inet_aton ("192.168.68.128", &ret); printf ("Inet_aton ret.s_addr = 0x%x\n", ret.s_addr);}

Based on address type conversions

int Inet_pton (int __af, __const char *__restrict __cp, void *__restrict __buf) : Will be stored in the starting position of CP, address protocol type AF The dotted decimal address is converted to BUF. If the ipv4,buf should be of type in_addr, if the ipv6,buf should be in6_addr type.

char * inet_ntop (int __af, __const void *__restrict __cp, char *__restrict __buf, socklen_t __len) : Network The IP address of the byte order store is converted to dotted decimal. If the IPV4,CP should be of type in_addr, if the IPV6,CP should be in6_addr type.

#include <arpa/inet.h>#include<netinet/inch.h>#include<stdio.h>#include<string.h>#include<sys/socket.h>intMainintargcChar*argv[])    {in_addr_t net; structin_addr net_addr, ret; Charbuf[ -]; Inet_pton (Af_inet,"192.168.68.128", &ret); Inet_ntop (Af_inet,&ret, BUF, -); printf ("buf =%s\n", buf);}

in_addr_t inet_lnaof (struct in_addr __in) : Extracts the host ID from the 32-bit network sequential IP address.

in_addr_t inet_netof (struct in_addr __in) : Extracts the network ID from the 32-bit network sequential IP address.

struct in_addr inet_makeaddr (in_addr_t __net, in_addr_t __host) : synthesizes a host ID and network ID into an IP address.

IP Datagram Header data structure definition:

TCP Header information structure

UDP header information Structure

Size End Principle

An integer 0x12345678 12 is a high byte, and 78 is a low byte

CPU processing data There are two ways of big-endian

Small End mode : High-byte on high address

Big- endian mode : High byte in low address

To detect the current system byte order:

#include <stdio.h>#include<endian.h>int main (void) {    printf ( " big-endian:\t%d\nlittle-endian:\t%d\nmine:\t%d\n " , __big_endian, __little_endian, __byte_order);     return 0 ;}

Common body detection system size end

For structural bodies

Union word{    int  A;     Char b;} C;

B will be stored in low-byte

#include <stdio.h>#include<stdlib.h>Union word{intA; Charb;} C;intCHECKCPU (void) {C.A=1; return(C.B = =1);}intMainvoid){    inti; I=checkcpu (); if(i = =0) printf ("This is big_endian\n"); Else if(i = =1) printf ("This is little_endian\n"); return 0;}

byte order conversion function

unsigned long int ntohl (unsigned long int) : Long network byte order to host byte order

unsigned long int htonl (unsigned long int) : Long host byte order to network byte order

unsigned short int ntohs (unsigned short int) : Short network byte order to host byte order

unsigned short int htons (unsigned short int) : Short host byte order to network byte order

Network programming Unified use of big-endian mode!

----------------------------------------------------------------------------

the use of #pragma pack (1) for interstitial points

Sets the boundary alignment of the struct to 1 bytes, which means that all data is stored continuously in memory.

For example, you define the following structure in the C language:

struct S {
Char ch;
int i;
};

Then write a sentence in the main function: printf ("%d", sizeof (struct s))

That is, the number of bytes that the output struct s occupies.

How much do you think the output will be?

We know that the char type occupies 1 bytes and the int is 4 bytes, so the output is 5?

The answer is in the negative. You can try it yourself and the output is 8.

Why is that? This is because the compiler optimizes the storage of the structure in order to keep the program running faster, reducing the instruction cycle of CPU read data. In fact, the first Char member, although originally only 1 bytes, actually takes up 4 bytes, so that the address of the second int member can be divisible by 4. So the actual consumption is 8 bytes.

The #pragma pack (1) allows the compiler to force the structure data to be continuously arranged so that the result of the sizeof (struct s) output is 5.

------------------------------------------------------------------------------------------------

The processing of the uniform conversion size end at the time of data transfer:

1. Single byte data, do not need to convert the size end. such as char buf[] = "Hello"

2. Multi-byte data, converting functions in byte order

3. Custom structure: If both parties know a member of the struct body, a member can be converted

can also be processed with #pragm Pack (1), so that the two sides of the same byte alignment, the overall conversion.

Linux Advanced Programming (13th) Linux Socket Network Programming Basics

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.