We know that network traffic using Java for the TCP/UDP protocol is typically written using the socket service under the Java NET package, with server servers and client clients, which are used to listen for client connections and receive information from clients. And the client can receive the information sent by the server, forming multiple clients to connect with a server, each client can communicate with each other mechanism.
Here we will write a simple socket service based on the TCP protocol to transmit information to the server, and then to analyze.
Create a new Java project in Eclipse, called "Ioserver", which is the service side:
Then create a cn.com.io.old package under SRC, and create a class named "Old_io_server" below:
Write the service-side code in this class:
Package cn.com.io.old;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket; /*** * Traditional Socket server/public class Old_io_server {public static void main (string[] args) throws IOException {//Chuang
Build socket service, monitor 10010 port serversocket server = new ServerSocket (10010); SYSTEM.OUT.PRINTLN ("Server-side startup.")
");
while (true) {//Get a Socket (block) final socket socket = server.accept (); System.out.println ("A new client appears.)
");
Business processing handler (socket); /** * Read Data * @param socket * @throws Exception */private static void handler (Socket SOC
KET) {try {byte [] bytes = new byte[1024];
InputStream input = Socket.getinputstream ();
int read = 0;
while (read!=-1) {//Read data (blocked) Read = Input.read (bytes);
System.out.println (New String (Bytes,0,read));
} catch (Exception e) {e.printstacktrace ();
}finally{try {System.out.println ("socket close"); SOcket.close ();
catch (IOException e) {e.printstacktrace (); }
}
}
}We hit a breakpoint on the first line of the Mian function and run the program using debug mode:
You can see that when the client is not connected, the server blocks the wait at the location of the server.accept ().
Then use the Windows CMD console telnet for client connections:
You can see the program going down:
When the program goes to read = Input.read (bytes), there is a blocking wait waiting for the client to transmit information:
At this point in the blocking process, means that this thread is always occupied by this socket, the other socket can not come in. (Open a CMD console again using Telnet, you can find that you can't get in)
When we enter the information in cmd (CTRL +] Enter the client, send Send message), you can see the server received information:
If you want the server to process multiple client-side information, you need to assign a thread to each client. Let's revise the service side below:
Package cn.com.io.old;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.net.ServerSocket;
Import Java.net.Socket;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;
/*** * Traditional Socket server/public class Old_io_server {public static void main (string[] args) throws IOException {
Create a cache thread pool Executorservice newcachedthreadpool = Executors.newcachedthreadpool ();
Create the socket service, listening on the 10010 port serversocket server = new ServerSocket (10010); SYSTEM.OUT.PRINTLN ("Server-side startup.")
");
while (true) {//Get a Socket (block) final socket socket = server.accept (); System.out.println ("A new client appears.)
");
The online pool opens a thread newcachedthreadpool.execute (new Runnable () {@Override public void run () {//Business process
Handler (socket);
}
}); }/** * Read data * @param socket * @throws Exception */private static void handler (socket Socket) {try {[] bytes = new byte[1024];
InputStream input = Socket.getinputstream ();
int read = 0;
while (read!=-1) {//Read data (blocked) Read = Input.read (bytes);
System.out.println (New String (Bytes,0,read));
} catch (Exception e) {e.printstacktrace ();
}finally{try {System.out.println ("socket close");
Socket.close ();
catch (IOException e) {e.printstacktrace (); }
}
}
}You can see that we created a cache thread pool that creates a new thread to service the client when the server is connected to a new client.
Now using two clients to connect to port 10010, you can see two clients receiving information:
So we can use this structure to implement a long connection server. Here is still a big drawback, because at present we each client has assigned a thread to run, if 10,000 clients come in, we have to allocate 10,000 threads to the client to use, such resource consumption is very huge. It's like a restaurant where every guest comes in and assigns a waiter to serve him, and the cost of the waiter is huge for the restaurant.
Based on the above, the traditional socket service to achieve long connection service is not appropriate, but can implement a short connection of the server, such as the old version of Tomcat.
Summarize:
Features of traditional IO transmissions:
(1) At least two blocking points (wait for client to connect, wait for client to send information)
(2) Only one client connection can be routed under a single thread
(3) Use the thread pool to connect multiple clients, but it consumes performance.
Using NIO, we can solve the problem of network transmission of traditional IO, which we will introduce in the next chapter.
Reprint please indicate the source: https://blog.csdn.net/acmman/article/details/80039159