Java Network Programming: Java Network programming learning summary


preface

This is an information age, in life, network communication is everywhere. Such as texting, emailing, video calling, etc.Take email for example: The recipient:Equivalent to network programmingIP address + port numberLocate to a specific place! You can only send it to the right place, right? 2.Content:Equivalent to network programmingThe packet. That’s what’s being sent. 3.Content language:Quite soNetwork protocolFor example, if I send you something written in English, people who don’t understand English can’t understand the content. Both sides agree on a good format, is the agreement, you understand I understand everyone understand! (4)The sender:Prove who sent it, as opposed to the recipient

A, IP

1.IP address: inetAddress Public:In general if it isNet IPYour IP address must be unique! For example, my Baidu cloud server address:

On the LAN:Generally, each LAN has its own subnet IP address, which is a series of consecutive IP addresses that can be manually configured by the network administrator. Generally is 192.168.×.× this format, mainly depends on the allocation, as long as not repeated can ~Classification of IP:

  • ipv4/ipv6

    • IPV4:127.0.0.1, consisting of four bytes. 0 ~ 25.5, 4.2 billion ~; 3 billion all in North America, 400 million in Asia. It ran out in 2001;

    • IPV6: fe80: : 755 f: fc6c: 2 ebc: b6e6%18128 bits. Eight unsigned integers!

I don’t see it all here, roughlyThe rulesIs this:

2001:0bb2:aaaa:0015:0000:0000:1aaa:1312
Copy the code

If you are interested, you can go to this article: click on the jump

  • ABCD url:

Look at the picture above should understand, the picture is ugly, forgive me ~

Domain Name (English: Domain Name), also known as a Domain, is the Name of a computer or computer group on the Internet composed of a series of names separated by dots. It is used to identify the location of the computer during data transmission (sometimes also refers to the geographical location). The above excerpt from Baidu Encyclopedia is popular: IP address is not good to remember! Good domain name everybody take a glance to remember ah! For example: www.baidu.com to know that a good fisherman but more expensive than the house oh, if I can go back through twenty years of money, I will alibaba jingdong what domain name all registered again, ha ha, rich daydream!

InetAddress: the main function of the class is to encapsulate IP and DNS, because this class does not have a constructor, so we will use some of its methods to obtain objects.

public class Test01 {
    public static void main(String[] args) throws UnknownHostException {
        // Use InetAddress to get it
        InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
        System.out.println("inetAddress:"+inetAddress);

        //localhost is the same as 127.0.0.1
        InetAddress inetAddress1 = InetAddress.getByName("localhost");
        System.out.println("inetAddress1:"+inetAddress1);

        The getLocalHost method obtains the local IP address
        InetAddress inetAddress2 = InetAddress.getLocalHost();
        System.out.println("inetAddress2:"+inetAddress2);

        // Query the website IP address, this is a bit slower, because the link is required
        InetAddress inetAddress3 = InetAddress.getByName("www.baidu.com");
        System.out.println("inetAddress3:"+inetAddress3);

        // Common methods
        System.out.println(inetAddress.getCanonicalHostName());// The name of the specification
        System.out.println(inetAddress.getHostAddress());  //ip
        System.out.println(inetAddress.getHostName()); // Domain name, or the name of your computer}}Copy the code

The result is as follows:

Ii. Port number

A port represents a process on a computer, and each process has its own port number.

  • Different processes have different port numbers to distinguish between processes!
  • 0 to 065535 is specified
  • Different protocols can be the same. TCP, UDP: 65535 x 2. In a single protocol, port numbers cannot conflict
  • Port classification
    • Public ports 0 to 1023
    • 80 HTTP:
    • HTTPS: 443
    • FTP: 21
    • Telent: 23
  • Program registration ports: 1024 to 49151, assigned to users or programs
    • Tomcat: 8080
    • MySQL: 3306
    • The Oracle: 1512
  • Dynamic, private: 49152 to 65535

Code example: The InetSocketAddress class encapsulates ports. It adds ports to InetAddress, but it has a constructor. Some specific methods can go to help document view.

public class Test02 {
    public static void main(String[] args) {

        InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1".8080);
        System.out.println(socketAddress);

        // Common methodsSystem.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getPort()); }}Copy the code

The result is as follows:

Communication protocol

Agreement = agreement Network Communication protocol:Rate, transmission bit rate, transmission control… 6. TCP/IP protocol cluster: a group of protocols: TCP/UDP difference:

  • TCP: user transport protocol
    • Make a phone call
    • Three handshakes and four waves
    • Client, server
    • Transmission completed, release connection, low efficiency
  • UDP: user datagram protocol
    • Not connected, unstable
    • Client side, server side: there is no clear boundary, the two can be changed at will
    • Ready or not, I can send it to you… If you want it or not, it’s not up to you. I want to give it to you. It’s none of my business
    • DDOS: Flood attack! (Saturation attack)

Why is it called TCP/IP? Because these two are the most famous…

Three handshakes and four waves of the hand.

  • Official explanation:
    • Three-way handshake
    • Four waves:

To be honest, my head hurts too.

  • 解 析 :
    • Three handshakes:

A minimum of three times is required to ensure a stable connection! ①A: What do you look at? ②B: What do you think? ③A: Let’s do it!

Valentine's Day version: ①A: Break up! ②B: Do you really want to break up? ③B :(confirms once) do you really want to break up? ④A: Break up!Copy the code

The road ahead is long, I see no end, I will search high and low

If you think I bloggers write well! Writing is not easy, please like, follow, comment and give encouragement to the blogger hahah~