Thread pool and Socket Programming Example
//Thread pool and Socket Programming Example
package dev;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private static final int THREAD_COUNT = 5;
private static final short LISTEN_PORT = 23;
ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
void run() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(LISTEN_PORT);
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
try {
System.out.println("Waiting for connection at port "+LISTEN_PORT);
Socket connection = serverSocket.accept();
threadPool.execute(new ConnectionManager(connection));
System.out.println("New connection established");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
Server server = new Server();
server.run();
}
}
class ConnectionManager implements Runnable {
private static int taskCount = 0;
private final int id = taskCount++;
Socket connection = null;
BufferedReader in;
BufferedWriter out;
public ConnectionManager(Socket connection) {
this.connection = connection;
}
public void run() {
try {
System.out.println("Connection received from "
+ connection.getInetAddress().getHostName()+" "+connection.getInetAddress().getHostAddress());
in = new BufferedReader(new InputStreamReader(connection
.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(connection
.getOutputStream()));
out.write("Welcome to Socket Server, User id:" + id);
out.flush();
String message = null;
do {
out.write("\r\n" + id + " >");
out.flush();
message = in.readLine();
if(message == null){break;}
System.out.println("client" + id + ">" + message);
if (message.trim().equals("hi")) {
out.write("hello");
out.flush();
} else if (message.trim().equals("bye")) {
out.write("bye");
out.flush();
}
} while (!message.trim().equals("bye"));
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
in.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
package dev;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Server {
private static final int THREAD_COUNT = 5;
private static final short LISTEN_PORT = 23;
ExecutorService threadPool = Executors.newFixedThreadPool(THREAD_COUNT);
void run() {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(LISTEN_PORT);
} catch (IOException e1) {
e1.printStackTrace();
}
while (true) {
try {
System.out.println("Waiting for connection at port "+LISTEN_PORT);
Socket connection = serverSocket.accept();
threadPool.execute(new ConnectionManager(connection));
System.out.println("New connection established");
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) {
Server server = new Server();
server.run();
}
}
class ConnectionManager implements Runnable {
private static int taskCount = 0;
private final int id = taskCount++;
Socket connection = null;
BufferedReader in;
BufferedWriter out;
public ConnectionManager(Socket connection) {
this.connection = connection;
}
public void run() {
try {
System.out.println("Connection received from "
+ connection.getInetAddress().getHostName()+" "+connection.getInetAddress().getHostAddress());
in = new BufferedReader(new InputStreamReader(connection
.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(connection
.getOutputStream()));
out.write("Welcome to Socket Server, User id:" + id);
out.flush();
String message = null;
do {
out.write("\r\n" + id + " >");
out.flush();
message = in.readLine();
if(message == null){break;}
System.out.println("client" + id + ">" + message);
if (message.trim().equals("hi")) {
out.write("hello");
out.flush();
} else if (message.trim().equals("bye")) {
out.write("bye");
out.flush();
}
} while (!message.trim().equals("bye"));
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
try {
in.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}