[JavaLearn] (14) network and classification, TCP, UDP protocol, IP, Socket, TCP programming, UDP programming

1. Basic concept of network

1.1 computer network

Definition: refers to a computer system that connects multiple computers and their external devices with independent functions (no network can exist independently) in different geographical locations through communication lines, and realizes resource sharing and information transmission under the management and coordination of network operating system, network management software and network communication protocol

  • Backbone: computer network is a computer system
  • Network functions: resource sharing and information transmission
  • Network composition
    • Network hardware: computer, external equipment, communication connection
    • Network software: network operating system, network management software, network communication protocol

Classification – by size:

  • LAN LAN
  • MAN man
  • Wide area network WAN

Classification – by transmission medium:

  • Coaxial cable network (similar to cable of cable TV network)

  • Twisted pair network

  • Optical fiber network (transmitting optical signals)

  • Satellite network

Classification – topology:

  • Star network (most commonly used)

  • Bus network: signals can be received in the transmission process. If it is identified as its own, it will be received

  • Ring network: the same delivery mode

1.2 network communication protocol

To realize communication in the network, there must be some conventions (Communication Protocol) to specify standards for rate, transmission code, transmission control steps and so on (such as traffic rules)

Question: network communication involves many contents: source address, target address, encryption and decryption, flow control, routing control, etc. How to realize such a complex network protocol?

===Layering: decompose complex components into some simple components, and then combine them (the same layer can communicate with each other, and the upper layer can call the next layer without any relationship with the next layer)

Network communication protocol layering:

  • Nominal standard: ISO -- > OSI reference model
  • De facto standard: TCP/IP protocol stack

Data encapsulation and unpacking: in the process of transmission, various data need to be added through each layer, and finally sent to the other end, and then disassembled at the other end

1.3 TCP/IP protocol stack

The most commonly used protocol for network communication

  • Main protocol of network layer: IP protocol
  • Main protocols of transport layer: TCP and UDP protocols

1.4 TCP protocol

Connection oriented, reliable, byte stream based transport layer communication protocol (telephone case)

  • Connection oriented (a piece of information is sent after segmentation, and the sending order is consistent with the receiving order)
  • Point to point communication
  • High reliability: three handshakes
  • Occupy more system resources and low efficiency

Application List: HTTP, FTP, Telnet, SMTP

1.5 UDP protocol

Connectionless transport layer protocol provides simple and unreliable information transmission services for transactions (sending telegrams, sending group SMS)

  • Non connection oriented, unreliable transmission and possible loss (a piece of information is sent after segmentation, not necessarily which section arrives first)
  • No matter whether the other party is ready or not, the receiver will not reply after receiving it
  • Can broadcast and send
  • Very simple protocol with less overhead

Application cases: DNS, SNMP

1.6 IP address and port

IP address, used to mark the address of a communication entity (computer, router) in the network

Classification:

  • IPV4: 32-bit address, expressed in dotted decimal system, such as 192.168.0.1
  • IPV6: 128 bits are written into 8 16 bit unsigned integers. Each integer is identified by 4 hexadecimal bits, and the number is divided by:

Special IP address:

  • 127.0.0.1: local address
  • 192.168.0.0 – 192.168.255.255 private address for internal use of the organization

port:

  • IP address is used to identify a computer, but a computer can provide multiple applications, so you need to use ports to distinguish applications
  • Range: 0 – 65535 (16 bit integer)

Port classification:

  • 0 – 1023: recognized port (for example, 80 to WWW, 21 to FTP, etc.)
  • 1024 – 49151: registered port (assigned to user or application)
  • 49152 – 65535: dynamic / private port

IP and port API s:

  • InetAddress class: encapsulates the ip address of a computer without a port
  • InetSocketAddress: contains the port used for socket communication

1.7 URL uniform resource locator

Uniform Resource Locator: it consists of four parts: protocol, host domain name where resources are stored, port number and resource file name

1.8 Socket

Socket is actually a programming interface provided by the transport layer to the application layer

Similar to sending a letter: the user (application layer) can put the letter (data) into the mailbox (the port of the mailbox is the socket). After entering the socket, how to send the letter is the matter of the post office, highway traffic management (transmission layer, network layer), etc.

2. Common classes of network programming

2.1 encapsulated IP address – InetAddress

// 1. Obtain IP address
InetAddress ia = InetAddress.getLocalHost();  // Native ip

