The basic steps of applying multithreading to communicate between server and multithreading 1, server side create ServerSocket, loop call accept () wait for Client Link 2, client create a socket and request and server side link 3, The server side accepts the client request, creates a Socekt with the client to establish a leased line link 4, establishes the linked socket on a separate thread on the dialog 5, the server continues to wait for the new linked server side Server.java
Package Test.concurrent.socket;import Java.io.*;import Java.net.inetaddress;import java.net.serversocket;import java.net.socket;/** * Created 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 sock ET, serversocket, specifies the bound port and listens on this port serversocket serversocket = new ServerSocket (8888); Socket socket = NULL; Number of client records int count = 0; SYSTEM.OUT.PRINTLN ("* * * server is about to start, wait for client link * * *"); The loop listens for the link waiting for the client while (true) {//calls the Accept () method to start listening, waiting for the client to link the socket = Serversoc Ket.accept (); Create a new thread serverthread Serverthread = new Serverthread (socket); Start thread Serverthread.start (); count++; Count the number of client System.out.println ("Number of client:" + count); InetAddress address = socket.getinetaddress (); System.out.println ("IP of the current client:" + address.gethostaddress ()); }} catch (IOException e) {e.printstacktrace (); } }}
Server thread processing class Serverthread.java
Package Test.concurrent.socket;import Java.io.*;import java.net.socket;/** * Created-on 15-6-22. * Server thread processing Classes */public class Serverthread extends Thread {//and the socket socket socket associated with this threading = null; Public serverthread (socket socket) {this.socket = socket; }//The operation that the thread is running in response to the client's request public void run () {InputStream is = null; InputStreamReader ISR = null; BufferedReader br = null; OutputStream OS = null; PrintWriter pw = null; try {//Gets an input stream and reads the client's information is = Socket.getinputstream (); ISR = new InputStreamReader (IS); Convert bytes to character stream br = new BufferedReader (ISR); Add buffer String info = null; The loop reads the data while ((info = br.readline ()) = null) {System.out.println ("I am the server.") Client said: "+info); } socket.shutdowninput (); Closes the input stream//gets the output stream. Response to client request OS = Socket.getoutputstream (); PW = new PriNtwriter (OS); Package for print flow pw.write ("welcome You"); Pw.flush (); The cache output} catch (IOException e) {e.printstacktrace (); }finally {try {//close resource if (PW = null) Pw.clo SE (); if (OS! = null) os.close (); if (is! = null) is.close (); if (ISR! = null) isr.close (); if (br! = null) br.close (); if (socket! = NULL) socket.close (); } catch (IOException e) {e.printstacktrace (); } } }}
Clientclient.java
Package Test.concurrent.socket;import Java.io.*;import java.net.socket;/** * Created-on 15-6-22. * Client */public class Client {public static void main (string[] args) {try {//1, create Clientsocket, specify Server port number and address socket socket = new socket ("localhost", 8888); 2. Get output stream, send information to server outputstream OS = Socket.getoutputstream (); BYTE output stream PrintWriter pw = new PrintWriter (OS); Wraps the output stream as a print stream pw.write ("Username:tom; password:456 "); Pw.flush (); Socket.shutdownoutput (); Turn off the output stream InputStream is = Socket.getinputstream (); InputStreamReader ISR = new InputStreamReader (IS); BufferedReader br = new BufferedReader (ISR); String info = null; The loop reads while ((info = br.readline ()) = null) {System.out.println ("I Am client:server," "+ info"); } br.close (); Is.close (); Isr.close (); Pw.close (); Os.close (); Socket.close (); } catch (IOException e) {e.printstacktrace (); } }}
Application of Java Multithreading for server-side and multi-client communication