What is the TCP/IP protocol?

78. TCP/IP协议是什么?

TCP/IPA protocol is a set of network protocols used for Internet communication, which defines the transmission methods and rules of data in the network. As a front-end engineer, understanding TCP/IPprotocols is very important for understanding network communication principles and debugging network problems. This article will introduce TCP/IPthe concept, main components and working principle of the protocol to help front-end engineers get started quickly.

What is the TCP/IP protocol?

TCP/IP(Transmission Control Protocol/Internet Protocol)It is a set of network communication protocols, which consists of two independent protocols: TCP(Transmission Control Protocol) and IP( InternetProtocol). TCP/IPThe protocol suite is the foundation of Internet communications and the standard for modern network communications.

TCP/IPThe protocol provides a reliable, end-to-end data transmission method, making it possible to exchange data between different computers and networks. It defines how data is split, transmitted, routed, and reassembled to ensure reliable transmission and delivery of data across the network.

The main components of the TCP/IP protocol

TCP/IPThe protocol consists of multiple layers, each responsible for different functions and tasks. Common TCP/IPprotocol levels are as follows:

  1. 物理层: The physical layer defines the electrical and physical characteristics of network hardware devices, such as network cables, optical fibers, and network interface cards. It is responsible for transferring the bitstream to the physical medium.

  2. 数据链路层: The data link layer is responsible for transferring data between directly connected nodes. It organizes the raw bit stream into data frames for transmission over physical connections and provides error detection and correction.

  3. 网络层: The network layer uses IPprotocols to define data transmission paths and address schemes. It is responsible for sending data packets (data packets) from the source host to the destination host, and determines the best path for the data through routing algorithms.

  4. 传输层: The transport layer uses TCPor UDPprotocol to provide end-to-end data transmission services. TCPThe protocol provides reliable, connection-oriented data transmission to ensure the order and reliability of data; the UDPprotocol provides connectionless data transmission, which is suitable for scenarios with high real-time requirements.

  5. 会话层: The session layer is responsible for establishing, managing and terminating network sessions. It defines communication rules and session control mechanisms between different hosts.

  6. 表示层: The presentation layer handles the representation and conversion of data to ensure that the data formats of different hosts can understand each other.

  7. 应用层: The application layer is the layer closest to the user, providing an interface for network applications and user interaction. It includes various network protocols, such as HTTP, FTP, , SMTPetc., for different types of data transmission and application requirements.

How the TCP/IP protocol works

TCP/IPThe protocol works in a layered manner, each layer is responsible for a specific function, and the upper layer uses the services provided by the lower layer to realize data transmission and interaction.

During data transmission, the sender's application program passes the data to the application layer, and then passes it down layer by layer, and each layer adds corresponding protocol header ( ) Headerinformation to the data. On the receiving side, each layer parses and processes according to the protocol header information, and passes the data up to the application layer by layer.

Features of the TCP/IP protocol include:

  • 可靠性: TCPThe protocol provides reliable data transmission, and guarantees the reliability and integrity of data through mechanisms such as sequence numbers, confirmation responses, retransmission mechanisms, and flow control.

  • 连接性: TCPThe protocol is connection-oriented, and a connection is established through a three-way handshake to ensure the reliability of both communication parties and the orderliness of data transmission.

  • 无连接性: IPThe protocol is connectionless, each data packet is sent independently without prior connection establishment.

  • 分组交换: TCP/IPThe protocol divides data into smaller packets for transmission, which can make more efficient use of network resources.

Summarize

TCP/IPThe protocol is the basic protocol of Internet communication, which defines the transmission mode and rules of data in the network. It consists of multiple protocols such as TCPand IP, and each protocol layer is responsible for different functions. TCP/IPThe protocol works in a layered manner to provide reliable, end-to-end data transmission services.

For front-end engineers, understanding TCP/IPprotocols is very important for understanding network communication, debugging network problems, and optimizing network transmission. Through a deep understanding of TCP/IPthe protocol, front-end engineers can better understand the data transmission process between the front-end and the back-end, and optimize network requests, improve performance and user experience during development.

Minesweeper

Minesweeper

<!DOCTYPE html>
<html>
<head>
  <title>扫雷游戏</title>
  <style>
    .cell {
      
      
      display: inline-block;
      width: 30px;
      height: 30px;
      border: 1px solid #ccc;
      text-align: center;
      vertical-align: middle;
      font-weight: bold;
      cursor: pointer;
    }
  </style>