// 2. Operation IP address
System.out.println(ia);  // DESKTOP-F31QQ1H/192.168.0.102
System.out.println(ia.getHostAddress()); // 192.168.0.102
System.out.println(ia.getHostName()); // DESKTOP-F31QQ1H

InetAddress ia2 = InetAddress.getByName("www.lwclick.com");  // Get ip through domain name
System.out.println(ia2);

2.2 encapsulation IP and Port – InetSocketAddress

// Create an InetSocketAddress object
InetSocketAddress isa = new InetSocketAddress("www.lwclick.com", 8888);

// Get object content
System.out.println(isa);  // www.lwclick.com/104.21.41.202:8888
System.out.println(isa.getAddress());  // www.lwclick.com/104.21.41.202
System.out.println(isa.getPort());  // 8888

2.3 URL class

// Create a URL protocol: https domain name / IP address: lwlick COM port: 80 path: / categories/MySQL/
URL url = new URL("https://lwclick.com:80/categories/MySQL/");

// Get the components of the URL
System.out.println(url.getProtocol());  //  https
System.out.println(url.getHost());  //  lwclick.com
System.out.println(url.getPort());  //  80
System.out.println(url.getDefaultPort());  //  443 default https port
System.out.println(url.getPath());  //  /categories/MySQL/

3. TCP programming

3.1 one way communication

  • Server side:

    • Create a ServerSocket and listen on the specified port (accept() method)

      And process the request (if the client request arrives, return the corresponding Socket; otherwise, wait all the time and the thread is blocked)

  • client:

    • To create a Socket, you need to specify the ip and port number of the server to send and receive responses to the server
  • Send data:

    • You need to use an output stream, which can be wrapped through DataOutputStream and ObjectOutputStream to improve efficiency
  • Receive data:

    • Use input stream, wrapped with DataInputStream and ObjectInputStream

Server side:

public class LoginServer {
    public static void main(String[] args) throws IOException {
        // 1. Create a ServerSocket and configure the listening port
        ServerSocket serverSocket = new ServerSocket(8080);

        // 2. Use ServerSocket to listen on the specified port
        Socket socket = serverSocket.accept(); // If the request is not received, it is blocked here; When the request arrives, a socket is returned and execution continues

        // 3. Receive the request data from the client and output the result
        InputStream is = socket.getInputStream(); // Get stream
        DataInputStream dis = new DataInputStream(is); // Data streams are also used for packaging
        String info = dis.readUTF(); // Read the written data of the corresponding type
        System.out.println("Client requests:" + info);

        // 4. Close resources
        dis.close();
        serverSocket.close();
    }
}

client:

public class LoginClient {
    public static void main(String[] args) throws IOException {
        // 1. Create a Socket to indicate the server-side ip and port
        Socket socket = new Socket(InetAddress.getLocalHost(), 8080); // InetAddress.getByName() get ip

        // 2. Send data to the server
        OutputStream os = socket.getOutputStream(); // Information is sent through stream and output stream
        DataOutputStream dos = new DataOutputStream(os); // Data stream packaging
        dos.writeUTF("userName=lwclick&pwd=123");

        // 3. Close resources
        dos.close(); // Close the high-level flow and the low-level flow will be closed automatically
    }
}

Note: during the test, the server needs to be started first, and then the client needs to be started

3.2 primary two-way communication

Server side:

public static void main(String[] args) throws IOException {
    ServerSocket serverSocket = new ServerSocket(8080);
    Socket socket = serverSocket.accept(); 
    InputStream is = socket.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    String info = dis.readUTF(); 
    System.out.println("Client requests:" + info);

    // ==============================Send data to client=====================================
    // 4. Give a response to the client
    OutputStream os = socket.getOutputStream(); // The socket here is the response of the received client
    DataOutputStream dos = new DataOutputStream(os);
    dos.writeUTF("Login succeeded, welcome!");

    // 5. Close resources
    dos.close();
    dis.close();
    serverSocket.close();
}

3.3 transmission object

User class: when the class is transmitted on the network, it must implement the serialization interface

public class User implements Serializable {
    private String userId;
    private String password;
    
    // getter / setter / toString / constructor
}

