Skip to content

Instantly share code, notes, and snippets.

@Eyad-Bereh
Created June 18, 2020 20:57
Show Gist options
  • Save Eyad-Bereh/2b1a70df2e87d17e2e3e56d19e957693 to your computer and use it in GitHub Desktop.
Save Eyad-Bereh/2b1a70df2e87d17e2e3e56d19e957693 to your computer and use it in GitHub Desktop.
package app;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class App {
public static void main(String[] args) throws Exception {
int PORT_NUMBER = 45012;
int THREAD_POOL_SIZE = 10;
if (args.length == 1) {
PORT_NUMBER = Integer.parseInt(args[0]);
}
else if (args.length == 2) {
THREAD_POOL_SIZE = Integer.parseInt(args[1]);
}
ExecutorService pool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
ServerSocket serverSocket = new ServerSocket(PORT_NUMBER);
try {
while (true) {
try {
Socket clientSocket = serverSocket.accept();
Runnable clientThread = new ClientThread(clientSocket);
pool.submit(clientThread);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
serverSocket.close();
}
catch (Exception e) {
e.printStackTrace();
}
pool.shutdown();
}
}
}
package app;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import com.google.gson.Gson;
class ClientThread implements Runnable {
private static final int MESSAGE_SIZE = 1024;
private static ArrayList<Socket> clients = new ArrayList<Socket>();
private Socket clientSocket;
public ClientThread(Socket clientSocket) {
this.clientSocket = clientSocket;
clients.add(clientSocket);
Message message = new Message();
message.setSender("Server");
message.setContent("Let's welcome [" + clientSocket.getInetAddress() + ":" + clientSocket.getPort() + "] !");
message.setTimestamp(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(3)));
try {
updateClients(message);
}
catch (Exception e) {
e.printStackTrace();
}
System.out.println("Client: " + clientSocket.getInetAddress() + ":" + clientSocket.getPort()
+ " has connected at: " + LocalDateTime.now());
}
public void updateClients(Message msg) throws IOException {
byte[] buffer = new Gson().toJson(msg, Message.class).getBytes();
for (int i = 0; i < clients.size(); i++) {
if (clients.get(i).isClosed()) {
clients.remove(i);
continue;
}
DataOutputStream os = new DataOutputStream(clients.get(i).getOutputStream());
os.write(buffer);
os.flush();
}
}
public void run() {
try {
DataInputStream is;
byte[] bytes = new byte[MESSAGE_SIZE];
Message incomingMessage;
Message outgoingMessage;
while (true) {
is = new DataInputStream(clientSocket.getInputStream());
incomingMessage = new Message();
outgoingMessage = new Message();
if (is.read(bytes) == -1) {
break;
}
String validJSON = new String(bytes).trim();
System.out.println("Recieved: (" + validJSON + ") from client: " + clientSocket.getInetAddress() + ":"
+ clientSocket.getPort() + " at: " + LocalDateTime.now());
incomingMessage = new Gson().fromJson(validJSON, Message.class);
outgoingMessage.setContent(incomingMessage.getContent());
outgoingMessage.setSender(incomingMessage.getSender());
outgoingMessage.setTimestamp(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
updateClients(outgoingMessage);
bytes = new byte[MESSAGE_SIZE];
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
clientSocket.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment