Tomcat's Web Communication Principle

Two programs on the socket network exchange data through a two-way communication connection, and one end of the connection is called a socket.

At least one pair of port numbers (socket) is required to establish a network communication connection. Socket is essentially a programming interface (API), which encapsulates TCP/IP. TCP/IP also provides an interface that programmers can use for network development. This is the Socket programming interface; HTTP is a car, which provides a way to encapsulate or display data. The specific form; Socket is the engine that provides the ability for network communication.

 

The English meaning of Socket is "hole" or "socket". As the process communication mechanism of BSD UNIX, take the latter meaning. Also commonly referred to as a "socket" to describe an IP address and port, it is a handle to a communication chain that can be used to communicate between different virtual machines or between different computers. A host on the Internet generally runs multiple service software and provides several services at the same time. Each service opens a Socket and binds to a port, and different ports correspond to different services. Socket is like a multi-hole socket, just like its original meaning in English. A host is like a room full of sockets, each socket has a number, some sockets provide 220V AC, some provide 110V AC, and some provide cable TV programs. The client software can get different services by inserting the plugs into sockets with different numbers.

 

 

Socket connection process

Depending on how the connection is initiated and the target to which the local socket is to be connected, the connection process between sockets can be divided into three steps: server monitoring, client request, and connection confirmation.

(1) Server monitoring: The server-side socket does not locate a specific client socket, but is in a state of waiting for a connection to monitor the network status in real time.

(2) Client request: refers to the connection request made by the socket of the client, and the target to be connected is the socket of the server. To do this, the client's socket must first describe the server's socket to which it wants to connect, point out the address and port number of the server-side socket, and then make a connection request to the server-side socket.

(3) Connection confirmation: When the server-side socket monitors or receives the connection request of the client-side socket, it responds to the request of the client-side socket, establishes a new thread, and connects the server-side socket to the connection request. The description of the connection is sent to the client, and once the client confirms the description, the connection is established. The server-side socket continues to be in the listening state and continues to receive connection requests from other client-side sockets.


 Communication principle

 

import java.io.IOException;

import java.io.PrintWriter;

import java.net.ServerSocket;

import java.net.Socket;

 

public class SocketService {

// build server side

public static void main(String[] args) throws IOException {

try {

ServerSocket server = null;

server = new ServerSocket(5209);

// b) Specify the bound port and listen on this port.

System.out.println("Server started successfully");

// Create a ServerSocket listening for client requests on port 5209

Socket socket;

PrintWriter writer ;

while(true){

socket = server.accept();

// 2. Call the accept() method to start listening and wait for the connection from the client

// Use accept() to block and wait for client requests, there are clients

// When the request comes, a Socket object is generated and the execution continues

// Get the input stream from the Socket object and construct the corresponding BufferedReader object

writer = new PrintWriter(socket.getOutputStream());

// if the string is "bye", stop the loop

writer.println(getTable());

// output the string to the client

writer.flush();

// 5. Close the resource

//writer.close(); 

            }

// writer.close(); // close the socket output stream

//socket.close(); // close Socket

//server.close(); // Close ServerSocket

            } catch (Exception e) {// Error, print error message

e.printStackTrace ();

}

}

 

public static String getTable(){

StringBuffer sb = new StringBuffer();

sb.append("<html>");

sb.append("<body>");

sb.append("<hr color='red'>This table has a title and a thick border:</hr>");

sb.append("<table style='border:1px solid #ff0000;width='1600px';border-collapse:collapse;'>");

sb.append("<caption>My title</caption>");

sb.append("<tr>");

sb.append(" <td style='border:1px #0066ff solid;' >100</td>");

sb.append("<td style='border:1px #0066ff solid;' >200</td>");

sb.append("<td style='border:1px #0066ff solid;' >300</td>");

sb.append("</tr>");

sb.append("<tr>");

sb.append("<td style='border:1px #0066ff solid;' >400</td>");

sb.append("<td style='border:1px #0066ff solid;' >500</td>");

sb.append("<td style='border:1px #0066ff solid;' >600</td>");

sb.append("</tr>");

sb.append("</table>");

sb.append("</body>");

sb.append("</html>");

return sb.toString();

}

}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326298001&siteId=291194637