Java network programming

Posted by holladb on Fri, 28 Jan 2022 10:47:23 +0100

Network programming

Overview of network programming

Computer network: connect computers with independent functions distributed in different regions with lines through communication equipment to realize the sharing of information and resources.

Network programming: Java language supports network. Java encapsulates the implementation details of the network into different classes. Using the open programs of these classes can realize the data transmission between networks.

The core element of network programming: how to accurately find a host IP (host) + port (program) of the network

How to transmit data reliably and efficiently after finding the transmission protocol

Application layer http - > transport layer TCP - > network layer IP - > physical layer (network card, network cable)

IP: find host

Port: find the port of a program of the computer

Communication protocol: TCP/UDP for data transmission through which regulations are adopted by both parties

Transport layer protocol

TCP

Safe and reliable, low transmission efficiency compared with UDP protocol;

Before sending data, establish a connection and shake hands three times

Confirm that there is no problem with the network and start data transmission

At the end, wave four times

Three handshakes:
First handshake: the client sends to the server; The server confirms that the other party's sending is normal
The second handshake: the client confirms that its sending and receiving are normal, and the other party's sending and receiving are normal; The server confirms that its reception is normal and the other party's transmission is normal
The third Handshake: the client confirms that its sending and receiving are normal, and the other party's sending and receiving are normal; The server confirms that its sending and receiving are normal, and the other party's sending and receiving are normal

Four waves:
First wave: the client sends a closed connection to close the data transmission from the client to the server.
Second wave: after the server receives the closed connection, it sends a confirmation to the server. The confirmation number is the received number + 1
Third wave: the server sends a closed connection to close the data transmission from the server to the client.
The fourth wave: after receiving FIN, the client sends a confirmation to the server, and the confirmation sequence number is the received sequence number + 1.

UDP

There is no need to establish and release a connection to send data, which is fast

However, it is unreliable. The data, source and destination are encapsulated in a datagram and just send it

TCP programming

Server side

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        Socket socket = serverSocket.accept();//monitor
        Scanner scanner = new Scanner(System.in);

        OutputStream out = socket.getOutputStream();
        DataOutputStream dout = new DataOutputStream(out);
        InputStream in = socket.getInputStream();
        DataInputStream din = new DataInputStream(in);

        while (true){
            String msg = din.readUTF();
            System.out.println("Messages from clients:"+socket.getRemoteSocketAddress()+msg);

            System.out.println("Server input:");
            String servetMsg = scanner.next();
            dout.writeUTF(servetMsg);
        }

    }
}

client

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",9999);
        Scanner scanner = new Scanner(System.in);

        OutputStream out = socket.getOutputStream();
        DataOutputStream dout = new DataOutputStream(out);
        InputStream in = socket.getInputStream();
        DataInputStream din = new DataInputStream(in);

        //Client cyclic sending and receiving
        while (true){
            System.out.println("Customer input:");
            String msg = scanner.next();
            dout.writeUTF(msg);

            String clientMsg = din.readUTF();
            System.out.println("Message from server:"+clientMsg);
        }
    }
}

UDP programming

Server side

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Server {
    public static void main(String[] args) throws IOException {
        DatagramSocket socket = new DatagramSocket(9999);
        byte[] b = new byte[1024];
        DatagramPacket packet = new DatagramPacket(b,b.length);//Datagram
        //receive
        socket.receive(packet);

        String msg = new String(b,0,packet.getLength(),"utf-8");
        System.out.println(msg);
    }
}

client

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;

public class Client {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();

        //Create datagram
        byte[] b = "Hello, server side".getBytes("utf-8");
        DatagramPacket datagramPacket = new DatagramPacket(b,b.length, InetAddress.getByName("127.0.0.1"),9999);
        datagramSocket.send(datagramPacket);
    }
}

Topics: Java network udp