Advanced java (11) -- network programming
Portal: ·Zhang Qi MAX·
##Learning objectives
- Be able to distinguish the characteristics of UDP and TCP protocols
- Be able to name two common classes under TCP protocol
- Be able to write string data transmission program under TCP protocol
- Be able to understand the case of file upload under TCP protocol
- Able to understand case 2 under TCP protocol
Chapter 1 Introduction to network programming
1.1 software structure
- C/S structure: the full name is Client/Server structure, which refers to client and server structure. Common programs include QQ, Xunlei and other software.
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-x50Iipr2-1620958714326)(img/1_cs.jpg)]
B/S structure: fully known as Browser/Server structure, it refers to browser and server structure. Common browsers include Google, Firefox, etc.
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG ezznrles-1620958714330) (IMG / 2_bs. JPG)]
The two architectures have their own advantages, but no matter which architecture, it is inseparable from the support of the network. Network programming is a program that realizes the communication between two computers under a certain protocol.
1.2 network communication protocol
-
**Network communication protocol: * * multiple computers can be connected through the computer network. Computers located in the same network need to abide by certain rules when connecting and communicating, just as cars driving on the road must abide by traffic rules. In the computer network, these rules of connection and communication are called network communication protocol. It makes unified provisions on the transmission format, transmission rate and transmission steps of data. Both sides of communication must abide by them at the same time to complete the data exchange.
-
TCP/IP protocol: Transmission Control Protocol / Internet protocol, which is the most basic and extensive protocol on the Internet. It defines standards for how computers connect to the Internet and how data is transmitted between them. It contains a series of protocols for processing data communication, and adopts a 4-layer layered model. Each layer calls the protocols provided by its next layer to complete its own requirements.
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-4MAjDDhz-1620958714331)(img/3_tcp_ip.jpg)]
In the figure above, the four layers of TCP/IP protocol are application layer, transmission layer, network layer and link layer, and each layer is responsible for different communication functions.
Link layer: the link layer is used to define the physical transmission channel. It is usually the driving protocol for some network connection devices, such as the driver provided for optical fiber and network cable.
Network layer: the network layer is the core of the whole TCP/IP protocol. It is mainly used to group the transmitted data and send the packet data to the target computer or network.
Transport layer: it mainly enables network programs to communicate. In network communication, TCP protocol or UDP protocol can be used.
Application layer: mainly responsible for the protocol of the application, such as HTTP protocol, FTP protocol, etc.
1.3 protocol classification
The communication protocol is still relatively complex, Java Net package, which provides low-level communication details. We can directly use these classes and interfaces to focus on network program development without considering the details of communication.
java.net package provides support for two common network protocols:
-
UDP: user datagram protocol. UDP is a connectionless communication protocol, that is, during data transmission, the sender and receiver of data do not establish a logical connection. In short, when a computer sends data to another computer, the sender will send data without confirming whether the receiver exists. Similarly, when the receiver receives the data, it will not feed back whether it has received the data to the sender.
Due to the low resource consumption and high communication efficiency of using UDP protocol, it is usually used for the transmission of audio, video and ordinary data. For example, video conference uses UDP protocol, because even if one or two packets are lost occasionally, it will not have a great impact on the receiving result.
However, when using UDP protocol to transmit data, due to the non connectivity of UDP, the integrity of data cannot be guaranteed. Therefore, it is not recommended to use UDP protocol when transmitting important data. The UDP exchange process is shown in the figure below.
[the external link picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (IMG twlopm0v-1620958714333) (IMG \ UDP communication diagram. bmp)]
Features: the data is limited to 64kb, beyond which it cannot be sent.
Datagram: the basic unit of network transmission
-
TCP: transmission control protocol. TCP protocol is a connection oriented communication protocol, that is, before transmitting data, establish a logical connection between the sending end and the receiving end, and then transmit data. It provides reliable and error free data transmission between two computers.
In TCP connection, the client and server must be specified. The client sends a connection request to the server. Each connection creation needs to go through "three handshakes".
- Three handshakes: in TCP protocol, three interactions between the client and the server in the preparation stage of sending data to ensure the reliability of the connection.
- For the first handshake, the client sends a connection request to the server and waits for the server to confirm.
- In the second handshake, the server sends back a response to the client to notify the client that it has received the connection request.
- For the third handshake, the client sends confirmation information to the server again to confirm the connection. The whole interaction process is shown in the figure below.
- Three handshakes: in TCP protocol, three interactions between the client and the server in the preparation stage of sending data to ensure the reliability of the connection.
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-jvw28MFg-1620958714336)(img/4_tcp.jpg)]
After three handshakes are completed and the connection is established, the client and server can start data transmission. Because of this connection oriented feature, TCP protocol can ensure the security of data transmission, so it is widely used, such as downloading files, browsing web pages and so on.
1.4 three elements of network programming
agreement
- **Protocol: * * the rules that must be observed in computer network communication have been introduced and will not be repeated.
IP address
- IP address: refers to the Internet Protocol Address, commonly known as IP. IP addresses are used to uniquely number computer devices in a network. If we compare "personal computer" to "a telephone", then "IP address" is equivalent to "telephone number".
IP address classification
-
IPv4: it is a 32-bit binary number, which is usually divided into 4 bytes and expressed in the form of a.b.c.d, such as 192.168.65.100. Where a, B, C and D are decimal integers between 0 and 255, then they can represent 4.2 billion at most.
-
IPv6: due to the vigorous development of the Internet, the demand for IP address is increasing, but the limited network address resources make the allocation of IP more and more tense.
In order to expand the address space, it is proposed to redefine the address space through IPv6. It adopts 128 bit address length, a group of 16 bytes, which is divided into 8 groups of hexadecimal numbers, expressed as ABCD:EF01:2345:6789:ABCD:EF01:2345:6789. It is said that it can compile a network address for every grain of sand in the world, which solves the problem of insufficient network address resources.
Common commands
- To view the local IP address, enter:
ipconfig
- Check whether the network is connected. Enter:
ping Space IP address ping 220.181.57.216
Special IP address
- Local IP address: 127.0.0.1, localhost.
Port number
Network communication is essentially the communication between two processes (Applications). Each computer has many processes, so how to distinguish these processes in network communication?
If the IP address can uniquely identify the device in the network, the port number can uniquely identify the process (application) in the device.
- **Port number: an integer represented by two bytes. Its value range is 065535 * *. Among them, the port number between 01023 is used for some well-known network services and applications, and ordinary applications need to use a port number of more than 1024. If the port number is occupied by another service or application, the current program will fail to start.
Using the ternary combination of protocol + IP address + port number, the processes in the network can be identified, and the communication between processes can use this identification to interact with other processes.
Chapter II TCP communication program
2.1 general
TCP communication can realize the data interaction between two computers. The two ends of communication should be strictly divided into Client and Server.
Steps for communication between two ends:
- The server program needs to be started in advance and wait for the connection of the client.
- The client actively connects to the server. Only after the connection is successful can communication be realized. The server cannot actively connect to the client.
In Java, two classes are provided to implement TCP communication programs:
- Client: Java net. The Socket class represents. Create a Socket object, send a connection request to the server, and the server responds to the request. The two establish a connection and start communication.
- Server: Java net. The ServerSocket class represents. Creating a ServerSocket object is equivalent to starting a service and waiting for the client to connect.
2.2 Socket class
Socket class: this class implements client socket. Socket refers to the endpoint of communication between two devices.
Construction method
-
public Socket(String host, int port): create a socket object and connect it to the specified port number on the specified host. If the specified host is null, it is equivalent to that the specified address is the loopback address.
Tip: the Loopback Address (127.x.x.x) is the Loopback Address of the local machine. It is mainly used for network software testing and local machine interprocess communication. No matter what program uses the Loopback Address to send data, it will return immediately without any network transmission.
Construction example, code is as follows:
Socket client = new Socket("127.0.0.1", 6666);
Member method
-
public InputStream getInputStream(): returns the input stream of this socket.
- If this socket has an associated channel, all operations of the generated InputStream are also associated with that channel.
- Closing the generated InputStream will also close the relevant Socket.
-
public OutputStream getOutputStream(): returns the output stream of this socket.
- If this socket has an associated channel, all operations of the generated OutputStream are also associated with the channel.
- Closing the generated OutputStream will also close the relevant Socket.
-
public void close(): close this socket.
- Once a socket is closed, it can no longer be used.
- Closing this socket will also close the related InputStream and OutputStream.
-
public void shutdownOutput(): disables the output stream of this socket.
- Any previously written data will be sent, and then the output stream will be terminated.
2.3 ServerSocket class
ServerSocket class: this class implements the server socket, which waits for requests through the network.
Construction method
- public ServerSocket(int port): when using this construction method to create a ServerSocket object, you can bind it to a specified port number. The parameter port is the port number.
Construction example, code is as follows:
ServerSocket server = new ServerSocket(6666);
Member method
- public Socket accept(): listen and accept the connection and return a new Socket object for communication with the client. This method blocks until a connection is established.
2.4 simple TCP network program
TCP communication analysis diagram
- [server] start, create ServerSocket object and wait for connection.
- [Client] start, create Socket object and request connection.
- [server] receives the connection, calls the accept method, and returns a Socket object.
- [Client] Socket object, obtain OutputStream and write data to the server.
- [server] socket object, get InputStream and read the data sent by the client.
At this point, the client sends data to the server successfully.
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-Ca9O21J8-1620958714338)(img/5_ simple communication. jpg)]
Since then, the server writes back data to the client.
- [server] Socket object, get OutputStream and write back data to the client.
- [Client] socket object, get InputStream, parse and write back data.
- [Client] release resources and disconnect.
The client sends data to the server
Server implementation:
public class ServerTCP { public static void main(String[] args) throws IOException { System.out.println("Server start , Waiting for connection .... "); // 1. Create the ServerSocket object, bind the port, and start waiting for the connection ServerSocket ss = new ServerSocket(6666); // 2. Receive the connection accept method and return the socket object Socket server = ss.accept(); // 3. Get the input stream through socket InputStream is = server.getInputStream(); // 4. Read data at one time // 4.1 create byte array byte[] b = new byte[1024]; // 4.2 read data into byte array int len = is.read(b); // 4.3 parse array and print string information String msg = new String(b, 0, len); System.out.println(msg); //5. Close resources is.close(); server.close(); } }
Client implementation:
public class ClientTCP { public static void main(String[] args) throws Exception { System.out.println("Client sends data"); // 1. Create a socket (IP, port) to determine where to connect Socket client = new Socket("localhost", 6666); // 2. Get the stream object Output stream OutputStream os = client.getOutputStream(); // 3. Write the data os.write("How are you?? tcp ,I'm coming.".getBytes()); // 4. Close resources os.close(); client.close(); } }
The server writes back data to the client
Server implementation:
public class ServerTCP { public static void main(String[] args) throws IOException { System.out.println("Server start , Waiting for connection .... "); // 1. Create the ServerSocket object, bind the port, and start waiting for the connection ServerSocket ss = new ServerSocket(6666); // 2. Receive the connection accept method and return the socket object Socket server = ss.accept(); // 3. Get the input stream through socket InputStream is = server.getInputStream(); // 4. Read data at one time // 4.1 create byte array byte[] b = new byte[1024]; // 4.2 read data into byte array int len = is.read(b); // 4.3 parse array and print string information String msg = new String(b, 0, len); System.out.println(msg); // =================Write back data======================= // 5. Obtain the output stream through socket OutputStream out = server.getOutputStream(); // 6. Write back data out.write("I'm fine,thank you".getBytes()); // 7. Close resources out.close(); is.close(); server.close(); } }
Client implementation:
public class ClientTCP { public static void main(String[] args) throws Exception { System.out.println("Client sends data"); // 1. Create a socket (IP, port) to determine where to connect Socket client = new Socket("localhost", 6666); // 2. Get the output stream object through socket OutputStream os = client.getOutputStream(); // 3. Write the data os.write("How are you?? tcp ,I'm coming.".getBytes()); // ==============Parse writeback========================= // 4. Get the input stream object through socket InputStream in = client.getInputStream(); // 5. Read data byte[] b = new byte[100]; int len = in.read(b); System.out.println(new String(b, 0, len)); // 6. Close resources in.close(); os.close(); client.close(); } }
Chapter III comprehensive cases
3.1 file upload cases
File upload analysis diagram
- [Client] input stream to read the file data from the hard disk into the program.
- [Client] output the stream and write out the file data to the server.
- [server] input the stream and read the file data to the server program.
- [server] output the stream and write out the file data to the server hard disk.
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-NfvScyoU-1620958714339)(img/6_upload.jpg)]
Basic implementation
Server implementation:
public class FileUpload_Server { public static void main(String[] args) throws IOException { System.out.println("Server startup..... "); // 1. Create server socket ServerSocket serverSocket = new ServerSocket(6666); // 2. Establish connection Socket accept = serverSocket.accept(); // 3. Create flow object // 3.1 get the input stream and read the file data BufferedInputStream bis = new BufferedInputStream(accept.getInputStream()); // 3.2 create an output stream and save it locally BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.jpg")); // 4. Read and write data byte[] b = new byte[1024 * 8]; int len; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } //5. Close resources bos.close(); bis.close(); accept.close(); System.out.println("File upload saved"); } }
Client implementation:
public class FileUPload_Client { public static void main(String[] args) throws IOException { // 1. Create flow object // 1.1 create input stream and read local files BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.jpg")); // 1.2 create an output stream and write it to the server Socket socket = new Socket("localhost", 6666); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); //2. Write the data byte[] b = new byte[1024 * 8 ]; int len ; while (( len = bis.read(b))!=-1) { bos.write(b, 0, len); bos.flush(); } System.out.println("File sending completed"); // 3. Release resources bos.close(); socket.close(); bis.close(); System.out.println("File upload completed "); } }
File upload optimization analysis
-
The file name is dead
On the server side, if the name of the saved file is written dead, only one file will be retained on the server hard disk. It is recommended to use system time optimization to ensure that the file name is unique. The code is as follows:
FileOutputStream fis = new FileOutputStream(System.currentTimeMillis()+".jpg") // File name BufferedOutputStream bos = new BufferedOutputStream(fis);
-
Circular receiving problem
The server means that a file is saved and then closed, and subsequent users cannot upload it again, which is not practical. The use of circular improvement can continuously receive files from different users. The code is as follows:
// Each time a new connection is received, a Socket is created while(true){ Socket accept = serverSocket.accept(); ...... }
-
Efficiency issues
The server may take a few seconds to receive large files. At this time, it cannot receive uploads from other users. Therefore, it is optimized using multithreading technology. The code is as follows:
while(true){ Socket accept = serverSocket.accept(); // accept to the child thread new Thread(() -> { ...... InputStream bis = accept.getInputStream(); ...... }).start(); }
Optimized implementation
public class FileUpload_Server { public static void main(String[] args) throws IOException { System.out.println("Server startup..... "); // 1. Create server socket ServerSocket serverSocket = new ServerSocket(6666); // 2. Receive circularly and establish connection while (true) { Socket accept = serverSocket.accept(); /* 3. socket The object is handed over to the sub thread for reading and writing Runnable In the interface, there is only one run method, which uses lambda expressions to simplify the format */ new Thread(() -> { try ( //3.1 get input stream object BufferedInputStream bis = new BufferedInputStream(accept.getInputStream()); //3.2 create an output stream object and save it locally FileOutputStream fis = new FileOutputStream(System.currentTimeMillis() + ".jpg"); BufferedOutputStream bos = new BufferedOutputStream(fis);) { // 3.3 reading and writing data byte[] b = new byte[1024 * 8]; int len; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } //4. Close resources bos.close(); bis.close(); accept.close(); System.out.println("File upload saved"); } catch (IOException e) { e.printStackTrace(); } }).start(); } } }
Information write back analysis diagram
The first four steps are consistent with the basic file upload
- [server] obtain the output stream and write back the data.
- [Client] obtain the input stream and parse the write back data.
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-bFt0Vjpj-1620958714341)(img/6_upload2.jpg)]
Writeback implementation
public class FileUpload_Server { public static void main(String[] args) throws IOException { System.out.println("Server startup..... "); // 1. Create server socket ServerSocket serverSocket = new ServerSocket(6666); // 2. Receive circularly and establish connection while (true) { Socket accept = serverSocket.accept(); /* 3. socket The object is handed over to the sub thread for reading and writing Runnable In the interface, there is only one run method, which uses lambda expressions to simplify the format */ new Thread(() -> { try ( //3.1 get input stream object BufferedInputStream bis = new BufferedInputStream(accept.getInputStream()); //Create stream, save to local. 3 FileOutputStream fis = new FileOutputStream(System.currentTimeMillis() + ".jpg"); BufferedOutputStream bos = new BufferedOutputStream(fis); ) { // 3.3 reading and writing data byte[] b = new byte[1024 * 8]; int len; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } // 4. ========= information updating=========================== System.out.println("back ........"); OutputStream out = accept.getOutputStream(); out.write("Upload successful".getBytes()); out.close(); //================================ //5. Close resources bos.close(); bis.close(); accept.close(); System.out.println("File upload saved"); } catch (IOException e) { e.printStackTrace(); } }).start(); } } }
Client implementation:
public class FileUpload_Client { public static void main(String[] args) throws IOException { // 1. Create flow object // 1.1 create input stream and read local files BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.jpg")); // 1.2 create an output stream and write it to the server Socket socket = new Socket("localhost", 6666); BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); //2. Write the data byte[] b = new byte[1024 * 8 ]; int len ; while (( len = bis.read(b))!=-1) { bos.write(b, 0, len); } // Close the output stream and notify the server that the data is written out socket.shutdownOutput(); System.out.println("File sending completed"); // 3. = = = = parse writeback============ InputStream in = socket.getInputStream(); byte[] back = new byte[20]; in.read(back); System.out.println(new String(back)); in.close(); // ============================ // 4. Release resources socket.close(); bis.close(); } }
3.2 simulate B\S server (expand knowledge points)
Simulate the website server, use the browser to access the server-side program written by yourself, and view the web page effect.
case analysis
-
Prepare page data, web folders.
Copy to our Module, such as day08
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (IMG rakrqrv0-1620958714342) (IMG \ copy. png)]
-
We simulate the server side. The ServerSocket class listens to the port and uses the browser to access it
public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8000); Socket socket = server.accept(); InputStream in = socket.getInputStream(); byte[] bytes = new byte[1024]; int len = in.read(bytes); System.out.println(new String(bytes,0,len)); socket.close(); server.close(); }
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-Pf6DauUR-1620958714344)(img \ unable to access. jpg)]
-
The byte input stream in the server program can read the request information sent by the browser
[the external link image transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-Kk9p2lnn-1620958714346)(img \ read access information. jpg)]
GET/web/index.html HTTP/1.1 is the request message of the browser/ web/index.html is the server-side resource that the browser wants to request, and the requested resource is obtained by string cutting.
//Convert the stream and read the first line of the browser request BufferedReader readWb = new BufferedReader(new InputStreamReader(socket.getInputStream())); String requst = readWb.readLine(); //Fetch the path of the requested resource String[] strArr = requst.split(" "); //Remove the in front of the web/ String path = strArr[1].substring(1); System.out.println(path);
Case realization
Server implementation:
public class SerDemo { public static void main(String[] args) throws IOException { System.out.println("Server start , Waiting for connection .... "); // Create ServerSocket object ServerSocket server = new ServerSocket(8888); Socket socket = server.accept(); // The transformation stream reads the browser's request message BufferedReader readWb = new BufferedReader(new InputStreamReader(socket.getInputStream())); String requst = readWb.readLine(); // Fetch the path of the requested resource String[] strArr = requst.split(" "); // Remove the in front of the web/ String path = strArr[1].substring(1); // Read the resource file requested by the client FileInputStream fis = new FileInputStream(path); byte[] bytes= new byte[1024]; int len = 0 ; // Byte output stream to write the file to the client OutputStream out = socket.getOutputStream(); // Write HTTP protocol response header, fixed writing method out.write("HTTP/1.1 200 OK\r\n".getBytes()); out.write("Content-Type:text/html\r\n".getBytes()); // You must write a blank line, or the browser will not parse it out.write("\r\n".getBytes()); while((len = fis.read(bytes))!=-1){ out.write(bytes,0,len); } fis.close(); out.close(); readWb.close(); socket.close(); server.close(); } }
Access effect
- Firefox
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-R57hmSrU-1620958714347)(img / effect picture 1.png)]
Tip: different browsers have different kernels, and the parsing effect may be different.
It is found that there are many forks in the browser, which indicates that the browser does not read the picture information.
The working principle of the browser is that a thread will be opened for separate access when encountering pictures, so thread technology is added to the server.
public class ServerDemo { public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(8888); while(true){ Socket socket = server.accept(); new Thread(new Web(socket)).start(); } } static class Web implements Runnable{ private Socket socket; public Web(Socket socket){ this.socket=socket; } public void run() { try{ //Convert the stream and read the first line of the browser request BufferedReader readWb = new BufferedReader(new InputStreamReader(socket.getInputStream())); String requst = readWb.readLine(); //Fetch the path of the requested resource String[] strArr = requst.split(" "); System.out.println(Arrays.toString(strArr)); String path = strArr[1].substring(1); System.out.println(path); FileInputStream fis = new FileInputStream(path); System.out.println(fis); byte[] bytes= new byte[1024]; int len = 0 ; //Write back data to browser OutputStream out = socket.getOutputStream(); out.write("HTTP/1.1 200 OK\r\n".getBytes()); out.write("Content-Type:text/html\r\n".getBytes()); out.write("\r\n".getBytes()); while((len = fis.read(bytes))!=-1){ out.write(bytes,0,len); } fis.close(); out.close(); readWb.close(); socket.close(); }catch(Exception ex){ } } } }
Access effect:
[the external chain picture transfer fails, and the source station may have anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-6ib6sEvA-1620958714349)(img / effect picture 2.png)] diagram:
[the transfer of external chain pictures fails. The source station may have anti-theft chain mechanism. It is recommended to save the pictures and upload them directly (img-M4wgl76d-1620958714350)(img\BS communication. bmp)]