</head>
<body>
  <h1>扫雷游戏</h1>
  <div id="game-board"></div>
  
  <script>
    const boardSize = 10;
    const mineCount = 10;
    
    let gameBoard = document.getElementById('game-board');
    let cells = [];
    let mines = [];
    let revealed = [];
    
    // 创建游戏面板
    function createBoard() {
      
      
      for (let i = 0; i < boardSize; i++) {
      
      
        let row = document.createElement('div');
        row.classList.add('row');
        gameBoard.appendChild(row);
        
        let cellRow = [];
        let revealedRow = [];
        
        for (let j = 0; j < boardSize; j++) {
      
      
          let cell = document.createElement('div');
          cell.classList.add('cell');
          cell.setAttribute('data-row', i);
          cell.setAttribute('data-col', j);
          cell.addEventListener('click', handleCellClick);
          row.appendChild(cell);
          
          cellRow.push(cell);
          revealedRow.push(false);
        }
        
        cells.push(cellRow);
        revealed.push(revealedRow);
      }
    }
    
    // 随机布置地雷
    function placeMines() {
      
      
      let count = 0;
      
      while (count < mineCount) {
      
      
        let row = Math.floor(Math.random() * boardSize);
        let col = Math.floor(Math.random() * boardSize);
        
        if (!mines[row][col]) {
      
      
          mines[row][col] = true;
          count++;
        }
      }
    }
    
    // 计算相邻地雷数量
    function calculateAdjacentMines(row, col) {
      
      
      let count = 0;
      
      for (let i = -1; i <= 1; i++) {
      
      
        for (let j = -1; j <= 1; j++) {
      
      
          let newRow = row + i;
          let newCol = col + j;
          
          if (newRow >= 0 && newRow < boardSize && newCol >= 0 && newCol < boardSize) {
      
      
            if (mines[newRow][newCol]) {
      
      
              count++;
            }
          }
        }
      }
      
      return count;
    }
    
    // 点击格子事件处理函数
    function handleCellClick(event) {
      
      
      let row = parseInt(event.target.getAttribute('data-row'));
      let col = parseInt(event.target.getAttribute('data-col'));
      
      if (mines[row][col]) {
      
      
        event.target.style.backgroundColor = 'red';
        event.target.textContent = 'X';
        revealMines();
        gameOver();
      } else {
      
      
        let count = calculateAdjacentMines(row, col);
        
        if (count > 0) {
      
      
          event.target.style.backgroundColor = 'lightgray';
          event.target.textContent = count;
        } else {
      
      
          event.target.style.backgroundColor = 'lightgray';
          event.target.textContent = '';
          revealEmptyCells(row, col);
        }
        
        revealed[row][col] = true;
        
        if (checkWin()) {
      
      
          gameWin();
        }
      }
      
      event.target.removeEventListener('click', handleCellClick);
    }
    
    // 揭示周围空白格子
    function revealEmptyCells(row, col) {
      
      
      let queue = [];
      queue.push({
      
       row, col });

      while (queue.length > 0) {
      
      
        const {
      
       row, col } = queue.shift();

        for (let i = -1; i <= 1; i++) {
      
      
          for (let j = -1; j <= 1; j++) {
      
      
            let newRow = row + i;
            let newCol = col + j;

            if (
              newRow >= 0 &&
              newRow < boardSize &&
              newCol >= 0 &&
              newCol < boardSize &&
              !revealed[newRow][newCol]
            ) {
      
      
              let count = calculateAdjacentMines(newRow, newCol);

              if (count > 0) {
      
      
                cells[newRow][newCol].style.backgroundColor = 'lightgray';
                cells[newRow][newCol].textContent = count;
              } else {
      
      
                cells[newRow][newCol].style.backgroundColor = 'lightgray';
                cells[newRow][newCol].textContent = '';
                queue.push({
      
       row: newRow, col: newCol });
              }

              revealed[newRow][newCol] = true;
              cells[newRow][newCol].removeEventListener('click', handleCellClick);
            }
          }
        }
      }
    }
    
    // 揭示所有地雷
    function revealMines() {
      
      
      for (let i = 0; i < boardSize; i++) {
      
      
        for (let j = 0; j < boardSize; j++) {
      
      
          if (mines[i][j]) {
      
      
            cells[i][j].style.backgroundColor = 'red';
            cells[i][j].textContent = 'X';
          }
        }
      }
    }
    
    // 检查是否胜利
    function checkWin() {
      
      
      for (let i = 0; i < boardSize; i++) {
      
      
        for (let j = 0; j < boardSize; j++) {
      
      
          if (!mines[i][j] && !revealed[i][j]) {
      
      
            return false;
          }
        }
      }
      
      return true;
    }
    
    // 游戏结束
    function gameOver() {
      
      
      for (let i = 0; i < boardSize; i++) {
      
      
        for (let j = 0; j < boardSize; j++) {
      
      
          cells[i][j].removeEventListener('click', handleCellClick);
        }
      }
    }
    
    // 游戏胜利
    function gameWin() {
      
      
      alert('恭喜!你赢了!');
      gameOver();
    }
    
    // 初始化游戏
    function initGame() {
      
      
      mines = [];
      revealed = [];
      
      for (let i = 0; i < boardSize; i++) {
      
      
        mines.push(new Array(boardSize).fill(false));
        revealed.push(new Array(boardSize).fill(false));
      }
      
      createBoard();
      placeMines();
    }
    
    // 开始游戏
    initGame();
  </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_42560424/article/details/131381001