1. What is network?
In the field of computer, network is a virtual platform for information transmission, reception and sharing. Through it, all points, faces and bodies are linked together
So as to realize the sharing of these resources
Function: information transmission and resource sharing
Advantages: sharing resources is free
2.OSI seven layer reference model and TCP/IP four layer reference model
Computer network is a collection of many autonomous computers connected by communication lines. What rules are used for communication between various components is the problem of network model research
Network model generally refers to OSI seven layer reference model and TCP/IP four layer reference model. These two models are most widely used in the network.
OSI seven layer reference model: application layer / presentation layer / session layer / transport layer / network layer / data link layer / physical layer
TCP/IP four layer reference model: application layer, transport layer, IP layer [Internet layer], network interface layer [network connection layer]
3. Four elements of computer network
1.IP address
All communication devices (computers, mobile phones, televisions, etc.) in the network will be assigned an IP address. Before communication, these devices must first be connected to the network through the network card of the communication device. This network card belongs to a hardware, and all network cards have a fixed network card address when they leave the factory, and this network card address (MAC address) is unique in the world.
View network card address (MAC address)
1. Enter ipconfig/all in the command prompt window to query the information details related to ip(MAC address)
2.Linux operating system can query ip (mac address) related details by using ifconfig
Relationship between fixed network card address (MAC address) and IP address
For example: fixed network card address ((MAC address)) - [00-0C-29-7A-CA-8E]
IP address ---------------------- [192.168.1.220]
All network card addresses have a unique physical address, but this address is very difficult to remember, so each network card is assigned a logical (IP) address. This logical address is the IP address. In the process of communication, you can find the device in the network according to this logical address
The IP address is also the identification of a communication terminal in the network.
Relationship between IP address and domain name
Because the IP address is not easy to remember, a name is bound to each, which is called (domain name).
[00-0C-29-7A-CA-8E]----[192.168.1.220]----https://www.baidu.com/ [domain name]
The default ip address of this machine is 127.0.0.1; It has a corresponding default domain name localhost.
2.DNS server [domain name resolution server]
There are 2 steps for domain name resolution:
1. Local resolution:
In our operating system, there is a hosts file. After entering the domain name, we will first find out whether there is an IP address corresponding to the current domain name from the hosts file. If there is, we will use this IP address. C:\Windows\System32\drivers\etc\hosts”
2.DNS server resolution:
If the first step of resolution fails, it will be resolved to the DNS server in the network. The DNS server will set all domain names and IP addresses in the world. If the DNS server fails to resolve, there is a problem with the current domain name.
3. Agreement
Agreement: both parties need to abide by the rules of communication when communicating. Later, we will encounter many protocols (Advanced protocols, application protocols: http, ftp, https)
The protocol we introduced in Java network programming belongs to the underlying protocol [TCP/UDP], and all high-level protocols are based on the underlying protocol.
4. Port
The specific communication terminal equipment in the network can be found through IP, and the specific resource (software) information in the equipment that needs to be accessed. Since there must be many running resources (software), each resource must be given a unique number (ID). Through this ID, we can ensure that we do not access the specific resource with the specified IP address incorrectly
Port: it is the unique identification of an application on a device (computer, mobile phone). Any software running in the equipment will have a unique number bound to its whole software as long as it is started.
The port starts from 0 to 65535. The port number between 0 and 1024 has been allocated to the application of the local operating system. Therefore, if the program needs to bind the port later, it must be greater than 1024. In fact, the port is the access to the program running on the computer when entering the local computer.
4. Use of InetAddress class
java.net package class InetAddress This class represents an Internet Protocol (IP) address.
public class InetAddress implements java.io.Serializable
Constructor of InetAddress class
Default modifier InetAddress() {}
In our own Java programs, we cannot create InetAddress class objects through new + construction methods.
We need to use the following two static methods to get the InetAddress class object.
static InetAddress getLocalHost() returns the InetAddress object containing the native IP address.
static InetAddress getByName(String host) obtain the ip address corresponding to the domain name according to the domain name InetAddress object.
Instance method:
String getHostAddress() returns the IP address string (expressed as text).
String getHostName() gets the host name of this IP address
For example:
package com.wangxing.test1; import java.net.InetAddress; import java.net.UnknownHostException; public class TestMain { public static void main(String[] args) throws UnknownHostException { //Get the local IP address and computer name //Get the InetAddress object containing the native IP address //static InetAddress getLocalHost() returns the InetAddress object containing the native IP address InetAddress bjInetAddress=InetAddress.getLocalHost(); //Get the local IP address String benjiIp=bjInetAddress.getHostAddress(); System.out.println("Local machine IP: "+benjiIp); //Get the local computer name String benjiname=bjInetAddress.getHostName(); System.out.println("Local computer name:"+benjiname); //Get Baidu's IP address and computer name String host="baidu.com"; //static InetAddress getByName(String host) get the InetAddress object containing the ip address corresponding to the domain name according to the domain name. InetAddress baiduInetAddress=InetAddress.getByName(host); //Get Baidu's ip address String babiduIP=baiduInetAddress.getHostAddress(); System.out.println("Baidu IP: "+benjiIp); //Get Baidu's computer name String baiduname=baiduInetAddress.getHostName(); System.out.println("Baidu computing Name:"+baiduname); } }
5. What is the difference between URL and URI?
1.URI - Uniform Resource Identifier (URI), which represents every available resource on the web, such as HTML document and image. Video clips, programs, etc. are identified by a URL
A URL usually consists of three parts:
1. Naming mechanism of resources;
2. Host name for storing resources;
3. Name of the resource itself.
2. URLs are a subset of URI s. It is the abbreviation of Uniform Resource Locator, which is translated as "Uniform Resource Locator".
URL can be used to describe various information resources in a unified format, including files, server addresses and directories.
A URL is an implementation of the URI concept.
The format of the URL consists of three parts:
1. The first part is the agreement (or service mode)
2. The second part is the host IP address where the resource is stored
3. The third part is the specific address of the host resource, such as directory and file name
The first part and the second part are separated by a ": / /" symbol,
The second and third parts are separated by "/".
The first and second parts are indispensable, and the third part can sometimes be omitted.
For example: https://www.baidu.com/index.html--- Resource access address
From the above example, you may think that URI and URL may have the same concept. In fact, both URI and URL define what resources are, but URL also defines how to access resources. URL is a specific URI, which is a subset of URIs. It not only uniquely identifies a resource, but also provides information to locate the resource. Uri is a semantic abstract concept, which can be absolute or relative, while URL must provide enough information to locate, which is absolute.
6. URL class in Java [access network resources]
URL construction method:
URL(String spec) -- form a URL object from the String representation
URL(String protocol,String host,String file) -- create a URL from the specified objects: protocol, host, port, and file
Instance method:
URLConnection openConnection() -- returns an urlconnection instance that represents the URL of the remote object referenced by the URL (open a URL connection)
InputStream openStream() opens the URL and returns an InputStream to read resources from the connection. (open the InputStream associated with this URL)
For example:
package com.wangxing.test2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; public class TestMain { public static void main(String[] args) throws IOException{ String path="https://img14.360buyimg.com/n0/jfs/t1/219743/31/983/108306/616e758aEc8fea4d3/1a18abe36a5d8ce9.jpg"; //Create URL object URL url=new URL(path); //Create input stream InputStream in=url.openStream(); //A byte array that holds the specific data read byte data[]=new byte[1024]; //Save the specific number of bytes read int temp=0; //Create a file File file=new File("D:"+File.separator+"test1.jpg"); //Create an output stream and export the data to a file FileOutputStream fileout=new FileOutputStream(file); //Read data while((temp=in.read(data))!=-1){ fileout.write(data,0,temp); } fileout.close(); in.close(); } }
package com.wangxing.test2; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; import java.net.UnknownHostException; public class TestMain2 { public static void main(String[] args) throws IOException{ String path="https://img14.360buyimg.com/n0/jfs/t1/219743/31/983/108306/616e758aEc8fea4d3/1a18abe36a5d8ce9.jpg"; //Create URL object URL url=new URL(path); //Create input stream InputStream in=url.openStream(); //A byte array that holds the specific data read byte data[]=new byte[1024]; //Save the specific number of bytes read int temp=0; //Create a memory output stream object. Output data to memory ByteArrayOutputStream out=new ByteArrayOutputStream(); //Read data while((temp=in.read(data))!=-1){ out.write(data,0,temp); } //Create a file File file=new File("D:"+File.separator+"test.jpg"); //Create an output stream and export the data to a file FileOutputStream fileout=new FileOutputStream(file); fileout.write(out.toByteArray()); fileout.close(); out.close(); in.close(); } }