First, the emergence of the Internet

In October and November 1957, In 1958, President Eisenhower proposed to the U.S. Congress to establish DARPA (Defense Advanced Research Project Agency). In June 1968, DARPA proposed Resource Sharing Computer Networks to connect DARPA computers. The network was called ARPAnet, or ARPAnet. It was the earliest prototype of InterneCopy the code

TCP is divided into two different protocols:

1. Transmission control protocol TCP used to detect errors in network transmission

2. Internet protocol IP that is specially responsible for interconnecting different networks

 

Second, network architecture

The OSI model

TCP/IP protocol

Three, IP

Basic concepts of IP:

An IP address is the identifier of a host on the Internet

A host address on the Internet must have an IP address to communicate with other machines

The IP address can be 32-bit (IPv4) or 128-bit (IPv6).

Each packet must carry a destination IP address and a source address, and the router relies on this information to route the packet

Representation: The common dotted form, such as 202.35.64.12, is eventually converted to a 32-bit unsigned integer

 

IP Address Classification

A total of five categories

(1) Class A 1.0.0.1-126.255.255.254 Government and Reserved (2) Class B 128.0.0.1-191.255.255.254 Large and Medium Enterprises (3) Class C 192.0.0.1-223.255.255.254 Common (4) Class D 224.0.0.0-239.255.255.255 multicast (5) Class E 240.0.0.0-255.255.255.254 experimentCopy the code

 

IP address: 192.168.1.155

Network address + host address

Special IP:

An address (” 0.0.0.0 “) where each byte is 0 corresponds to the current host address: INADDR_ANY

127.0.0.1 to 127.255.255.255 Used for loop testing. For example, 127.0.0.1 can represent the local IP address. You can use http://127.0.0.1 to test the web server configured on the local server

The IP address where each bit of the host address is 1 is the broadcast address of the current subnet

An IP address in which each bit of the network part is 1 and each bit of the host part is 0 is a subnet mask

In the IP address, the host part is 1, xxx.XXX.XXX.1 this is the gateway address, router address

 

Subnet mask:

255.255.255.0 This is the address of the subnet mask (class C).

An address in which all the network parts are ones and all the host parts are zeros is a subnet mask

 

Port number:

To further distinguish which process a host should forward packets to for processing, use the port number

(1) 1-1023 (Ports 1-255 are well-known ports, and ports 256 to 1023 are usually occupied by UNIX systems)

(2) Registered port: 1024 to 49150

(3) Dynamic or private port: 49151 to 65535

 

Byte order:

Data transmitted on the network must be in network byte order: big endian

 

The socket socket

7 file types – D, P, C, B, L, S

Six interprocess communication modes pipes, shared memory, semaphores, signals, message queues, sockets

Process communication interface, special file descriptor

Socket classification

(1) Stream socket (TCP socket) (SOCK_STREAM)

Provides a connection-oriented, reliable data transfer service that sends data error-free, duplicate-free and receives data in the order it is sent. Set the flow control to prevent the data flow from drowning the slow receiver. Data is treated as a byte stream with no length limitation.Copy the code

(2) Datagram socket (UDP socket) (SOCK_DGRAM)

Provides connectionless service. Data packets are sent as independent packets without error assurance. Data may be lost or repeated. Data may be sent sequentially or received out of order.Copy the code

(3) Raw socket (SOCK_RAW)

It can directly access lower-level protocols such as IP and ICMP.Copy the code

 

Socket programming often has an API

TCP and UDP communication are related

Socket () creates a socket

Bind () binds the local address and port

Connect () establishes a connection

Listen () Sets the listening port

Accept () accepts the TCP connection

Recv (), read(), recvfrom() Data received

Send (), write(), sendto() Data is sent

Close (), shutdown() closes the socket

Network address translation function

#include <sys/socket.h>

#include <netinet/in.h>

#include <arpa/inet.h>

int inet_aton(const char *cp, struct in_addr *inp);

in_addr_t inet_addr(const char *cp);

in_addr_t inet_network(const char *cp);

char *inet_ntoa(struct in_addr in);

struct in_addr inet_makeaddr(in_addr_t net, in_addr_t host);

in_addr_t inet_lnaof(struct in_addr in);

in_addr_t inet_netof(struct in_addr in);
Copy the code

Gets and sets socket properties

#include <sys/types.h>          /* See NOTES */

#include <sys/socket.h>

 

int getsockopt(int sockfd, int level, int optname,

                      void *optval, socklen_t *optlen);

int setsockopt(int sockfd, int level, int optname,

                      const void *optval, socklen_t optlen);

 

#include <arpa/inet.h>

uint32_t htonl(uint32_t hostlong);
Copy the code

Host byte order is converted to unsigned long integer network byte order

uint16_t htons(uint16_t hostshort);
Copy the code

Host byte order is converted to unsigned short integer network byte order

uint32_t ntohl(uint32_t netlong);

uint16_t ntohs(uint16_t netshort);
Copy the code