JAVA------Three Elements of Network Programming

Three elements of network programming:

  1. IP address
  2. port
  3. protocol

1. IP address

Common commands for IP address:

  • ipconfig: View the local ip address
  • Ping IP address: check whether the network is connected
  • Special IP address: 127.0.0.1, which is the loopback address, which can represent the address of the machine, and is generally used for testing

IP address
IP address: is the unique identifier of the device in the network

IP addresses are divided into two categories:

  1. IPv4: It assigns a 32-bit address to each host connected to the network. According to TCP/IP regulations, IP addresses are expressed in binary, and each IP address is 32 bits long, which is 4 bytes. For example, an IP address in binary form is "10100000 10101000 000000101000010*. Such a long address is too laborious to handle. For ease of use. IP addresses are often written in decimal form with the symbol "" in the middle to separate different bytes So, the above IP address can be expressed as "192.168.1.66". This notation of IP address is called "dotted decimal notation", which is obviously much easier to remember than 1 and 0

  2. IPv6: Due to the vigorous development of the Internet, the demand for IP addresses is increasing, but the limited network address resources make the allocation of IP more and more tense. In order to expand the address space, redefine the address space through IPv6, using a 128-bit address length, each group of 16 bytes, divided into 8 groups of hexadecimal numbers, which solves the problem of insufficient network address resources


Use of InetAddress

In order to facilitate our acquisition and manipulation of IP addresses, Java provides a class InetAddress for us to use
InetAddress: This class represents an Internet Protocol (IP) address

  • static InetAddress getByName(String host): Determine the IP address of the host name.
    The host name can be a machine name or an IP address

  • String getHostName(): Get the host name of this IP address

  • String getHostAddress() returns the IP address string in the text display

Look at the code demo:

netAddress address=InetAddress.getByName("LAPTOP-A61GB327");

This LAPTOP-A61GB327 is the name of my computer, it can also be changed to the IP of the computer

package InteAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InteAdressDemo {
    
    
	public static void main(String[] args) throws UnknownHostException{
    
    
		InetAddress address=InetAddress.getByName("LAPTOP-A61GB327");
		
		String name=address.getHostName();
		
		String ip=address.getHostAddress();
		
		System.out.println("主机名:"+name);
		System.out.println("ip地址:"+ip);
	
	}
}

Second, the port

The port is the unique identifier of the application on the device

Port number: an integer represented by two bytes, its value range is 0~65535. Among them, the port number between 0~1023 is used for some well-known network services and applications, and ordinary applications need to use port numbers above 1024. If the port number is occupied by another service or application, it will cause the current program to fail to start


3. Agreement

Protocol: In computer networks, the rules of connection and communication are called network communication protocols

1. UDP protocol

  • User Datagram Protocol (User Datagram Protocol)
    UDP is a connectionless communication protocol, that is, during data transmission, the sender and receiver of the data do not establish a logical connection. Simply put, when a computer sends data to another computer, the sending end will not confirm whether the receiving end exists, and will send out the data. Similarly, when the receiving end receives the data, it will not give feedback to the sending end. To the data.
    Due to the low resource consumption and high communication efficiency using the UDP protocol, it is usually used for the transmission of audio, video and ordinary data.

  • For example, video conferencing usually uses the UDP protocol, because even if one or two data packets are occasionally lost in this case, it will not have much impact on the reception result. However, when using the UDP protocol to transmit data, due to the connectionlessness of UDP, the integrity of the data cannot be guaranteed. Therefore, it is not recommended to use the UDP protocol when transmitting important data.

2. TCP protocol

  • Transmission Control Protocol (Transmission Control Protocol)
    TCP protocol is a connection-oriented communication protocol, that is, before transmitting data, a logical connection is established between the sending end and the receiving end, and then the data is transmitted. It provides reliable and error-free data between the two computers transmission. In the TCP connection, the client and the server must be clarified, and the client sends a connection request to the server, and each connection creation requires a "three-way handshake"

  • Three-way handshake: In the TCP protocol, in the preparation phase of sending data, three interactions between the client and the server to ensure the reliability of
    the connection. The first handshake, the client sends a connection request to the server and waits for the server to confirm
    the second handshake , The server sends a response back to the client, notifying the client that it has received the connection request
    . The third handshake, the client sends a confirmation message to the server again to confirm the connection.

  • After the three-way handshake is completed and the connection is established, the client and server can begin data transmission. Because of this connection-oriented characteristic, TCP protocol can guarantee the security of data transmission, so it is widely used. Such as uploading files, downloading files, browsing the web, etc.


Guess you like

Origin blog.csdn.net/weixin_45102820/article/details/113777287