Java network programming 01: communication protocol

Java network programming 01: communication protocol

summary

Computer network:

Computer network refers to a computer system that connects multiple computers and their external devices with independent functions 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.

Purpose of network programming:

Radio stations... Transmit and exchange information and data. communication

What is required to achieve this effect:

  1. How to accurately locate a host on the network: [ip address]: port, and locate a resource on the computer.
  2. After finding this host, how can I transfer data?

Elements of network communication

How to realize network communication?

Through the addresses of both parties:

  • ip
  • Port number

Rules: protocol for network communication

Summary:

1. two main problems in network programming

  • How to accurately locate one or more hosts on the network
  • How to communicate after finding the host

2. elements in network programming

  • IP and port numbers
  • Network communication protocols TCP and UDP

3. all things are objects

IP

ip address: InetAddress

  • Uniquely locate a computer on a network
  • 127.0.0.1: local localhost
  • Classification of ip addresses
    • ipv4/ipv6
      • ipv4:127.0.0.1, consisting of 4 bytes. 0~25.5, 4.2 billion; Three billion are in North America and 400million in Asia. Exhausted in 2011;
      • ipv6:128 bit. 8 unsigned integers!
    • Public network (Internet) - private network (LAN)
      • 192.168 Xx XX, for internal use of the organization
  • Domain name: memory ip problem!

Case:

//Test IP
public class TestInetAddress {

    public static void main(String[] args) {
        try {
            //Query local address
            InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress1);
            InetAddress inetAddress2 = InetAddress.getByName("localhost");
            System.out.println(inetAddress2);
            InetAddress inetAddress3 = InetAddress.getLocalHost();
            System.out.println(inetAddress3);


            //Query website ip address
            InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress4);

            //common method
            System.out.println(inetAddress4.getAddress());
            System.out.println(inetAddress4.getCanonicalHostName()); //Name of the specification
            System.out.println(inetAddress4.getHostAddress());//ip
            System.out.println(inetAddress4.getHostName());//Domain name or computer name
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

port

Port represents the process of a program on the computer

  • Different processes have different port numbers! Used to distinguish software!

  • Specified 0~65535

  • TCP, UDP:65535*2, tcp:80, UDP:80. Port numbers cannot conflict under a single protocol

  • Port classification

    • Public port 0~1023

      • HTTP: 80
      • HTTPS: 443
      • FTP: 21
      • Telent: 23
    • Program registration port: 1024~49151, assign users or programs

      • Tomcat: 8080
      • MySQl: 3306
      • Oracle: 1521
    • Dynamic and private: 49152~65535

      netstat -ano #View all ports
      netstat -ano|findstr "5900" #View the specified port
      taskList|findstr "8696" #View processes for the specified port
      
      public class TestInetSockeAddress {
          public static void main(String[] args) {
              InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",80);
              System.out.println(socketAddress);
              InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080);
              System.out.println(socketAddress2);
      
              System.out.println(socketAddress.getAddress());
              System.out.println(socketAddress.getHostName()); //address
              System.out.println(socketAddress.getPort()); //port
          }
      }
      

communication protocol

Agreement: agreement, just like the Mandarin we speak now

Network communication protocol: rate, transmission code rate, code structure, transmission control

Question: very complicated?

Make things small: layered!

TCP/IP protocol cluster: actually a group of protocols

Important:

  • TCP: user transport protocol
  • UDP: User Datagram Protocol

TCP UDP comparison

TCP: making phone calls

  • Even, stable

  • Three handshakes and four waves

    At least three times to ensure a stable connection!
    A: What are you worried about?
    B: What do you think?
    A: Have a fight!!!!
    
    A: I'm leaving!
    B: Are you really leaving?
    B: Are you really leaving?
    A: I really, really have to go!
    
  • Client and server

  • Transmission completed, connection released, low efficiency

UDP: send SMS

  • Not connected, unstable
  • Client and server: no clear boundary
  • Ready or not, it can be sent to you

Tags: Java network Network Protocol

Posted by rskandarpa on Mon, 30 May 2022 09:15:06 +0530