current position:Home>Do you want to ask about network knowledge in the interview? Just know a little

Do you want to ask about network knowledge in the interview? Just know a little

2022-01-27 00:11:34 nezha

One 、TCP And UDP

TCP(Transmission Control Protocol, Transmission control protocol ) agreement : Responsible for segmenting the files to be transferred transmitted , Generally used to establish a session , Its basic characteristic is reliable transmission 、 flow control , The so-called three handshakes 、 Four waves are also based on TCP Agreed .

UDP(User Data Protocol, User datagram protocol ) agreement : One data packet can complete data communication , Packets are not segmented , No need to establish a session , No need for flow control , It's an unreliable transmission , On screen broadcast 、 multicast 、 Broadcasts are based on UDP agreement .

Two 、 be based on TCP Connection oriented socket of protocol (Internet Socket)

1、TCP

The Transmission Control Protocol Transmission control protocol .

2、 Streaming socket

We often talk about sockets, that is Internet Scoket, According to the data transmission mode , Can be Internet Scoket There are several kinds of , In the use of socket() When creating a network connection , It must be told which data transmission mode it is , Simply put, it is the control protocol of data transmission .

The computer field is called : Streaming socket (Stream Sockets), Use... In computer code SOCK_STREAM Express .

3、SOCK_STREAM Has the following characteristics

  • reliable 、 Two way communication data flow ;
  • Data transmission is carried out in sequence ;
  • The data is accurate in transmission , It has its own retransmission mechanism ;
  • The sending and receiving of data are out of sync , There is a buffer ;
  • The typical application of stream format socket is HTTP agreement , Because the browser must have complete and correct data when loading and parsing .

3、 ... and 、 be based on UDP Connectionless socket for protocol (Datagram Sockets)

1、UDP

User Datagram Protocol User datagram protocol .

2、 Datagram format socket

be based on UDP Connectionless socket for protocol , The computer field is called : Datagram format socket (Datagram Sockets), Used in computer code SOCK_DGRAM Express .

SOCK_DGRAM It's a high efficiency 、 high velocity 、 Unreliable communication data flow . It is based on UDP Agreed , No data verification , Only data transmission .

3、SOCK_DGRAM Has the following characteristics

  • There is a limit on the size of data transmitted each time ;
  • The pursuit of high efficiency 、 high velocity ;
  • The transmitted data may be lost or damaged ( Small probability events );
  • Sending and receiving data is synchronous , That is, there are data boundaries ;
  • A typical application of datagram format is QQ Video and voice , Because the requirements of both are high efficiency and low latency , At the same time, the loss of data with small probability will not affect the transmission quality of the whole data .

Four 、 Blocking problem of receiving data

1、TCP

If there is no data , Blocking Socket I'll wait , Until there are several ;

Non blocking Socket return WSAEWOULDBLOCK;

If there's data , Take as much as you have .

2、UDP

If there is no data , Blocking Socket I'll wait ;

Non blocking Socket return WSAEWOULDBLOCK;

If there's data , It will wait for the whole contract to arrive , And receive the whole contract , To return to .

5、 ... and 、TCP Realize data upload

1、TCP Server side

package com.tcpip;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
public class TcpServerTest {
    
    public static void main(String[] args) throws Exception {
    
        //1、 Create services 
        ServerSocket serverSocket = new ServerSocket(9000);
        //2、 Listen for client connections 
        Socket socket = serverSocket.accept();
        //3、 Get input stream 
        InputStream is = socket.getInputStream();
        //4、 File output 
        FileOutputStream fos = new FileOutputStream(new File("copy.png"));
        byte[] buffer = new byte[1024];
        int len;
        while ((len=is.read(buffer))!=-1){
    
            fos.write(buffer,0,len);
        }
        //5、 close resource 
        fos.close();
        is.close();
        socket.close();
        serverSocket.close();
    }
}

2、TCP client

package com.tcpip;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
 
public class TcpClientTest {
    
    public static void main(String[] args) throws Exception {
    
        //1、 Create a socket Connect 
        Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
        //2、 Create an output stream 
        OutputStream os = socket.getOutputStream();
        //3、 Read the file 
        FileInputStream fis = new FileInputStream(new File("01.png"));
        //4、 write file 
        byte[] buffer = new byte[1024];
        int len;
        while ((len=fis.read(buffer))!=-1){
    
            os.write(buffer,0,len);
        }
        //5. close resource 
        fis.close();
        os.close();
        socket.close();
    }
}

3、 test

 Insert picture description here

6、 ... and 、UDP Realize data upload

1、UDP The receiver

package com.tcpip;
 
import java.net.DatagramPacket;
import java.net.DatagramSocket;
 
public class UdpServerTest {
    
    public static void main(String[] args) throws Exception {
    
        //1、 Open ports 
        DatagramSocket socket = new DatagramSocket(9090);
        //2、 Receive packets 
        byte[] buffer = new byte[1024];
        DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
        socket.receive(packet);
        System.out.println(new String(packet.getData(),0,packet.getData().length));
        //3、 Close the connection 
        socket.close();
    }
}

2、UDP The sender

package com.tcpip;
 
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
 
public class UdpClientTest {
    
    public static void main(String[] args) throws Exception {
    
        //1、 Build a socket
        DatagramSocket socket = new DatagramSocket();
        //2、 Build a package 
        String msg = "UDP  test ";
        InetAddress localhost = InetAddress.getByName("localhost");
        int port  = 9090;
        DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
        //3、 Send package 
        socket.send(packet);
        //4、 close resource 
        socket.close();
    }
}

3、 test

 Insert picture description here

7、 ... and 、 Network layer security

1、 What are the security issues ?

  1. IP Deceptive attack
  2. Smurf attack (DDOS A type of attack )
  3. ICMP Redirection and unreachable attacks
  4. IP Address scanning attack

2、ipidea brief introduction

ipidea Is an overseas reptile agent , Provide overseas housing ip And overseas static ip, Mainly engaged in data collection business , Maintain long-term cooperation with multiple data acquisition enterprises . It provides 9000 ten thousand ip resources , You can customize the settings according to the capture requirements ip Rotation time , So as to avoid being blocked by the website . Support account secret authentication and API There are two ways to extract ip resources , The most important thing is to support free testing .

See the world's leading Internet big data for more details IP resources



 Insert picture description here

3、ipidea Generate api link , Copy the link to apply

Plain or transparent ip During test extraction ip At the same time, it will waste a lot of time , Last , It may also fail ip. It's just that you can use agents ip, The way of use is simpler , Whether it's practical , stability 、 Security can be guaranteed . Use IPIDEA Conduct ip Extract and use .
 Insert picture description here

4、 Build agent ip Operation and use

In the process of server maintenance or its own agent ip when , Many of them are not maintained in , So many agents ip appear , agent ip The use of is relatively simple , If there are problems, you can feed back to the agent in time and find technicians to solve them . Crawlers are used to quickly complete the work requirements , Using agents can save working time , Finish the work quickly .

Java Learning route supporting articles :Java Summary of learning routes , Brick movers counter attack Java Architects ( The best in the whole network )
Basic recommendation :Java Basic tutorial series
Practical recommendation :Spring Boot Basic course
brief introduction :Java Quality creators in the field 、CSDN The author of the official account of the No 、Java Architects, strivers
Scan the QR code on the left side of the home page , Join the group chat , Learning together 、 Progress together
Welcome to thumb up Collection Leaving a message.

 Insert picture description here

Add wechat , remarks 1024, Give Java Learning route mind map

copyright notice
author[nezha],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270011308628.html

Random recommended