Network programming - Understanding

Posted by BAM1979 on Thu, 10 Feb 2022 06:43:36 +0100

computer network

It refers to a computer system in which multiple computers and their external devices with independent functions in different geographical locations are connected through communication lines to realize 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, data exchange, communication

Elements of network communication

  • Address of both parties
  • Rules: protocol of network communication

Network model:

There are two main problems in network programming:

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

Elements in network programming:

  • ip and port number
  • Network communication protocol

ip

ip address: InteAddress

  • 127.0.0.1 local address
  • IP address classification: IP address classification; Public and private networks
public class IPTest {
    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 localHost = InetAddress.getLocalHost();
            System.out.println(localHost);

            //Query website ip address
            InetAddress baidu = InetAddress.getByName("www.baidu.com");
            System.out.println(baidu);
            //common method
            System.out.println(baidu.getAddress());
            System.out.println(baidu.getCanonicalHostName()); //Canonical name
            System.out.println(baidu.getHostAddress()); //ip
            System.out.println(baidu.getHostName()); //domain name
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

Test results:

/127.0.0.1
localhost/127.0.0.1
DESKTOP-IHB2C5F/192.168.136.1
www.baidu.com/110.242.68.3
[B@1b6d3586
110.242.68.3
110.242.68.3
www.baidu.com

Port port

A port represents the process of a program on a computer:

  • Different processes have different port numbers (used to distinguish software)
  • Specified 0-65535
  • TCP; UDP, under a single protocol, the port number cannot conflict
  • Port classification: there are ports 0-1023, Http 80, Https 443, FTP 21 and Telene 23 in total; Program registration port: 1024 ~ 49151, assign users or programs Tomcat 8080, MySQL 3306 and Oracle 1521; Dynamic and private: 49152 ~ 65535
public class PortTest {
    public static void main(String[] args) {
        InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.0.0.1",8080);
        InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost",8080);

        System.out.println(inetSocketAddress1);
        System.out.println(inetSocketAddress2);

        System.out.println(inetSocketAddress1.getAddress());
        System.out.println(inetSocketAddress1.getHostName()); //address
        System.out.println(inetSocketAddress1.getPort()); //port
    }
}

Test results:

/127.0.0.1:8080
localhost/127.0.0.1:8080
/127.0.0.1
activate.navicat.com
8080

communication protocol

Agreement: agreement
Network communication protocol: rate, transmission code rate, transmission control

TCP/IP protocol cluster: it is actually a group of protocols

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

Comparison of TCP and UDP

TCP: call

  • Stable connection
  • Three handshakes and four waves
  • Client and server
  • The transmission is completed and the connection is released, which is inefficient

UDP: send SMS

  • Not connected, unstable
  • Client and server (no clear boundary)
  • It can be sent to you whether it is ready or not
  • DDOS: flood attack! Saturation attack!

Three handshakes:
It takes at least three times to ensure a stable connection

  • A sends a message to B and tells B that a wants to establish a connection with B
  • After receiving A's message, B replies to A and tells A that B has received A's message and agrees to establish A connection
  • After receiving the reply from B, A replies to B and tells B that A is still there. Let's establish A connection

Four waves:

  • A tells B that I want to disconnect
  • B replies to A after receiving it. B receives the message that A wants to disconnect
  • B asks A if he really wants to disconnect
  • A replies to B to confirm disconnection

Topics: network socket Network Protocol