Java network programming, socket socket

Table of contents

A network overview

Two types of network classification

Three network architecture

Four Network Communication Protocol Overview

Five types of network communication protocols

Six Socket Introduction

Seven Socket paths

Eight three elements of java network programming

Nine Socket programming based on UDP protocol

Ten Socket programming based on TCP protocol

11 The difference between TCP protocol and UDP


A network overview

  1. multiple interconnected computers
  2. Resource Sharing
  3. exchange data

Two types of network classification

Three network architecture

Four Network Communication Protocol Overview

        Core points: In the network, if multiple computers want to communicate/data transfer, they must follow a certain protocol. If they do not follow, resource sharing or data transfer cannot be performed.

Five types of network communication protocols

        UDP features: connectionless, data unreliable/unsafe

        TCP features: connection-oriented, reliable/secure data

Six Socket Introduction

  1. Socket: socket [node or endpoint of a communication link]
  2. Socket: the interface provided to the application

Seven Socket paths

        java.net package path

Eight three elements of java network programming

  1. IP address: the unique identification of the device in the network
  2. Port number: the unique identification of the application in the device
  3. Protocol: the protocol that information must follow in network transmission, such as UDP, TCP protocol

Nine Socket programming based on UDP protocol

Send segment:

package org.example.demo03;


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

public class SendMsg {
    public static void main(String[] args) throws IOException {
        DatagramSocket datagramSocket = new DatagramSocket();
        String st = "jojo";
        byte[]bytes = st.getBytes();

        InetAddress address = InetAddress.getByName("127.0.0.1");

        int port = 10086;

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

        datagramSocket.send(datagramPacket);
        datagramSocket.close();
    }
}

Receiving end:

package org.example.demo03;

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

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

        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        datagramSocket.receive(datagramPacket);

        byte[]data = datagramPacket.getData();
        int length = datagramPacket.getLength();

        System.out.println("接收到的数据"+new String(data,0,length));
        datagramSocket.close();
    }
}

 

Ten Socket programming based on TCP protocol

sender:

package org.example.demo04;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

public class SendMsg {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1",10001);
        OutputStream os = socket.getOutputStream();
        os.write("乔尼".getBytes());

        os.close();
        socket.close();
    }

}

Receiving end:

package org.example.demo04;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ReceMsg {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(10086);

        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        int b;
        while ((b = isr.read()) != -1) {
            System.out.print((char)b);
        }
        is.close();
        ss.close();

    }
}

11 The difference between TCP protocol and UDP

Guess you like

Origin blog.csdn.net/jojo_oulaoula/article/details/132722296