[computer network] - Protocol layering

Posted by mrpickleman on Fri, 04 Mar 2022 14:21:06 +0100

1. Agreement: an agreement.

2. Protocol layering:

(1) OSI (open system interconnection) seven layer model

(2) TCP/IP five layer model

(3) TCP/IP four layer model

Layer namefunctionRelated agreements
application layerResponsible for communication between applications

HTTP SSH DNS

Display layerConvert the network datagram into the package required by the program 
Session layerResponsible for managing connections and disconnections 
Transport layerResponsible for the routing of data transmission between two hostsTCP UDP
network layerResponsible for address management and routingIP 
data link layerBe responsible for the transmission and identification of data frames between devices 
physical layerResponsible for the transmission mode of optical / electrical signals 

3. Differences between TCP and UDP:

UDP: connectionless, unreliable, datagram oriented (Datargam as a whole) - fast performance.

Because there is no UDP connection buffer, there is only no UDP connection buffer.

TCP: connection oriented, stable and data flow oriented.

TCP has both sending buffer and receiving buffer.

4. Address management in the network

(1) IP protocol has two versions: IPv4 and IPv6;

What is the difference between IPv4 and IPv6?

1. Number of addresses

The IP address of IPv4 is a 32-bit binary number, which can provide 2 ^ 32 addresses.

IPv6 adopts 128 bit address length and can provide 2 ^ 128 addresses, which is 2 ^ 96 times that of IPv4.

2. Transmission speed

IPv6 uses a fixed header;

IPv6 has smaller routing table and stronger aggregation ability, which ensures shorter data forwarding path and improves forwarding efficiency;

IPv6 eliminates most of the common address conflicts in IPv4 and provides more simplified connection and communication for devices.

3. Security of transmission mode

IPv4 is insecure;

IPv6 is relatively secure. It authenticates and encrypts data at the network layer and verifies IP messages to provide users with data security from client to server and ensure that data is not hijacked.

4. Others

Compared with IPv4, IPv6 protocol is more friendly to mobile terminals. It can enhance the mobility, security and routing characteristics of mobile terminals, and reduce the difficulty and investment of network deployment.

IPv6 adds automatic configuration and reconfiguration technology, plug and play, which can automatically add, delete and update the configuration for IP address and other information, so as to improve the manageability of IPv6.

(2) Mac address: used to identify the connected nodes in the data link layer - Size 6 bytes;

4.UDP network programming

(1) Object datagramsocket - > client (the end that actively sends the request), server (the end that passively receives the request) (layering of business layer)

Set a fixed port number on the server side; To facilitate the connection of the client.

(2) Datagram packet (container for receiving and sending requests): common methods: send(); receive().

(3) Specific application: realize the communication between client and server

UDP server side:

//UDP server side
public class UDPServer {
    // Port number
    private static final int port = 9001;
    // Maximum value of data
    private static final int bleng = 1024;
    public static void main(String[] args) throws IOException {
        // 1. Start a UDP server (IP, port number)
        DatagramSocket socket = new DatagramSocket(port);
        System.out.println("Server started!");
        while (true){
            // 2. Initialize packet
            DatagramPacket clientPacket = new DatagramPacket(
                    new byte[bleng],
                    bleng
            );
            // 3. Wait for the client's link
            socket.receive(clientPacket);
            // This step indicates that there is already a client connection
            // 4. Information received from the client
            String msg = new String(clientPacket.getData());
            System.out.println("Information received from client:"+msg);
            // Give the client a response
            String serMsg = "I got it.";
            // send message
            DatagramPacket serPacket = new DatagramPacket(
                    serMsg.getBytes(),
                    serMsg.getBytes().length, //Byte length
                    clientPacket.getAddress(),
                    clientPacket.getPort()
            );
            socket.send(serPacket);
        }
    }
}

UDP client:

public class UDPClient {
    // IP address of the server
    private static final String ip = "127.0.0.1";
    // Port number of the server
    private static final int port = 9001;
    // Maximum data length
    private static final int bleng = 1024;
    public static void main(String[] args) throws IOException {
        // 1. Start the client
        DatagramSocket client = new DatagramSocket();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.print("->");
            String msg = scanner.nextLine();
            // 2. Send a message to the server
            DatagramPacket datagramPacket = new DatagramPacket(
                    msg.getBytes(),
                    msg.getBytes().length,
                    InetAddress.getByName(ip),
                    port
            );
            client.send(datagramPacket);
            // 3. Receive the return information from the server
            DatagramPacket serPacket = new DatagramPacket(
                    new byte[bleng],
                    bleng
            );
            client.receive(serPacket);
            // Server side return information received
            String serMsg = new String(serPacket.getData());
            System.out.println("Server returned:"+serMsg);
        }
    }
}

 

 

Topics: Java network socket udp TCPIP