Overview of network programming
the network class library provided by java can realize painless network connection. The underlying details of networking are hidden in the Java Native installation system and controlled by the JVM. And Java implements a cross platform network library. Programmers face a unified network programming environment.
purpose of network programming: realize data exchange and communication with other computers directly or indirectly through network protocol.
there are two main problems in network programming:
>How to accurately locate one or more hosts on the network; Locate a specific application on the host
>How to transfer data reliably and efficiently after finding the host
Overview of network communication elements
Two elements in network programming:
1. Corresponding question 1: IP and port number
2. Corresponding question 2: provide network communication protocol: TCP/IP reference model (application layer, transport layer, network layer, physical + data link layer)
address of both parties
>IP
>Port number
certain rules (i.e. network communication protocol. There are two sets of reference models)
>OSI reference model: the model is too idealized to be widely popularized on the Internet
>TCP/IP reference model (or TCP/IP Protocol): a de facto international standard.
Communication element 1: IP and port number
-
IP: uniquely identifies the computer (communication entity) on the Internet
-
Use the InetAddress class to represent IP in Java
-
IP Classification: IPv4 and IPv6; World Wide Web and local area network
-
Domain name: www.baidu.com com www.mi. com www.sina. com www.jd. com www.vip. com
-
Local loop address: 127.0 0.1 corresponds to localhost
-
How to instantiate InetAddress: two methods: getByName(String host) and getLocalHost()
There are two common methods: getHostName() to get the local domain name / getHostAddress() to get the local address -
Port number: the process running on the computer.
Requirement: different processes have different port numbers
Range: specified as a 16 bit integer 0 ~ 65535. -
The combination of port number and IP address yields a network Socket: Socket
Some IP and port numbers are shown below.
public class InetAddressTest { public static void main(String[] args) { try { //File file = new File("hello.txt"); InetAddress inet1 = InetAddress.getByName("192.168.10.14"); System.out.println(inet1); InetAddress inet2 = InetAddress.getByName("www.atguigu.com"); System.out.println(inet2); InetAddress inet3 = InetAddress.getByName("127.0.0.1"); System.out.println(inet3); //Get local ip InetAddress inet4 = InetAddress.getLocalHost(); System.out.println(inet4); //getHostName() System.out.println(inet2.getHostName()); //getHostAddress() System.out.println(inet2.getHostAddress()); } catch (UnknownHostException e) { e.printStackTrace(); } } }
Communication element 2: network protocol
network communication protocol
There must be some conventions to realize communication in computer network, that is, communication protocol, and formulate standards for rate, transmission code, code structure, transmission control steps, error control, etc.
problem: the network protocol is too complex
Computer network communication involves many contents, such as specifying source address and target address, encryption and decryption, compression and decompression, error control, flow control and routing control. How to realize such a complex network protocol?
the idea of communication protocol layering
When making an agreement, break down the complex components into some simple components, and then combine them. The most commonly used composite method is the hierarchical method, that is, the same layer can communicate, the upper layer can call the next layer, and there is no relationship with the next layer. Each layer does not affect each other, which is conducive to the development and expansion of the system.
there are two very important protocols in the transport layer protocol:
- Transmission control protocol (TCP)
- User datagram protocol (UDP).
TCP/IP is named after its two main protocols: transmission control protocol (TCP) and network interconnection protocol (IP). It is actually a group of protocols, including multiple protocols with different functions and related to each other.
IP(Internet Protocol) protocol is the main protocol of network layer, which supports data communication of interconnection between networks.
from a more practical point of view, TCP/IP protocol model has formed an efficient four-layer architecture, namely physical link layer, IP layer, transport layer and application layer.
TCP protocol:
- Before using TCP protocol, a TCP connection must be established to form a data transmission channel
- Before transmission, the "triple handshake" mode is adopted for point-to-point communication, which is reliable
- Two application processes communicating with TCP protocol: client and server.
- A large amount of data can be transmitted in the connection
- After transmission, the established connection needs to be released, which is inefficient
UDP protocol:
- Encapsulate the data, source and destination into packets without establishing a connection
- The size of each datagram is limited to 64K
- Whether the sending party is ready or not, the receiving party does not confirm the receipt, so it is unreliable
- Can broadcast and send
- At the end of sending data, there is no need to release resources, low overhead and high speed
Socket
using socket to develop network applications has long been widely used, so that it has become a de facto standard.
the IP address and port number with unique identification on the network can be combined to form a unique identifier socket.
sockets shall be provided at both ends of the communication, which are the communication endpoints between two machines.
network communication is actually the communication between sockets.
Socket allows the program to treat the network connection as a stream, and the data is transmitted between the two sockets through IO.
generally, the application that initiates communication actively belongs to the client, and the one waiting for communication request is the server.
Socket classification:
- stream socket: use TCP to provide reliable byte stream services
- datagram socket: provide "best effort" datagram service using UDP
Common constructors of Socket class:
- public Socket(InetAddress address,int port) creates a stream socket and connects it to the specified port number of the specified IP address.
- public Socket(String host,int port) creates a stream socket and connects it to the specified port number on the specified host.
Common methods of Socket class:
- public InputStream getInputStream() returns the input stream of this socket. Can be used to receive network messages
- public OutputStream getOutputStream() returns the output stream of this socket. Can be used to send network messages
- public InetAddress getInetAddress() the remote IP address to which this socket is connected; Returns null if the socket is not connected.
- public InetAddress getLocalAddress() gets the local address of the socket binding. That is, the IP address of the local end
- public int getPort() the remote port number to which this socket is connected; Returns 0 if the socket is not connected.
- public int getLocalPort() returns the local port to which this socket is bound. Returns - 1 if the socket has not been bound. That is, the port number of this end.
- public void close() closes the socket. After the socket is closed, it cannot be used in future network connections (i.e. it cannot be reconnected or rebound). A new socket object needs to be created. Closing this socket will also close the InputStream and input stream of the socket
OutputStream. - public void shutdownInput() if you call shutdownInput() on a socket and read from the socket input stream, the stream will return EOF (end of file). That is, no data can be received in the input stream from this socket.
- public void shutdownOutput() disables the output stream for this socket. For TCP sockets, any previously written data will be sent, followed by TCP's normal connection termination sequence. If the socket output stream is written after calling shutdown output() on the socket, the stream will throw an IOException. That is, no data can be sent through the output stream of this socket.
TCP network programming (during transmission)
TCP programming based on Socket
Socket based programming in Java language is divided into server-side programming and client-side programming.
The working process of the client Socket includes the following four basic steps:
create Socket: construct Socket class objects according to the IP address or port number of the specified server. If the server responds, a communication line from the client to the server is established. If the connection fails, an exception will appear.
open the input / output stream connected to the Socket: use getInputStream() method to obtain the input stream and getOutputStream() method to obtain the output stream for data transmission
read / write the Socket according to a certain protocol: read the information put into the line by the server through the input stream (but cannot read the information put into the line by itself), and write the information into the thread through the output stream.
close the Socket: disconnect the client from the server and release the line
The client creates a Socket object
the client program can use the Socket class to create objects, which will automatically initiate a connection to the server at the same time.
The constructor of the Socket is:
Socket(String host,int port)throws UnknownHostException,IOException: initiate a TCP connection to the server (the domain name is host and the port number is port). If successful, create a Socket object, otherwise an exception will be thrown.
Socket(InetAddress address,int port)throws IOException: initiate the connection according to the IP address and port number represented by the InetAddress object.
the process that the client establishes the socketAtClient object is to send a socket connection request to the server
The server creates a ServerSocket object
the ServerSocket object is responsible for waiting for the client to request to establish a socket connection, similar to the salesman in a window of the post office. In other words, the server must establish a ServerSocket object waiting for the client's request to establish a socket connection in advance.
the so-called "receiving" the client's Socket request means that the accept() method will return a Socket object
The following shows that some clients send information to the server, which displays the data on the console.
public class TCPTest1 { //client @Test public void client() { Socket socket = null; OutputStream os = null; try { //1. Create a Socket object to indicate the ip and port number of the server InetAddress inet = InetAddress.getByName("192.168.14.100"); socket = new Socket(inet,8899); //2. Obtain an output stream for outputting data os = socket.getOutputStream(); //3. Write data os.write("Hello, this is the client mm".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { //4. Closure of resources if(os != null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } //Server @Test public void server() { ServerSocket ss = null; Socket socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1. Create a ServerSocket on the server side and indicate its own port number ss = new ServerSocket(8899); //2. Call accept() to receive the socket from the client socket = ss.accept(); //3. Get input stream is = socket.getInputStream(); //It is not recommended to write like this. There may be garbled code // byte[] buffer = new byte[1024]; // int len; // while((len = is.read(buffer)) != -1){ // String str = new String(buffer,0,len); // System.out.print(str); // } //4. Read the data in the input stream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[5]; int len; while((len = is.read(buffer)) != -1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); System.out.println("Received from:" + socket.getInetAddress().getHostAddress() + "Data"); } catch (IOException e) { e.printStackTrace(); } finally { if(baos != null){ //5. Close resources try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(socket != null){ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } if(ss != null){ try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
The following shows some clients sending files to the server, which saves the files locally.
public class TCPTest2 { /* The exceptions involved here should be handled with try catch finally */ @Test public void client() throws IOException { //1. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); //2. OutputStream os = socket.getOutputStream(); //3. FileInputStream fis = new FileInputStream(new File("beauty.jpg")); //4. byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ os.write(buffer,0,len); } //5. fis.close(); os.close(); socket.close(); } /* The exceptions involved here should be handled with try catch finally */ @Test public void server() throws IOException { //1. ServerSocket ss = new ServerSocket(9090); //2. Socket socket = ss.accept(); //3. InputStream is = socket.getInputStream(); //4. FileOutputStream fos = new FileOutputStream(new File("beauty1.jpg")); //5. byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer,0,len); } //6. fos.close(); is.close(); socket.close(); ss.close(); } }
The following shows some files sent from the client to the server, and the server saves them locally. And return "send successfully" to the client. And close the corresponding connection..
public class TCPTest3 { /* The exceptions involved here should be handled with try catch finally */ @Test public void client() throws IOException { //1. Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090); //2. OutputStream os = socket.getOutputStream(); //3. FileInputStream fis = new FileInputStream(new File("beauty.jpg")); //4. byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ os.write(buffer,0,len); } //Turn off data output socket.shutdownOutput(); //5. Receive data from the server and display it on the console InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] bufferr = new byte[20]; int len1; while((len1 = is.read(buffer)) != -1){ baos.write(buffer,0,len1); } System.out.println(baos.toString()); //6. fis.close(); os.close(); socket.close(); baos.close(); } /* The exceptions involved here should be handled with try catch finally */ @Test public void server() throws IOException { //1. ServerSocket ss = new ServerSocket(9090); //2. Socket socket = ss.accept(); //3. InputStream is = socket.getInputStream(); //4. FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg")); //5. byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer,0,len); } System.out.println("Picture transfer complete"); //6. The server gives feedback to the client OutputStream os = socket.getOutputStream(); os.write("Hello, beauty, I have received the photo. It's very beautiful!".getBytes()); //7. fos.close(); is.close(); socket.close(); ss.close(); os.close(); } }
UDP network programming (during transmission)
UDP network communication
classes datagram socket and datagram packet implement network programs based on UDP protocol.
UDP datagrams are sent and received through datagram socket. The system does not guarantee that UDP datagrams can be safely sent to the destination, nor can it determine when they can arrive.
the datagram packet object encapsulates UDP datagrams, which contain the IP address and port number of the sending end and the IP address and port number of the receiving end.
each datagram in UDP protocol gives complete address information, so there is no need to establish a connection between the sender and the receiver. It's like sending an express package.
Common methods of DatagramSocket class
public datagram socket (int port) creates a datagram socket and binds it to the specified port on the local host. The socket is bound to a wildcard address, and the IP address is selected by the kernel.
public datagram socket (int port, InetAddress, ladder) creates a datagram socket and binds it to the specified local address. The local port must be between 0 and 65535 (including both). If the IP address is 0.0.0.0, the socket will be bound to the wildcard address, which is selected by the kernel.
public void close() closes the datagram socket.
public void send (datagram packet P) sends datagram packets from this socket. The datagram packet contains information indicating the data to be sent, its length, the IP address of the remote host and the port number of the remote host.
public void receive (datagram packet P) receives datagram packets from this socket. When this method returns, the buffer of datagram packet is filled with the received data. The datagram packet also contains the IP address of the sender and the port number on the sender's machine. This method blocks until a datagram is received. The length field of the datagram packet object contains the length of the received information. If the message is longer than the length of the packet, the message will be truncated.
public InetAddress getLocalAddress() gets the local address of the socket binding.
public int getLocalPort() returns the port number on the local host to which this socket is bound.
public InetAddress getInetAddress() returns the address of this socket connection. Returns null if the socket is not connected.
public int getPort() returns the port of this socket. If the socket is not connected, - 1 is returned.
Common methods of DatagramPacket class
public datagram packet (byte [] buf, int length) constructs datagram packet to receive data packets with length. The length parameter must be less than or equal to buf length.
public datagram packet (byte [] buf, int length, InetAddress, address, int port) constructs datagram packet, which is used to send the packet with length to the specified port number on the specified host. The length parameter must be less than or equal to buf length.
public InetAddress getAddress() returns the IP address of a machine. This datagram will be sent to or received from the machine.
public int getPort() returns the port number of a remote host. This datagram will be sent to or received from the host.
public byte[] getData() returns the data buffer. The data received or to be transmitted starts at the offset in the buffer and lasts for length.
public int getLength() returns the length of the data to be sent or received.
process:
- Datagram socket and datagram packet
- Establish sender and receiver
- Create packet
- Call Socket sending and receiving methods
- Close Socket
the sender and receiver are two independent running programs
The following shows some network programming of UDPd protocol.
public class UDPTest { //Sender @Test public void sender() throws IOException { DatagramSocket socket = new DatagramSocket(); String str = "I am UDP Missile sent by"; byte[] data = str.getBytes(); InetAddress inet = InetAddress.getLocalHost(); DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090); socket.send(packet); socket.close(); } //receiving end @Test public void receiver() throws IOException { DatagramSocket socket = new DatagramSocket(9090); byte[] buffer = new byte[100]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); socket.receive(packet); System.out.println(new String(packet.getData(),0,packet.getLength())); socket.close(); } }
URL programming
URL class
URL(Uniform Resource Locator): uniform resource locator, which represents the address of a resource on the Internet.
it is a specific URI, that is, the URL can be used to identify a resource, and it also indicates how to locate the resource.
through the URL, we can access various network resources on the Internet, such as the most common www and ftp sites. The browser can find the corresponding files or other resources on the network by parsing the given URL.
the basic structure of URL consists of five parts:
- < transport protocol > / / < host name >: < port number > / < file name > # fragment name? parameter list
- For example: http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123
- #Segment name: that is, the anchor point, such as reading a novel, is directly positioned to the chapter
- Parameter list format: parameter name = parameter value & parameter name = parameter value
to represent the URL, Java Net. We can initialize a URL object through the following constructor:
public URL (String spec): a URL object can be constructed through a string representing the URL address.
- For example: url = new URL(“ http://www. atguigu.com/”);
public URL(URL context, String spec): construct a URL object through base URL and relative URL. - For example: URL downloadUrl = new URL(url, "download.html")
public URL(String protocol, String host, String file); - For example: new URL("http", "www.atguigu.com", "download. html");
public URL(String protocol, String host, int port, String file); - For example: URL gamelan = new URL("http", "www.atguigu.com", 80, "download.html");
the constructors of URL classes declare that they throw non runtime exceptions, which must be handled, usually captured with try catch statements.
after a URL object is generated, its properties cannot be changed, but these properties can be obtained through its given method:
- public String getProtocol() gets the protocol name of the URL
- public String getHost() gets the host name of the URL
- public String getPort() gets the port number of the URL
- public String getPath() gets the file path of the URL
- public String getFile() gets the file name of the URL
- public String getQuery() gets the query name of the URL
URLConnection class for HTTP protocol
URL method openStream(): it can read data from the network
if you want to output data, for example, send some data to the server-side CGI (short for common gateway interface, which is the interface between the user browser and the server-side application), you must establish a connection with the URL before you can read and write it. At this time, you need to use URLConnection.
URLConnection: indicates the connection to the remote object referenced by the URL. When establishing a connection with a URL, first generate the corresponding URLConnection object on a URL object through the method openConnection(). If the connection process fails, an IOException will be generated
- URL netchinaren = new URL ("http://www.atguigu.com/index.shtml");
- URLConnectonn u = netchinaren.openConnection( );
the input stream and output stream obtained through URLConnection object can interact with existing CGI programs. - public Object getContent( ) throws IOException
- public int getContentLength( )
- public String getContentType( )
- public long getDate( )
- public long getLastModified( )
- public InputStream getInputStream( )throws IOException
- public OutputSteram getOutputStream( )throws IOException
Here is a brief introduction to some URL s.
public class URLTest { public static void main(String[] args) { try { URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom"); // public String getProtocol() gets the protocol name of the URL System.out.println(url.getProtocol()); // public String getHost() gets the host name of the URL System.out.println(url.getHost()); // public String getPort() gets the port number of the URL System.out.println(url.getPort()); // public String getPath() gets the file path of the URL System.out.println(url.getPath()); // public String getFile() gets the file name of the URL System.out.println(url.getFile()); // public String getQuery() gets the query name of the URL System.out.println(url.getQuery()); } catch (MalformedURLException e) { e.printStackTrace(); } } }
Here are some URL network programming 2.
public class URLTest1 { public static void main(String[] args) { HttpURLConnection urlConnection = null; InputStream is = null; FileOutputStream fos = null; try { URL url = new URL("http://localhost:8080/examples/beauty.jpg"); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.connect(); is = urlConnection.getInputStream(); fos = new FileOutputStream("day10\\beauty3.jpg"); byte[] buffer = new byte[1024]; int len; while((len = is.read(buffer)) != -1){ fos.write(buffer,0,len); } System.out.println("Download complete"); } catch (IOException e) { e.printStackTrace(); } finally { //close resource if(is != null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if(fos != null){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } if(urlConnection != null){ urlConnection.disconnect(); } } } }
The difference between URI, URL and URN
-
Uri is a uniform resource identifier, which is used to uniquely identify a resource. The URL is a uniform resource locator, a uniform resource locator. It is a specific URI, that is, the URL can be used to identify a resource, and also indicates how to locate the resource. URN, uniform resource name, uniform resource name, identifies resources by name, such as mailto:java-net@java.sun.com . In other words, URI is an abstract and high-level concept to define unified resource identification, while URL and URN are specific ways of resource identification. Both URL and URN are URIs.
-
In Java URIs, a URI instance can represent absolute or relative, as long as it conforms to the syntax rules of URIs. The URL class not only conforms to the semantics, but also contains the information to locate the resource, so it cannot be relative.