Java implements multi-threaded socket communication technology, and ocket Communication Technology
In the previous example, we use the following code:
Package com.test.net; import java. io. dataInputStream; import java. io. inputStream; import java.net. serverSocket; import java.net. socket; public class EchoServer {/*** print all client output */@ SuppressWarnings ("deprecation") public static void main (String [] args) throws Exception {ServerSocket serverSocket = new ServerSocket (9999);/* The accept method is blocked */Socket server = serverSocket. accept (); while (true) {/* The getInputStream method is blocked */ InputStream in = server. getInputStream (); DataInputStream dataIn = new DataInputStream (in); String str = dataIn. readLine (); if (str! = Null) {System. out. println (str );}}}}
A client connection request can be processed continuously. But what if there are two or more connections? As we can see, although there are multiple, the program has been running in the while loop. The accept method has been executed and cannot respond to other connection requests.
What if I put accept in it? It is true that multiple requests can be received. However, the condition is that the second accept enters only after the last one ends, which is equivalent to a serial event, how can we process multiple requests at the same time? We can use multiple threads.
That is to say, each time a socket request is obtained, a thread is opened for processing, and the main thread continues to wait for the next request. The Code is as follows:
package com.test.net;import java.io.DataInputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;public class MuliEchoServer {/** * @param args * @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stubServerSocket serverSocket=new ServerSocket(8888);while(true){Socket s=serverSocket.accept();task mytask=new task(s);mytask.start();}}}class task extends Thread{private Socket s=null;public task(Socket s){this.s=s;}public void run(){InputStream inStream = null;try {inStream = s.getInputStream();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}DataInputStream dStream=new DataInputStream(inStream);String str = null;try {while(true){str = dStream.readLine();if(str!=null){System.out.println(str);}}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
The output result shows
The content is from the and B windows of telnet, and the output content can be exchanged.
What is the communication principle of java multi-thread socket?
First, the program provides services through time-sharing. That is to say, a program listens to a port. In the first second, a packet can be sent from another place, and in the second, a packet can be sent from another place.
Second, each socket has an ip address and port number sent by the package. The server sends data to the port of the machine corresponding to the ip address to respond to the other party.
Java multi-thread socket communication
ServerSocket server = new ServerSocket (port );
/*. The following is the listener .*/
Try {
While (true ){
Socket socket = server. accept (0;
Thread handleThread = new Thread (new HandleRun (socket). start ();
// Directly put the socket connected to the client into a thread for processing.
// Then, define a HandleRun class that implements Runnable (used to process communication between c-s)
}
} Catch (Exception ex ){}
If you don't understand it, ask. No, you can take some time to help you write a small Demo.