Java Examples - Socket Implementation of a Multithreaded Server Program
In Java, implementing a multithreaded server program can be done by usingServerSocketto listen for client connections. Whenever a new client connects, a new thread is started to handle that connection. Below is an example code demonstrating how to use Java Socket to implement a multithreaded server program.
Server-side code
First, we create a server-side program that listens on a specified port and starts a new thread for each client connection to handle communication.
MultiThreadedServer.java file
Client-side code
The client code is used to connect to the server and send messages. You can create multiple clients to test the server's multithreading capability.
Client.java file code:
import java.net.*;
public class Client {
public static void main(String[] args) {
String serverAddress = "localhost";
int port = 12345; // Server port
try (Socket socket = new Socket(serverAddress, port)) {
System.out.println("Connected to server");
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter(output, true);
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));
String userInput;
while ((userInput = consoleReader.readLine()) != null) {
writer.println(userInput);
String serverResponse = reader.readLine();
System.out.println("Server response: " + serverResponse);
}
} catch (UnknownHostException e) {
System.err.println("Unknown host: " + serverAddress);
e.printStackTrace();
} catch (IOException e) {
System.err.println("Unable to get I/O connection to: " + serverAddress);
e.printStackTrace();
}
}
}
Running steps
-
Run the server side:
- Compile and run
MultiThreadedServerclass. - The server will start and listen for client connections on the specified port.
- Compile and run
-
Run the client:
- Compile and run multiple
Clientclass instances to simulate multiple clients connecting to the server. - Enter a message in the client console. The client will send the message to the server, and the server will respond with the same message.
- Compile and run multiple
Explanation
ServerSocket: The socket used on the server side to listen for client connections.Socket: The communication channel between the client and the server.ClientHandler: The thread class that handles each client connection.BufferedReaderandPrintWriter: Used for reading and writing data streams.
This implementation ensures that the server can handle multiple client connections simultaneously without one client's long-running operation blocking other clients' requests.
Compile the code
Open a terminal or command prompt, switch to the directory containing the above Java file, and compile the code:
javac MultiThreadedServer.java Client.java
After compilation is complete, first run the server program. The server will start and begin listening on port 12345.
java MultiThreadedServer
You will see the server output:
Server started, waiting for client connection...
Open another terminal or command prompt window and run the client program. You can run multiple client instances to test multithreaded processing.
java Client
You will see the client output:
Connected to server
Then, you can enter a message in the client console, for example:
Hello Server
The server will respond:
服务器回应: Hello Server
The server-side output will display:
客户端已连接: 127.0.0.1 收到客户端消息: Hello ServerOther Extensions