Server side:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    ServerSocket serverSocket = new ServerSocket(8080);
    Socket socket = serverSocket.accept(); 
    InputStream is = socket.getInputStream(); 
    
    // ===============Here, use [object stream] to receive data===================
    ObjectInputStream ois = new ObjectInputStream(is); 
    User user = (User)ois.readObject(); 
    System.out.println("Client requests:" + user);
    
    OutputStream os = socket.getOutputStream(); 
    DataOutputStream dos = new DataOutputStream(os);
    if (user.getUserId().indexOf("lwclick") >= 0 && user.getPassword().length() > 6) {
        dos.writeUTF("Login succeeded, welcome!");
    } else {
        dos.writeUTF("Login failed, please try again!");
    }

    dos.close();
    ois.close();
    serverSocket.close();
}

client:

public static void main(String[] args) throws IOException {
    Socket socket = new Socket(InetAddress.getLocalHost(), 8080);

    // Get user input
    Scanner sc = new Scanner(System.in);
    System.out.print("userId:  ");
    String userId = sc.next();
    System.out.print("password:  ");
    String password = sc.next();
    User user = new User(userId, password);

    OutputStream os = socket.getOutputStream();
    // [object flow] for packaging
    ObjectOutputStream oos = new ObjectOutputStream(os); 
    oos.writeObject(user);

    InputStream is = socket.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    String info = dis.readUTF();
    System.out.println("Server side response:" + info);

    dis.close();
    oos.close();
}

3.4 introduction of multithreading

Put the processing steps after the server receives the request into the run() method of the thread, and create a thread to execute every request

Thread class:

public class LoginThread extends Thread {
    private Socket socket;

    public LoginThread() {
    }
    
    public LoginThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        DataOutputStream dos = null;
        ObjectInputStream ois = null;
        try {
            InputStream is = socket.getInputStream();
            ois = new ObjectInputStream(is);
            User user = (User)ois.readObject();
            System.out.println("Client requests:" + user);

            OutputStream os = socket.getOutputStream();
            dos = new DataOutputStream(os);
            if (user.getUserId().indexOf("lwclick") >= 0 && user.getPassword().length() > 6) {
                dos.writeUTF("Login succeeded, welcome!");
            } else {
                dos.writeUTF("Login failed, please try again!");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (dos != null) {
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Server side: create a thread to process each login request

public static void main(String[] args) throws IOException, ClassNotFoundException {
    ServerSocket serverSocket = new ServerSocket(8080);

    int i = 1;
    while (true) {
        Socket socket = serverSocket.accept();
        
        // Create a thread to handle each login request
        new LoginThread(socket).start();

        // Count the IP address of the client and the total number of requests
        InetAddress ia = socket.getInetAddress();
        System.out.println("This is the second" + (i++) + "A request, the other party's IP the address is:" + ia.getHostAddress());
    }
}

Client: (no need to change, in fact, multiple clients access the server at the same time)

public static void main(String[] args) throws IOException {
    Socket socket = new Socket(InetAddress.getLocalHost(), 8080);

    // Get user input
    Scanner sc = new Scanner(System.in);
    System.out.print("userId:  ");
    String userId = sc.next();
    System.out.print("password:  ");
    String password = sc.next();
    User user = new User(userId, password);

    OutputStream os = socket.getOutputStream();
    // [object flow] for packaging
    ObjectOutputStream oos = new ObjectOutputStream(os); 
    oos.writeObject(user);

    InputStream is = socket.getInputStream();
    DataInputStream dis = new DataInputStream(is);
    String info = dis.readUTF();
    System.out.println("Server side response:" + info);

    dis.close();
    oos.close();
}

4. UDP programming

Online communication between clients and consultants without connection

  • Socket network programming based on UDP protocol
  • No need to use IO stream to realize data transmission
  • Each data sending unit is uniformly encapsulated into data packets (ip, interface, data, etc.). The sender sends the data to the network, and the data packet looks for its destination on the network

Classes to use:

  • Datagram socket: used to send or receive data packets
  • Datagram packet: data packet

4.1 one way communication

client:

public static void main(String[] args) throws IOException {
    // 1. Create a Socket to send and receive data packets
    DatagramSocket socket = new DatagramSocket(9999); // Interface monitored by client

    // 2. Send a packet using socket
    String str = "Kiss, are you there";
    byte[] buf = str.getBytes();
    InetAddress ia = InetAddress.getLocalHost();
    int port = 8888; // Port number of the server receiving data
    DatagramPacket packet = new DatagramPacket(buf, buf.length, ia, port);
    // Send packet
    socket.send(packet);

    // 3. Close the socket
    socket.close();
}

Server side:

public static void main(String[] args) throws IOException {
    // 1. Create a Socket to send and receive data packets
    DatagramSocket socket = new DatagramSocket(8888);  // Server side listening interface

    // 2. Use socket to receive a packet
    byte[] buf = new byte[128];
    DatagramPacket packet = new DatagramPacket(buf, buf.length);
    socket.receive(packet); // ip, port and other information
    System.out.println(new String(packet.getData(), 0, packet.getLength()));
    System.out.println(packet.getAddress());
    System.out.println(packet.getPort());

    // 3. Close the socket
    socket.close();
}

4.2 multiple two-way communication

client:

public static void main(String[] args) throws IOException {
    // 1. Create a Socket to send and receive data packets
    DatagramSocket socket = new DatagramSocket(9999); // Interface monitored by client
    Scanner sc = new Scanner(System.in);
    while (true) {
        String line = sc.nextLine();
        // 2. Send a packet using socket
        byte[] buf = line.getBytes();
        InetAddress ia = InetAddress.getLocalHost();
        int port = 8888; // Port number of the server receiving data
        DatagramPacket packet = new DatagramPacket(buf, buf.length, ia, port);
        // Send packet
        socket.send(packet);

        // If the client enters bye, the conversation ends
        if ("bye".equals(line)) {
            break;
        }

        // Receive messages returned by the server
        byte[] bytes = new byte[128];
        DatagramPacket packetReceive = new DatagramPacket(bytes, bytes.length);
        socket.receive(packetReceive);
        System.out.println(new String(packetReceive.getData(), 0, packetReceive.getLength()));
    }

    // 3. Close the socket
    socket.close();
}

Server side:

public static void main(String[] args) throws IOException {
    // 1. Create a Socket to send and receive data packets
    DatagramSocket socket = new DatagramSocket(8888); // Server side listening interface
    Scanner sc = new Scanner(System.in);

    while (true) {
        // 2. Use socket to receive a packet
        byte[] buf = new byte[128];
        DatagramPacket packet = new DatagramPacket(buf, buf.length);
        socket.receive(packet); // ip, port and other information
        String info = new String(packet.getData(), 0, packet.getLength());
        System.out.println(info);
        if ("bye".equals(info)) {
            break;
        }

        // Use socket to send a packet to the client
        String str = sc.nextLine();
        byte[] bytes = str.getBytes();
        InetAddress address = packet.getAddress(); // Address of the client sending the data
        int port = packet.getPort(); // Port number
        DatagramPacket sendPacket = new DatagramPacket(bytes, bytes.length, address, port);
        socket.send(sendPacket);
    }

    // 3. Close the socket
    socket.close();
}

5. File upload

Using TCP programming to realize file upload function

  • Idea: copy the file twice
    • [Client] copies the file from [local] to [network]
    • [server] copy the file from [network] to [local]

client:

public class UploadClient {
    public static void main(String[] args) throws IOException {
        // Create a socket to indicate the server-side ip and listening port
        Socket socket = new Socket(InetAddress.getLocalHost(), 8800);

        // ========================Upload files to the destination port of the server===========================
        // Source files for this machine
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("e:/readme.txt")); 
        // Write the file to the destination server port
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); 

        byte[] buf = new byte[1024];
        int len = bis.read(buf);
        while (len != -1) {
            bos.write(buf, 0, len);
            len = bis.read(buf);
        }

        bos.close();
        bis.close();
    }
}

Server side:

public class UploadServer {
    public static void main(String[] args) throws IOException {
        // Create a ServerSocket
        ServerSocket serverSocket = new ServerSocket(8800);

        // Use ServerSocket to listen on the specified port
        Socket socket = serverSocket.accept();

        // =====================Fetch file from destination port========================
        // Content fetching destination
        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); 
        // Save to local of server
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("e:/readme2.txt")); 

        byte[] buf = new byte[1024];
        int len = bis.read(buf);
        while (len != -1) {
            bos.write(buf, 0, len);
            len = bis.read(buf);
        }

        bos.close();
        bis.close();
    }
}

Tags: websocket udp TCP/IP JavaLearn

Posted by titangf on Fri, 15 Apr 2022 13:46:39 +0930