Java network programming

Posted by Studio381 on Wed, 27 Oct 2021 14:52:22 +0200

Network programming is a program running on different computers under the network communication protocol, which can transmit data  

Three elements of network programming

IP address

        The identification number of the computer, that is, the device identification. The IP address of each computer is different, and the IP address is unique

port

        The port number is the identification of applications in the computer equipment, and each application corresponds to a unique port number

agreement

        The rules of link and communication are called protocols, which uniformly stipulate data transmission, transmission rate, transmission steps, etc

        Both sides of communication must abide by it at the same time to complete data exchange. The common protocols are UDP and TCP

UDP protocol

User datagram protocol

UDP is A connectionless communication protocol. There is no logical link between the sending end and the receiving end of data: when computer A sends data to computer B, A will send data regardless of whether computer B exists, and B will not feed back to A whether it has received data when receiving data

Because UDP protocol consumes less system resources and has high communication efficiency, it is often used for the transmission of audio, video and ordinary data

UDP cannot guarantee the integrity of data transmission, and some data may be lost during transmission

TCP protocol

Transmission control protocol

TCP is a connection oriented communication protocol. It establishes a logical connection before data transmission. It provides reliable and error free data transmission between two computers. Each connection needs to go through "three handshakes"

Triple handshake refers to the three interactions between the client and the server in the preparation stage of sending data in TCP protocol to ensure reliable connection

First handshake: the client sends a connection request to the server and waits for the server to confirm

Second handshake: the server sends back a response to the client to notify the client that it has received the connection request

Third Handshake: after the connection is established, the client and server can transmit data

Because TCP is connection oriented, TCP protocol can ensure the security of data transmission, so it is widely used

TCP is commonly used to upload files, download files, browse web pages, etc

Send data using UDP

 //Create the sender's Socket object DatagramSocket
        DatagramSocket ds = new DatagramSocket();
//Create data and package it
        byte[] bys = "hello,UDP".getBytes();
 //The four parameters passed in are to store the information to be sent into the byte type array, the length of the array to be sent, the IP address of the receiving end, and the port number of the receiving end
        DatagramPacket dp = new DatagramPacket(bys,bys.length, InetAddress.getByName("127.0.0.1"),12345);
 //Call DatagramSocket object to send data
        ds.send(dp);
 //Close sender        
        ds.close();

Receive data using UDP

//Create the Socket object DatagramSocket of the receiving end
        DatagramSocket ds = new DatagramSocket(12345);
//Create an array to receive data
        byte[] bys = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bys, bys.length);
//Call the method of DatagramSocket object to receive data
        ds.receive(dp);
//Analyze
        System.out.println(new String(dp.getData(),0, dp.getLength()));

Three communication modes of UDP

Unicast -- unicast is used for end-to-end communication between two hosts

Multicast -- multicast is used to communicate with a specific group of hosts

Broadcast -- broadcast is used for data communication between a host and all hosts on the whole LAN

Multicast creation method

Sender

         Replace the IP address specified by unicast with multicast address

receiving end

        Bind the current computer to a multicast address to receive

Broadcast creation method

Sender

        Replace the IP address with the broadcast address for encapsulation

Only the differences from unicast are explained here

Send data using TCP

//Create the Socket object of the client. The parameters are IP address and port number
        Socket s = new Socket("127.0.0.1",10000);
 //Get output stream and write data
        OutputStream os = s.getOutputStream();
        os.write("hello,TCP".getBytes());
//        Release resources
        s.close();

Receive data using TCP

//ServerSocket(int port) creates a server socket bound to a specified port
        ServerSocket ss = new ServerSocket(10000);
        //Socket accept() listens for the socket to connect to and accept it
        Socket s = ss.accept();
        //Get the input stream, read the data, and display the data on the console
        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String data = new String(bys,0,len);
        System.out.println(data);
        //Release resources
        s.close();
        ss.close();

Topics: Java udp TCP/IP