I. Architecture and features of TCP/IP
1. TCP/IP architecture
TCP/IP is actually a complete set of network protocols on the physical network. TCP provides the transport layer service, while IP provides the network layer service. TCP/IP includes the following protocols: (Structure: 1.1)
(Fig. 1.1)
IP: The internetprotocol is used to route data between hosts and store data on the network. The Group Sending service is also provided for ICMP, TCP, and UDP. User processes generally do not need to be involved in this layer.
ARP: Address Resolution Protocol)
This Protocol maps network addresses to hardware addresses.
RARP: Reverse Address Resolution Protocol)
This Protocol maps hardware addresses to network addresses
ICMP: inter-network message Control Protocol (Internet Control Message Protocol)
This protocol handles communication and host errors and transfer control.
TCP: Transmission Control Protocol)
This is a reliable full-duplex byte stream connection-oriented protocol for user processes. It provides virtual circuit services for user processes and establishes a check for reliable data transmission. (Note: Most network user programs use TCP)
UDP)
This is a connection-free protocol provided to the user process for data transmission without performing the correctness check.
FTP: File Transfer Protocol)
Allows users to communicate with another host through file operations (such as file addition, deletion, modification, query, and transfer.
SMTP: Simple Mail Transfer Protocol)
The SMTP protocol is used to send emails between systems.
TELNET: terminal protocol (Telnet terminal procotol)
Allow users to access remote hosts through virtual terminals
HTTP: Hypertext Transfer Protocol (Hypertext Transfer procotol)
TFTP: simple File Transfer Protocol (Trivial File Transfer Protocol)
2. TCP/IP features
The core part of TCP/IP is the transport layer protocol (TCP, UDP), network layer protocol (IP), and physical interface layer, which are usually implemented in the operating system kernel. Therefore, users generally do not. During programming, the programming interface has two forms: one is the system call directly provided by the Internal core; the other is the function provided by the library function. The former is nuclear implementation, and the latter is non-nuclear implementation. User services can only be implemented through application procedures outside the core, so it must be implemented through sockets.
Figure 1.2 shows the relationship between TCP/IP protocol core and applications.
(Fig. 1.2)
Ii. Special terms
1. Socket
It is the basic component of the network. It is a communication endpoint that can be named and addressable. Each socket in use has its type and a process connected to it. The socket exists in the communication area (also known as address cluster. The socket only exchanges data with the socket in the same region (a specific and conversion process must be executed across regions ). In Windows, sockets only support one domain-Internet domain. The socket has a type.
Windows Socket 1.1 supports two types of sockets: stream socket (sock_stream) and datagram socket (sock_dgram)
2. Windows Sockets implementation
A Windows Sockets implementation is a set of software that implements all the functions described in the Windows Sockets specification. Generally, it is implemented through DLL files.
3. Blocking processing routine
Blocking hook is a mechanism provided by Windows Sockets to support blocking socket function calls.
4. Multi-address broadcast (Multicast)
It is a one-to-multiple transmission mode. The transmission initiator transfers the information to a group of recipients after one transmission, and
(Unicast) corresponds to broadcast (broadcast.
I. Client/Server mode
In a TCP/IP network, the host mode in which two processes interact is the client/server model ). The establishment of this mode is based on the following two points: 1. Non-peer effect; 2. Communication is completely asynchronous. In the Client/Server mode, the following request methods are used:
First, the server must start and provide corresponding services according to the request: (the process is as follows)
1. Open a channel and inform the local host that it is willing to receive customer requests at a recognized address.
2. Wait for the customer's request to reach the port.
3. Receive a duplicate service request, process the request, and send a response signal.
4. Return step 2, waiting for another customer's request
5. Disable the server.
Customer:
1. Open a channel and connect to the specific port of the host where the server is located.
2. Send a service request message to the server, wait for and receive the response; Continue to make the request ......
3. Close and terminate the communication channel after the request ends.
Ii. Basic socket
In order to better illustrate the socket programming principles, we will provide several basic sockets, which will be described in more detail in the future.
1. Create a socket-socket ()
Function: Create a new socket
Format: Socket Pascal far socket (int af, int type, int procotol );
Parameter: AF: Communication Region
Type: socket type to be created
Procotol: specific protocol used
2. Specify the local address -- BIND ()
Function: Associate the socket address with the created socket number.
Format: int Pascal far BIND (socket S, const struct sockaddr far * Name, int namelen );
Parameter: s: the socket Descriptor (socket number) returned by the socket () call and not connected ).
Others: no error. BIND () returns 0; otherwise, socket_error
Address structure description:
Struct sockaddr_in
{
Short sin_family; // af_inet
U_short sin_port; // 16-bit port number, network byte sequence
Struct in_addr sin_addr; // 32-bit IP address, in bytes
Char sin_zero [8]; // Reserved
}
3. Establish a socket connection-Connect () and accept ()
Function: complete the connection together.
Format: int Pascal far connect (socket S, const struct sockaddr far * Name, int namelen );
Socket Pascal far accept (socket S, struct sockaddr far * Name, int far * addrlen );
Parameters: Same as above
4. Listener connection-Listen ()
Function: it is used for connection servers and indicates that it is willing to receive connections.
Format: int Pascal far listen (socket S, int backlog );
5. Data Transmission-send () and Recv ()
Function: send and receive data
Format: int Pascal far send (socket S, const char far * Buf, int Len, int flags );
Int Pascal far Recv (socket S, const char far * Buf, int Len, int flags );
Parameter: Buf: pointer to a buffer with transmitted data.
6. multiplexing -- select ()
Function: used to detect the status of one or more sockets.
Format: int Pascal far select (INT NFDs, fd_set far * readfds, fd_set far * writefds,
Fd_set far * contains TFDs, const struct timeval far * timeout );
Parameter: readfds: pointer to read Detection
Writefds: pointer to write detection
Invalid TFDs: pointer to the vulnerability to be detected.
Timeout: Maximum waiting time
7. Disable socket-closesocket ()
Function: Disable socket S.
Format: bool Pascal far closesocket (socket S );
3. Typical Process Diagram
2.1 System Call Sequence Diagram for connection-oriented sockets
2.2 sequence diagram of SOCKET call without connection protocol
2.3 connection-oriented application Flowchart