Java provides network-related class libraries, painless networking, and the underlying details are handed over to the JVM control
Java implements a cross-platform network library, our development is facing a unified network programming environment
purpose:
Directly or indirectly communicate and communicate with other computer data through network protocols
main problem:
Accurately locate a computer or multiple computers on the network and locate specific applications on the host
How to transfer data reliably and efficiently after finding the host
Communication elements:
IP and port number
TCP / IP protocol
IP: is the unique identifier of the computer in the network
Local loopback address; 127.0.0.1 host name: LocalHost
IP address classification : IPV4 IPV6
v4-4 bytes, 4 0-255 about 4.2 billion total available, 3 billion in North America, 400 million in Asia, exhausted in 2011,
v6-128-bit 16-byte 8 unsigned integers, each integer is represented by 4 hexadecimal digits, and each number is separated by a colon
Classification by address : public address [use of the World Wide Web] and private address [use of the LAN]
192.168.0.0-192.168.255.255 is used internally by the organization
static void socket01IP_Address() throws UnknownHostException { // DNS Domain Name System // localhost/127.0.0.1 本机地址 InetAddress localhost = InetAddress.getByName("localhost"); System.out.println(localhost); InetAddress a = InetAddress.getByName("www.cnblogs.com"); System.out.println(a); // www.baidu.com/14.215.177.38 // www.acfun.cn/117.21.225.193 // www.bilibili.com/119.3.70.188 // www.cnblogs.com/101.37.113.127 InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost); String address = localHost.getHostAddress(); String hostName = localHost.getHostName(); }
The port number is the process (program) running on the computer
Different processes have different port numbers
Defined as a 16-bit integer 0-65535
Well-known ports 0-1023 Predefined service communications occupy HTTP 80, FTP 21, Telnet 23
Registration ports 1024-49151 are allocated to user processes or applications Tomcat8080, MySQL3306, Oracle1521
Dynamic port / private port 49152-65535
Socket = port number + IP address
TCP
-Establish a connection and form a transmission channel,
-Three-way handshake confirmation, point-to-point communication, reliable
-TCP application process client and server
-Can transfer large data
-The transmission is completed, the established connection is released, and the efficiency is low
UDP
-Data packaging, no connection
-Limit size of 64KB per package
-No response is confirmed regardless of whether the other party is prepared and whether the receiving party receives it
-Broadcast transmission
-At the end of sending, the resources are released out of order, the overhead is small and the speed is fast
TCP
public class TCP_Test { @Test // Server public void server () throws Exception { // Create server program ServerSocket server = new ServerSocket (65000 ); Socket client = server.accept (); // Indicates that the incoming client suite Connection InputStream inputStream = client.getInputStream (); byte [] bytesBuffer = new byte [512 ]; int len; while ((len = inputStream.read (bytesBuffer))! = -1 ) { String str= new String (bytesBuffer, 0 , len); System.out.println (str); } System.out.println ( "Message from server: client has been received:" + client.getInetAddress (). getHostAddress () + "Message" ); // Close the resource inputStream.close (); client.close (); server.close (); } @Test // Client public void client () throws Exception { // This is to connect The other party's socket information, IP + port Socket client = new Socket (InetAddress.getByName ("localhost"), 65000 ); OutputStream outputStream= client.getOutputStream (); outputStream.write ( "Message sent from client: Connect Success !!!" .getBytes ()); // Stream object release outputStream.close (); // Socket release client. close (); } }
UDP
public class UDP_Test { @Test public void send() throws Exception { DatagramSocket ds = new DatagramSocket(); java.lang.String message = "来自发送器的消息:Hello UDP!!!"; byte[] bytes = message.getBytes(); InetAddress localHost = InetAddress.getLocalHost(); DatagramPacket dp = new DatagramPacket(bytes,0,bytes.length,localHost,65000); // 发送包 ds.send(dp); ds.close(); } @Test public void receive() throws Exception{ DatagramSocket ds = new DatagramSocket(65000); byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); ds.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); ds.close(); } }
URL
Uniform Resource Locator Uniform Resource Locator, which represents a resource address of the network
-Is a specific URI, URL can be used to identify a resource, indicating how to locate this resource
-Transfer protocol host name port number file name fragment name? parameter list
The general port number is 80
#Clip name is the anchor point, to locate a specific part
The parameter list is a KV pair? K1 = v1 like this, if there are multiple parameters, use & connect
public class URL_Test { public static void main (String [] args) throws Exception { String url = "https://www.ygo-sem.cn/photo/story-430.aspx" ; URL url_link = new URL (url) ; System.out.println ( "protocol name:" + url_link.getProtocol ()); System.out.println ( "host name:" + url_link.getHost ()); System.out.println ( "port number:" + url_link.getPort ()); // -1 means System.out.println ("file path:" + url_link.getPath ()); System.out.println ( "File name:" + url_link.getFile()); System.out.println("查询名:" + url_link.getQuery()); } }