Application of multithreading to achieve the communication between the server and multithreading basic steps 1, server-side Create ServerSocket, loop call accept () Waiting for client link 2, the client creates a socket and the request and server-side link 3, the server side accepts the client request, Create a Socekt with the client to establish a leased line Link 4, establish a 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 socket, that is, ServerSocket, specify the bound port, and listen to this port serversocket serversocket = new ServerSocket (8888); Socket socket = NULL; Number of logged clients int count = 0; SYSTEM.OUT.PRINTLN ("* * * server is about to start, waiting for client links * * *"); The loop listener waits for the client's link while (true) {//calls the Accept () method to start listening, waiting for the client's link to socket = Serversocket.ac Cept (); Create a new thread serverthread Serverthread = new Serverthread (socket); Start thread Serverthread.start (); count++; Count the number of clients System.out.println ("Number of clients:" + count); InetAddress address = socket.getinetaddress (); SYstem.out.println ("IP of 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 performed by the thread, responding 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, the client says" +info); } socket.shutdowninput (); Closes the input stream//gets the output stream, responding to the client's request OS = Socket.getoutputstream (); PW = new PrintWriter (OS); PackagingFor the print stream 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 (); } } }}
Client Client.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 client socket, specify server port number and address socket socket = new socket ("localhost", 8888); 2, get the output stream, send information to the server outputstream OS = Socket.getoutputstream (); BYTE output stream PrintWriter pw = new PrintWriter (OS); Wraps the output stream as a print stream pw.write ("User name: 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 the client: Server says:" + 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