java multithreading enables communication between the server and multiple clients
- 2020-05-12 02:38:18
- OfStack
Build a network server with java language, realize the communication between the client and the server, and realize that the client has independent threads without interfering with each other.
Applying multithreading to realize the basic steps of communication between server and multithreading
The server side creates ServerSocket and loops accept() to wait for the client link The client creates an Socket and requests a link to the server The server accepts the client's request and creates socekt to establish a dedicated link with the client The linked socket talks on a separate thread The server continues to wait for the new link
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.
* Based on the TCP Of the agreement Socket Communication, user login
* The server side
*/
public class Server {
public static void main(String[] args) {
try {
//1 , create, 1 Server side Socket, namely ServerSocket, Specify the bound port and listen on 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 and is waiting for the client to connect ***");
// Loop listens for links to waiting clients
while (true){
// call accept() Method starts listening and waits for the client to link
socket = serverSocket.accept();
// create 1 Five new threads
ServerThread serverThread = new ServerThread(socket);
// Starting a thread
serverThread.start();
count++; // Count the number of clients
System.out.println(" Number of clients : " + count);
InetAddress address = socket.getInetAddress();
System.out.println(" Current client IP : " + 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 threads handle classes
*/
public class ServerThread extends Thread {
// Related to this thread Socket
Socket socket = null;
public ServerThread(Socket socket){
this.socket = socket;
}
// An action performed by a thread in response to a request from the client
public void run(){
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
OutputStream os = null;
PrintWriter pw = null;
try {
// To obtain 1 Input stream, and read the client's information
is = socket.getInputStream();
isr = new InputStreamReader(is); // Converts the byte stream into a character stream
br = new BufferedReader(isr); // Add the buffer
String info = null;
// Loop read data
while ((info = br.readLine()) != null){
System.out.println(" I'm the server, said the client : " +info);
}
socket.shutdownInput(); // Close the input stream
// Gets the output stream and responds to the client's request
os = socket.getOutputStream();
pw = new PrintWriter(os); // Wrap as a print stream
pw.write(" You're welcome ");
pw.flush(); // Cache the 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 (isr != null)
isr.close();
if (br != null)
br.close();
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
The client Client java
package test.concurrent.socket;
import java.io.*;
import java.net.Socket;
/**
* Created by dong on 15-6-22.
* The client
*/
public class Client {
public static void main(String[] args) {
try {
//1 Create a client Socket , specifying the 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); // Wrap the output stream as a print stream
pw.write(" The user name :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;
// Loop reads
while ((info = br.readLine()) != null){
System.out.println(" I'm the client : The server said :" + info);
}
br.close();
is.close();
isr.close();
pw.close();
os.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}