Build a network server in Java language to realize communication between the client and the server, and realize that the client has independent threads and does not interfere with each other.
Basic steps for applying multi-threading to realize communication between server and multi-threading
Server-side Server.java
package test.concurrent.socket; import java.io.*; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Created by dong on 15-6-22. * Socket communication based on TCP protocol, implement user login* Server side*/ public class Server { public static void main(String[] args) { try { //1. Create a server-side Socket, that is, ServerSocket, specify the bound port, and listen to this port ServerSocket serverSocket = new ServerSocket(8888); Socket socket = null; //Record the number of clients int count = 0; System.out.println("***The server is about to start, waiting for the client's link***"); //Loop listen to wait for the client's link while (true){ //Calling the accept() method to start listening, waiting for the client's link socket = serverSocket.accept(); //Creating a new thread ServerThread serverThread = new ServerThread(socket); //Starting thread serverThread.start(); count++; //Search the number of clients System.out.println("Number of clients: " + count); InetAddress address = socket.getInetAddress(); System.out.println("The IP of the current client: " + address.getHostAddress()); } } catch (IOException e) { e.printStackTrace(); } } } Server-side thread processing class ServerThread.java
package test.concurrent.socket; import java.io.*; import java.net.Socket; /** * Created by dong on 15-6-22. * Server-side thread processing class*/ public class ServerThread extends Thread { //Socket Socket socket = null; public ServerThread(Socket socket){ this.socket = socket; } //The operation executed by the thread responds to the client's request public void run(){ InputStream is = null; InputStreamReader isr = null; BufferedReader br = null; OutputStream os = null; PrintWriter pw = null; try { //Get an input stream and read the client's information is = socket.getInputStream(); isr = new InputStreamReader(is); //Convert byte streams to character streams br = new BufferedReader(isr); //Add buffered String info = null; //Loop reading data while ((info = br.readLine()) != null){ System.out.println("I am the server, and the client said: " +info); } socket.shutdownInput(); //Close the input stream//Get the output stream in response to the client's request os = socket.getOutputStream(); pw = new PrintWriter(os); //Packaged as print stream pw.write("Welcome"); pw.flush(); //Cached output} catch (IOException e) { e.printStackTrace(); } finally { try { //Close the resource if (pw != null) pw.close(); if (os != null) os.close(); if (is != null) is.close(); if (br != null) br.close(); if (socket != null) socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } Client.java
package test.concurrent.socket; import java.io.*; import java.net.Socket; /** * Created by dong on 15-6-22. * Client*/ public class Client { public static void main(String[] args) { try { //1. Create client Socket, specify server port number and address Socket socket = new Socket("localhost",8888); //2. Get output stream and send information to the server OutputStream os = socket.getOutputStream(); //Byte output stream PrintWriter pw = new PrintWriter(os); //Wrap the output stream as a print stream pw.write("Username:tom; Password: 456"); pw.flush(); socket.shutdownOutput(); //Close the output stream InputStream is = socket.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String info = null; //Read while ((info = br.readLine()) != null){ System.out.println("I am the client: the server says:" + info); } br.close(); is.close(); isr.close(); pw.close(); os.close(); socket.close(); } catch (IOException e) { e.printStackTrace(); } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.