The IP address

IP is a unique identifier that identifies every computer on the Internet

Classification:

  1. IPv4:32-bit, divided into four segments (each segment ranges from 0 to 255) in decimal notation. For example: 192.168.1.1 (the number of these four segments is limited, and is not enough, Ipv6 appears)
  2. IPv6:128, paragraph 8, 0000 ~ FFFF hexadecimal values, colon segmentation, such as: 1080:0:0:0:8:8 00:20 0 c: 417 a

Java support for basic networking

1. To represent an IP address, the InetAddress class has two subclasses

  • Inet4Address(IPv4)
  • Inet6Address(IPv6)

2. Common methods (mainly understand the method of obtaining IP address objects)

  • Static InetAddress getLocalHost() returns the local host
  • Static InetAddress getByName(String host) Determines the IP address of the host given the host name. If an IP address exists on the LOCAL area network (LAN), an exception occurs
  • Static InetAddress getByAddress(byte[] addr) getByAddress(byte[] addr) getByAddress(byte[] addr
  • Boolean isReachable(int timeout) Tests whether the address can be reached (similar to ping).

Example:

public class Demo1 {
    public static void main(String[] args) throws IOException {
        // Obtain the local IP address
        InetAddress ip1 = InetAddress.getLocalHost();
        System.out.println(ip1);
        // Obtain the IP address from the IP name
        InetAddress ip2 = InetAddress.getByName("localhost");
        System.out.println(ip2);
        // Get the IP from the byte array
        InetAddress ip3 = InetAddress.getByAddress(new byte[] {(byte) 10, (byte) 185, (byte) 127, (byte) 22});
        System.out.println(ip3);
        // Tests whether the address can be reached
        System.out.println(ip1.isReachable(5000)); }}Copy the code

Results:


port

What is a port?

If an IP address is a house, a port is the door in and out of the house. A real house has only a few doors, but a single IP address can have 65536 ports (i.e. 2^16) as many! Ports are marked by port numbers, which are integers ranging from 0 to 65535 (2^16-1).

Simply put: an application on a computer is bound to a corresponding number (port), through the IP can be located to the specified host (computer), plus the port can be located to the specified program.

Note: Two programs cannot use the same port on the same machine.


URL (Uniform Resource Locator)

Uniform Resource Locator (URL) a Uniform Resource Locator (URL) that can be used to locate resources on the Internet, such as a simple web page. It consists of the following components: protocol name, host where the resource resides, port, and resource name. Such as: baidu.com: 80 / class_info /…

URLEncoder and URLDecoder

String encoding and decoding

public class Demo1 {
    public static void main(String[] args) throws IOException {
        / / code
        String encode = URLEncoder.encode("Progress every day!"."utf-8");
        System.out.println(encode);
        / / decoding
        String decode = URLDecoder.decode(encode,"utf-8"); System.out.println(decode); }}Copy the code

Results:

Differences between TCP and UDP

  1. Connection-based and connectionless;
  2. Requirements on system resources (more TCP and less UDP);
  3. UDP program structure is simple;
  4. Flow mode and datagram mode;
  5. TCP guarantees data correctness, UDP packet loss, TCP guarantees data order, UDP does not guarantee;

TCP protocol:

  • Based on the link
  • Safe and reliable
  • Byte stream
  • slowly

UDP protocol.

  • message
  • Don’t connect
  • unsafe
  • Speed is fast

The HTTP protocol

  1. Request Request object
    • Request header information
  2. Response the corresponding object
    • Information about the response header