Skip to content

Instantly share code, notes, and snippets.

@sinanduman
Last active November 6, 2015 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinanduman/1148ca254f84d63af789 to your computer and use it in GitHub Desktop.
Save sinanduman/1148ca254f84d63af789 to your computer and use it in GitHub Desktop.
ServerSocket example using ThreadPool
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
public class ClientService {
Socket socket = null;
DataInputStream dis = null;
DataOutputStream dos = null;
Scanner scan = null;
public static void main(String[] args) {
ClientService cs = new ClientService();
cs.run();
}
public void run() {
try {
socket = new Socket("127.0.0.1", 7777);
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
scan = new Scanner(System.in);
String message = scan.nextLine();
dos.writeUTF(message);
String inStr = dis.readUTF();
if (inStr != null) {
System.out.println(inStr);
}
if (message.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ServerService implements Closeable {
static ServerSocket serversocket = null;
static {
try {
serversocket = new ServerSocket(7777);
} catch (IOException e) {
e.printStackTrace();
}
}
public ServerService() {
}
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 0; i < 10; i++) {
executor.submit(new RunService());
}
}
@Override
public void close() throws IOException {
serversocket.close();
}
}
class RunService implements Runnable {
Socket socket = null;
DataInputStream dis = null;
DataOutputStream dos = null;
@Override
public void run() {
try {
socket = ServerService.serversocket.accept();
dis = new DataInputStream(socket.getInputStream());
dos = new DataOutputStream(socket.getOutputStream());
while (true) {
String tmpStr = dis.readUTF();
if (tmpStr != null) {
System.out.println(tmpStr);
dos.writeUTF("Hi " + tmpStr);
if (tmpStr.equals("bye")) {
System.out.println("Arrivederci");
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
dis.close();
dos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@sinanduman
Copy link
Author

  • Added guard for Closing ServerService
  • Added sending message back to ClientService
  • Decreased the poolsize to see a few active threads but to see waiting a few more ClientService

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment