Detailed notes on the introduction of network programming based on Java

First knowledge of network programming

Three elements of network programming

IP

  • The address of the device in the network is a unique identifier.

The port number

  • The unique identifier of the application in the device.

protocol

  • Rules for data transmission in the network. Common protocols include UDP, TCP, http, https, and ftp.

The use of the api interface of the InetAddress class

insert image description here


Network Transmission Model

  • OSI reference model: World Internet protocol standard, global communication specification, a single model is too idealistic, and has not been widely promoted on the Internet
  • TCP/IP Reference Model (or TCP/IP Protocol): The de facto international standard.

insert image description here


TCP/IP four-layer model

insert image description here


network transport protocol

In computer networks, the rules for connection and communication are called network communication protocols

UDP protocol

  • User Datagram Protocol (User Datagram Protocol) UDP is a connectionless communication protocol.
  • high speed,size limitSend up to 64K at a time, the data is not safe and easy to lose

The UDP protocol does not care whether the sending and receiving parties have successfully connected, it only sends


TCP protocol

  • Transmission Control Protocol TCP (Transmission Control Protocol) TCP protocol is a connection-oriented communication protocol.
  • Slow speed, no size limit, data security.

The TCP protocol will first confirm whether the sender and the receiver are successfully connected, and then send


UDP

UDP communication program

send data

    public static void main(String[] args) throws IOException {
    
    
        //发送数据

        //1.创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket();

        //2.打包数据
        String str = "hello world!";

        byte[] bytes = str.getBytes();
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port = 10086;//指定发送到哪个端口

        DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);

        //3.发送数据
        ds.send(packet);

        //4.释放资源
        ds.close();
    }

Receive data

    public static void main(String[] args) throws IOException {
    
    
        //1.创建DatagramSocket对象
        DatagramSocket ds = new DatagramSocket(10086);

        //2.接收数据包
        byte[] bytes = new byte[1024];//定义接收数据的数组
        //定义接收数据的容器
        DatagramPacket packet = new DatagramPacket(bytes, bytes.length);
        ds.receive(packet);//该方法是阻塞的,程序执行到这一步的时候,会在这里死等等发送端发送消息

        //3.解析数据包
        //就是获取发送时封装的内容
        byte[] data = packet.getData();
        int len = packet.getLength();
        InetAddress address = packet.getAddress();
        int port = packet.getPort();

        System.out.println("接收该数据"+ new String(data,0,len ));
        System.out.println("该数据是从"+address+"这台电脑的"+port+"这个端口发出的");

        //4.关闭资源
        ds.close();
    }

Note: Run the code for receiving data first, and then run the code for sending data, otherwise, the sent data cannot be received


Three communication methods of UDP

①Unicast One-to-one data transmission, one-to-one correspondence between
sender and receiver


②Multicast The recipient is a group and sends information to a group of computers

insert image description here


The example is as follows

sender

insert image description here

Receiving end

insert image description here


③Broadcast Send data to all computers in the LAN ,
The broadcast address is unique to UDP
insert image description here


TCP

TCP communication protocol

The TCP communication protocol is a reliable network protocol. It needs to establish a Socket object communication at both ends of the communication beforeMake sure the connection is established

Generate IO streams through Socket for network communication

The following is the code implemented in idea using TCP for communication

client

    public static void main(String[] args) throws IOException {
    
    
        //TCP协议发送数据

        //1.创建Socket对象
        //细节:在创建对象的同时会连接服务端
        //     如果连接不上,代码会报错
        Socket socket = new Socket("127.0.0.1",10086);

        //2.从连接通道中获取输出流
        OutputStream os = socket.getOutputStream();
        //写出数据
        os.write("hello".getBytes());

        //3.释放资源
        os.close();
        socket.close();
    }

Server

    public static void main(String[] args) throws IOException {
    
    
        //TCP协议, 接收数据

        //1.创建对象ServerSocker
        ServerSocket ss = new ServerSocket(10000);

        //2.监听客户端的连接
        Socket socket = ss.accept();

        //3.从连接通道中获取输入流读取数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        int b;
        while ((b = br.read()) != -1){
    
    
            System.out.print((char) b);
        }

        //4.释放资源
        socket.close();
        ss.close();
    }

TCP three-way handshake

Make sure the connection is established

insert image description here


TCP's four waves

Make sure the connection is disconnected and the data is processed, which is why the TCP protocol is reliable

insert image description here

This is to confirm whether the server has processed the data, and the disconnection will be confirmed after processing

insert image description here
insert image description here


Guess you like

Origin blog.csdn.net/giveupgivedown/article/details/129012189