day11 [Network Programming](* Software Architecture CS/BS * Three Elements of Network Communication * TCP Communication * Socket Socket * ServerSocket)

Posted by Deadmeat on Sat, 11 May 2019 14:23:04 +0200

day11 [Network Programming]

primary coverage

  • Software Architecture CS/BS
  • Three Elements of Network Communication
  • TCP communication
  • Socket socket
  • ServerSocket

Teaching objectives

  • Ability to distinguish UDP and TCP protocol features
  • Can name two common class names under TCP protocol
  • Can write string data transmission program under TCP protocol
  • Understanding the case of file upload under TCP protocol
  • Understanding Case 2 of TCP Protocol

Chapter 1 Introduction to Network Programming

1.1 Software Architecture

  • C/S structure: All called Client/Server structure, refers to the client and server structure. Common programs are QQ, Xunlei and other software.

B/S structure: All called Browser/Server structure, refers to the browser and server structure. Common browsers include Google, Firefox, etc.

The two architectures have their own advantages, but no matter which one, they can not be separated 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: ** Through computer network, multiple computers can be connected, and computers in the same network need to obey certain rules when connecting and communicating, just like cars on the road must obey traffic rules. In computer networks, these rules of connection and communication are called network communication protocols. They uniformly stipulate the data transmission format, transmission rate, transmission steps and so on. Both sides of communication must abide by these rules to complete data exchange at the same time.

  • TCP/IP protocol: Transmission Control Protocol/Internet Protocol is the most basic and extensive protocol in 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 data communication, and adopts a four-tier hierarchical model. Each layer calls the protocol provided by its next layer to fulfill its own requirements.

In the figure above, the four layers of TCP/IP protocol are application layer, transport layer, network layer and link layer, each layer is responsible for different communication functions.
Link Layer: Link Layer is used to define physical transmission channels, usually the driver protocol for some network connection devices, such as for optical fibers and wires.
Network Layer: Network Layer is the core of 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: Mainly make the network program communicate. In network communication, TCP protocol or UDP protocol can be used.
Application Layer: Mainly responsible for the application protocol, such as HTTP protocol, FTP protocol, etc.

1.3 Protocol Classification

Communication protocols are still more complex. The classes and interfaces contained in the java.net package provide low-level communication details. We can use these classes and interfaces directly to concentrate on the development of network programs without considering the details of communication.

Two common network protocols are supported in the java.net package:

  • UDP: User Datagram Protocol. UDP is a connectionless communication protocol, that is, when data is transmitted, there is no logical connection between the sender and the receiver. Simply put, when a computer sends data to another computer, the sender will not confirm the existence of the receiver, it will send data, and when the receiver receives data, it will not give feedback to the sender whether it receives data.

    Because the use of UDP protocol consumes less resources and has high communication efficiency, it is usually used for the transmission of audio, video and ordinary data, such as video conferencing, because the occasional loss of one or two data packets will not have a great impact on the reception results.

    However, when using UDP protocol to transmit data, UDP protocol is not recommended to transmit important data because UDP is connectionless and can not guarantee the integrity of data. The exchange process of UDP is shown in the following figure.

Features: Data is limited to 64 kb, 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, which establishes a logical connection between the sender and the receiver before transferring data, and then transfers data. It provides reliable and error-free data transmission between two computers.

    In TCP connection, it must be clear that the client and the server side, from the client to the server side to send a connection request, each connection creation needs to go through "three shakes of hands".

    • Three handshakes: TCP protocol, in the preparation stage of sending data, three interactions between client and server to ensure reliable connection.
      • For 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 back a response to the client, informing the client that the connection request has been received.
      • The third handshake, the client sends confirmation information to the server again to confirm the connection. The whole interaction process is shown in the following figure.

After three handshakes, 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

  • ** Agreement: ** The rules that computer network communication must abide by have been introduced and will not be repeated.

IP address

  • IP address: refers to 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: A 32-bit binary number, usually divided into four bytes, expressed in the form of a.b.c.d, such as 192.168.65.100. Among them, a, b, C and D are decimal integers between 0 and 255, which can represent up to 4.2 billion.

  • IPv6: Due to the vigorous development of the Internet, the demand for IP addresses is growing, but the limited network address resources make the distribution of IP more and more tense.

    In order to expand the address space, IPv6 is proposed to redefine the address space, using 128-bit address length, each 16 bytes group, divided into eight groups of hexadecimal numbers, expressed as ABCD:EF01:2345:6789:ABCD:EF01:2345:6789, the title can be used for each grain of sand in the world, thus solving the problem of insufficient network address resources.

Frequently used commands

  • View the local IP address and enter in the console:
ipconfig
  • Check whether the network is connected, and enter in the console:
ping Blank 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 when communicating in the network?

If the IP address can uniquely identify the device in the network, then the port number can uniquely identify the process (application) in the device.

  • ** Port Number: An integer expressed in two bytes with a range of values of 065535**. Among them, the port numbers between 01023 are used for some well-known network services and applications, and ordinary applications need to use more than 1024 port numbers. If the port number is occupied by another service or application, the current program will fail to start.

By using the triple combination of protocol, IP address and port number, the process in the network can be identified, and then the communication between processes can interact with other processes.

Chapter 2 TCP Communication Program

2.1 overview

TCP communication can realize data interaction between two computers. The two ends of communication should be strictly distinguished between Client and Server.

The steps of communication between two terminals are as follows:

  1. Server-side programs need to be started beforehand, waiting for the connection of the client.
  2. The client actively connects to the server, and only when the connection is successful can it communicate. The server can not actively connect to the client.

In Java, two classes are provided to implement TCP communication programs:

  1. Client: java.net.Socket class representation. 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.
  2. Server side: java.net.ServerSocket class representation. Creating a ServerSocket object is equivalent to opening a service and waiting for a client to connect.

2.2 Socket class

Socket class: This class implements client sockets, which refer to the endpoints of communication between two devices.

Construction method

  • public Socket(String host, int port): Creates a socket object and connects it to the specified port number on the specified host. If the specified host is null, then the specified address is the return address.

    Tip: The return address (127.x.x.x) is the local return address (Loopback Address), which is mainly used for network software testing and local inter-process communication. No matter what program, once the return address is used to send data, it will return immediately without any network transmission.

Construct an example with the following code:

Socket client = new Socket("127.0.0.1", 6666);

Member method

  • public InputStream getInputStream(): Returns the input stream for this socket.

    • If this Scoket has an associated channel, all operations of the generated InputStream are associated with that channel.
    • Closing the generated InputStream will also close the associated Socket.
  • public OutputStream getOutputStream(): Returns the output stream of this socket.

    • If the Scoket has an associated channel, all operations of the generated OutputStream are associated with the channel.
    • Closing the generated OutputStream will also close the associated Socket.
  • public void close(): Close this socket.

    • Once a socket is turned off, 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 the output stream 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 you create a ServerSocket object with this constructor, you can bind it to a specified port number, the parameter port is the port number.

Construct an example with the following code:

ServerSocket server = new ServerSocket(6666);

Member method

  • public Socket accept(): Listens for and accepts connections, returns a new Socket object, which is used to communicate with the client. This method will block until the connection is established.

2.4 Simple TCP Network Program

TCP Communication Analysis Diagram

  1. [Server] Start, create the ServerSocket object, and wait for the connection.
  2. [Client] Start, create Socket object, request connection.
  3. The server receives the connection, calls the accept method, and returns a Socket object.
  4. The Socket object obtains OutputStream and writes data to the server.
  5. Scoket object, get InputStream, read the data sent by the client.

At this point, the client successfully sends data to the server.

Since then, the server writes back data to the client.

  1. The Socket object obtains OutputStream and writes back data to the client.
  2. [Client] Scoket object, get InputStream, parse write-back data.
  3. [Client] Release resources and disconnect.

Client sends data to server

Server-side implementation:

public class ServerTCP {
    public static void main(String[] args) throws IOException {
        System.out.println("Server Start , Waiting for connection .... ");
        // 1. Create ServerSocket objects, bind ports, and start waiting for connections
        ServerSocket ss = new ServerSocket(6666);
        // 2. Receive the connection accept method and return the socket object.
        Socket server = ss.accept();
        // 3. Getting input stream through socket
        InputStream is = server.getInputStream();
        // 4. Read data at one time
      	// 4.1 Create byte arrays
        byte[] b = new byte[1024];
      	// 4.2 Data is read into the byte array.
        int len = is.read(b)// 4.3 Parse arrays, 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 Socket (ip, port) and determine where to connect.
		Socket client = new Socket("localhost", 6666);
		// 2. Getting stream objects. Output stream
		OutputStream os = client.getOutputStream();
		// 3. Write out 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-side implementation:

public class ServerTCP {
    public static void main(String[] args) throws IOException {
        System.out.println("Server Start , Waiting for connection .... ");
        // 1. Create ServerSocket objects, bind ports, and start waiting for connections
        ServerSocket ss = new ServerSocket(6666);
        // 2. Receive the connection accept method and return the socket object.
        Socket server = ss.accept();
        // 3. Getting input stream through socket
        InputStream is = server.getInputStream();
        // 4. Read data at one time
      	// 4.1 Create byte arrays
        byte[] b = new byte[1024];
      	// 4.2 Data is read into the byte array.
        int len = is.read(b)// 4.3 Parse arrays, 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 Socket (ip, port) and determine where to connect.
		Socket client = new Socket("localhost", 6666);
		// 2. Obtain the output stream object through Scoket 
		OutputStream os = client.getOutputStream();
		// 3. Write out the data.
		os.write("How are you?? tcp ,I'm coming.".getBytes());
      	// ============== parsing write-back=========================
      	// 4. Obtain input stream objects through Scoket
      	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 Graphics

  1. [Client] Input stream, which reads file data from hard disk to program.
  2. [Client] Output stream, write file data to server.
  3. [Server] Input stream, read file data to server program.
  4. Output stream, write file data to server hard disk.

Basic realization

Server-side implementation:

public class FileUpload_Server {
    public static void main(String[] args) throws IOException {
        System.out.println("Server startup.....  ");
        // 1. Create Server Socket on the Service Side
      	ServerSocket serverSocket = new ServerSocket(6666);
  		// 2. Connecting 
        Socket accept = serverSocket.accept();
      	// 3. Creating stream objects
      	// 3.1 Get input stream and read file data
        BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
        // 3.2 Create the output stream and save it locally.
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.jpg"));
		// 4. Reading and writing data
        byte[] b = new byte[1024 * 8];
        int len;
        while ((len = bis.read(b)) != -1) {
            bos.write(b, 0, len);
        }
        //5. Closing 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 stream objects
        // 1.1 Create input stream and read local files  
        BufferedInputStream bis  = new BufferedInputStream(new FileInputStream("test.jpg"));
        // 1.2 Create the output stream and write it to the server 
        Socket socket = new Socket("localhost", 6666);
        BufferedOutputStream   bos   = new BufferedOutputStream(socket.getOutputStream());

        //2. Write out 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("The file has been sent.");
        // 3. Releasing resources

        bos.close(); 
        socket.close();
        bis.close(); 
        System.out.println("File upload completed ");
	}
}

Optimal Analysis of File Upload

  1. Dead File Name Writing

    On the server side, if the name of the saved file is written to death, it will eventually lead to the server hard disk, which will only retain one file. 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);
  1. Cyclic Receiving Problems

    Server, refers to the storage of a file closed, after the user can no longer upload, this is not realistic, using circular improvement, can continuously receive different user files, the code is as follows:

// Create a Socket each time you receive a new connection
while(true){
    Socket accept = serverSocket.accept();
    ......
}
  1. Efficiency problem

    The server may take several seconds to receive large files, and can not receive uploads from other users at this time. Therefore, multi-threading technology is used to optimize the system. The code is as follows:

while(true){
    Socket accept = serverSocket.accept();
    // Acept is handed over to the sub-thread.
    new Thread(() -> {
      	......
        InputStream bis = accept.getInputStream();
      	......
    }).start();
}

Optimization and Implementation

public class FileUpload_Server {
    public static void main(String[] args) throws IOException {
        System.out.println("Server startup.....  ");
        // 1. Create Server Socket on the Service Side
        ServerSocket serverSocket = new ServerSocket(6666);
      	// 2. Receiving circularly and establishing connections
        while (true) {
            Socket accept = serverSocket.accept();
          	/* 
          	3. socket Objects are handed over to sub-threads for read and write operations
               Runnable In the interface, there is only one run method, using lambda expression to simplify the format
            */
            new Thread(() -> {
                try (
                    //3.1 Getting Input Stream Objects
                    BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
                    //3.2 Create output stream objects and save them 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. Closing resources
                    bos.close();
                    bis.close();
                    accept.close();
                    System.out.println("File upload saved");
                } catch (IOException e) {
                  	e.printStackTrace();
                }
            }).start();
        }
    }
}

Information Writback Analysis Graphics

The first four steps are consistent with the upload of the basic document.

  1. Get the output stream and write back the data.
  2. The client obtains the input stream and parses the write-back data.

Write back implementation

public class FileUpload_Server {
    public static void main(String[] args) throws IOException {
        System.out.println("Server startup.....  ");
        // 1. Create Server Socket on the Service Side
        ServerSocket serverSocket = new ServerSocket(6666);
        // 2. Receiving circularly and establishing connections
        while (true) {
            Socket accept = serverSocket.accept();
          	/*
          	3. socket Objects are handed over to sub-threads for read and write operations
               Runnable In the interface, there is only one run method, using lambda expression to simplify the format
            */
            new Thread(() -> {
                try (
                    //3.1 Getting Input Stream Objects
                    BufferedInputStream bis = new BufferedInputStream(accept.getInputStream());
                    //3.2 Create output stream objects and save them 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. ============== Information Write-back===========================
                    System.out.println("back ........");
                    OutputStream out = accept.getOutputStream();
                    out.write("Upload success".getBytes());
                    out.close();
                    //================================

                    //5. Closing 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. Creating stream objects
        // 1.1 Create input stream and read local files
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.jpg"));
        // 1.2 Create the output stream and write it to the server
        Socket socket = new Socket("localhost", 6666);
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());

        //2. Write out 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, notify the server and write out the data.
        socket.shutdownOutput();
        System.out.println("The file has been sent.");
        // 3. ========= parsing write-back============
        InputStream in = socket.getInputStream();
        byte[] back = new byte[20];
        in.read(back);
        System.out.println(new String(back));
        in.close();
        // ============================

        // 4. Releasing resources
        socket.close();
        bis.close();
    }
}

3.2 Simulate B S Server (Extended Knowledge Points)

Simulate the website server, use the browser to access the server program written by oneself, and view the effect of the web page.

case analysis

  1. Prepare page data, web folder.

    Copy to our Module, for example, to day08

  1. We simulate server side, ServerSocket class listening port, using browser access

    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();
    }
    

  1. The byte input stream in the server program can read the request information from the browser.

GET/web/index.html HTTP/1.1 is the request message of the browser. / Web/index.html is a server-side resource that browsers want to request. It uses string cutting to get the requested resource.

//Conversion stream, read the first line of browser request
BufferedReader readWb = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String requst = readWb.readLine();
//The path to fetch the requested resource
String[] strArr = requst.split(" ");
//Remove the front of the web/
String path = strArr[1].substring(1);
System.out.println(path);

Case realization

Server-side implementation:

public class SerDemo {
    public static void main(String[] args) throws IOException {
        System.out.println("Server Start , Waiting for connection .... ");
        // Create ServerSocket Objects
        ServerSocket server = new ServerSocket(8888);
        Socket socket = server.accept();
        // Transform stream reads browser request message
        BufferedReader readWb = new
        BufferedReader(new InputStreamReader(socket.getInputStream()));
        String requst = readWb.readLine();
        // The path to fetch the requested resource
        String[] strArr = requst.split(" ");
        // Remove the 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, Writing Files to Clients
        OutputStream out = socket.getOutputStream();
        // Write HTTP Protocol Response Header, Fixed Writing
        out.write("HTTP/1.1 200 OK\r\n".getBytes());
        out.write("Content-Type:text/html\r\n".getBytes());
        // You must write blank lines, otherwise the browser will not parse
        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

Tip: Different browsers have different kernels and different parsing effects.

Many forks were found in the browser, indicating that the browser did not read the picture information.

The browser works by opening a thread for separate access when it encounters a picture, so thread technology is added to the server side.

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{
                //Conversion stream, read the first line of browser request
                BufferedReader readWb = new
                        BufferedReader(new InputStreamReader(socket.getInputStream()));
                String requst = readWb.readLine();
                //The path to fetch 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 the 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){

            }
        }
    }

}

Visit effects:

Illustration:

Topics: socket network Java Programming