Linux Network Programming - Preliminaries

1.Socket

 1.1, socket is an application programming interface, a special file descriptor (IO operations may be performed thereon, open, write, close)

 1.2, socket programming of a resource represents the network

    1.3, socket type

    Stream socket (SOCK_STREAM) : uniquely corresponds TCP

      Providing a connection-oriented, reliable data transmission services, data error-free transmission and reception is repeated according to the order of transmission, the built-in control flow, data flow to avoid

           Submerged slow receiver, the data stream is treated as a byte stream, no length limit.

    Datagram sockets (SOCK_DGRAM) : uniquely corresponding UDP

          Providing the connectionless service, data packets transmitted as a separate packet is not guaranteed to provide error-free data may be lost or repeated transmission order may be received out of order.

    Raw socket (SOCK_RAM) : corresponding to a plurality of protocols, transport layer penetrates transmitted

      As can be IP, ICMP direct access to the lower layer protocol

 

2, IP address

 2.1, IP address is a host of Internet identity 1

  - Internet host must have an IP address to other hosts to communicate with

  - IP Address is 32 bits (IPV4), or 128 (IPV6)

  - Each packet must carry the destination IP address and source IP address, router rely on this information to route packets

   2.2 Representation

  - Common dotted form, such as 196.128.1.2

  - 32-bit integer

  Special IP addresses:

  - LAN IP: 192.xxx.xxx.xxx 10.xxx.xxx.xxx

  - Broadcast IP: xxx.xxx.xxx.255 255.255.255.255 (the whole network Broadcasting)

  - Multicast IP: 224.xxx.xxx.xxx ~ 239.xxx.xxx.xxx

 

3. Port Number

  In order to distinguish a host receives a packet which should be referred to for processing the task, using port numbers to distinguish

  TCP port number and UDP port number of independent

  Port number generally consists of INAN (Internet assigned numbers authority) management

  - known Port: 1--1023 (a well known port 255, port 256 to 1023 are usually occupied UNIX systems)

  - Registration port: 1024 to 50000

  - Dynamic or private port: 50000 to 65535

 

The network communication is determined by the IP address and port number of

 

4. endian

4.1 host byte order

  Refers endian  integer  sequence stored in memory, CPU refers to the different memory access multi-byte data , there is the problem size side.

 (If the CPU is to access a string, the size of the end is not an issue.)

  Host byte order (host-byte) refers to a byte order of the processor to store data.

 eg: 0X 12345678 (0X ------ High Low)

  There are two most common:

  1. Little endian: The starting address in the low-order byte is stored --LE

  2. Big endian: The high-order byte of the start address stored --BE

 In general:

  X86 / ARM: little endian

    Powerpc / mips: big-endian mode

 1 #include <stdio.h>
 2 #include <arpa/inet.h>
 3  
 4 int main(){
 5  
 6                 unsigned long a = 0x12345678;
 7                 unsigned char *p = (unsigned char *)(&a);
 8  
 9                 printf("主机字节序:%0x    %0x   %0x   %0x\n",  p[0], p[1], p[2], p[3]);
10  
11                 unsigned long b = htonl(a);  // host endian byte order converted into network 
12 is                  
13 is                  P = (unsigned char *) (& B);
 14   
15                  the printf ( " Network Byte Order: 0x%%% 0x 0x 0x% \ n- " , P [ 0 ], P [ . 1 ], P [ 2 ], P [ . 3 ]);
 16                  return  0 ;
 . 17          } 
 18 is  ----------------
 . 19 source link: https : // blog.csdn.net/msdnwolaile/article/details/50727653
Verify the host terminal size

 

4.2 Network Byte Order

  Network byte order (network-byte) programming refers to the network, the data byte order. Used when programming the network IP address and port number are required network byte order.

  And "host byte sequence" Instead, the important part of the network byte order of data stored in low address, high unimportant parts stored address.

  Since the byte order machines using different terminal sizes may be different, so that during the transmission of data, the intermediate data format required i.e. a standardized network byte order.

  a normalized individual data storage ------- -------- converted to the native format b   ->     a host network byte order byte order ---------- --------- b host endian

  

4.3 Conversion Functions

  Byte order conversion between the host and the network byte order can be achieved by a set of functions.

  The function h represents the host host, n represents the network networ, l represents long integer long, s represents a short integer short.

1  / * *** host byte order ---> network byte order *** * / 
2 uing16_t the htons (uint16_t Short );  
 . 3  uint32_t htonl (uint32_t hostlong);   
 . 4  
. 5  / * *** Network Byte Order ----> host byte order *** * / 
. 6  uint16_t ntohs (uint16_t netshort);    
 . 7 uint32_t ntohl (uint32_t netlong);     

 

  IP address conversion functions:

. 1 #include <ARPA / inet.h>
 2  
. 3 in_addr_t the inet_addr ( const  char * CP);
 . 4  // parameter net_addr is a: decimal point character string, returns a value of a binary 32-bit network byte order of IPv4 address is otherwise: INADDR_NONE
 . 5  @ features: 1, applies only to the IPV4
 . 6  //            2, error return -1
 . 7  //            3, this function can not be used to convert the 255.255.255.255 
. 8  
. 9  int inet_pton ( int AF , const  char * the src, void * DST);
 10  // parameters: af --- address protocol family (AF_INST and the AF_INET6)
 . 11  //            src-- is a pointer (dotted fill in the form of an IP address, IPV4)
 12 is  //             results dst-- converted to the DST
 13 is  // features: 1, adapted to the IPV4 and IPV6
 14  //             2, correctly handle 255.255. 255.255 conversion of
 15  //             
16 inet_ntop ();

 

 1 #include <stdio.h>
 2 #include <arpa/inet.h>
 3  
 4 int main()
 5 {
 6         struct in_addr ipaddr;
 7         unsigned long addr = inet_addr("192.168.1.100");
 8         printf("addr = %u\n", ntohl(addr));
 9  
10         ipaddr.s_addr = addr;
11         printf("%s\n", inet_ntoa(ipaddr));      
12         return 0;       
13 }
 14  ----------------
 15  source:
 16 HTTPS: // blog.csdn.net/msdnwolaile/article/details/50727653

result:

Guess you like

Origin www.cnblogs.com/y4247464/p/12129398.html