Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Server {
- private static int clientCount = 0;
- private static final int MAX_CLIENTS = 5;
- public static void main(String[] args) throws IOException {
- ServerSocket handshake = new ServerSocket(4000);
- System.out.println("Server started at: " + handshake.getLocalPort());
- System.out.println("Waiting for Clients...");
- while (clientCount < MAX_CLIENTS) {
- Socket socket = handshake.accept();
- clientCount++;
- System.out.println("A new client is connected" + socket);
- DataOutputStream output_stream = new DataOutputStream(socket.getOutputStream());
- DataInputStream input_stream = new DataInputStream(socket.getInputStream());
- System.out.println("A new thread is assigning");
- Thread new_tunnel = new ClientHandler(socket, input_stream, output_stream);
- new_tunnel.start();
- }
- System.out.println("Sever has reached it's maximum capacity. Shutting down!");
- handshake.close();
- }
- }
- public class ClientHandler extends Thread {
- final Socket com_tunnel;
- final DataInputStream dis_tunnel;
- final DataOutputStream dos_tunnel;
- public ClientHandler(Socket s, DataInputStream dis, DataOutputStream dos)
- {
- this.com_tunnel = s;
- this.dis_tunnel = dis;
- this.dos_tunnel = dos;
- }
- public void run() {
- while(true){
- try {
- dos_tunnel.writeUTF("Send a number or type 'Exit' to disconnect:");
- String received = dis_tunnel.readUTF();
- if(received.equalsIgnoreCase("Exit")){
- System.out.println("Client " + this.com_tunnel + " sends exit");
- System.out.println("Closing the connection");
- this.com_tunnel.close();
- break;
- }
- try {
- int number = Integer.parseInt(received);
- List<Integer> primes = findPrimes(number);
- dos_tunnel.writeUTF("The prime numbers are: " + primes);
- } catch (NumberFormatException e) {
- dos_tunnel.writeUTF("Invalid input. Please send a valid number.");
- }
- } catch (IOException ex) {
- System.err.println("Error: " + ex.getMessage());
- break;
- }
- }
- try {
- this.dos_tunnel.close();
- this.dis_tunnel.close();
- } catch (IOException ex) {
- System.err.println("Error closing resources: " + ex.getMessage());
- }
- }
- private List<Integer> findPrimes(int num) {
- List<Integer> primes = new ArrayList<>();
- for (int i = 2; i <= num; i++) {
- if (isPrime(i)) {
- primes.add(i);
- }
- }
- return primes;
- }
- private boolean isPrime(int n) {
- if (n <= 1) return false;
- for (int i = 2; i <= Math.sqrt(n); i++) {
- if (n % i == 0) return false;
- }
- return true;
- }
- }
- public class Client {
- public static void main(String[] args)throws IOException {
- try {
- Socket clientSocket = new Socket("localhost", 4000);
- System.out.println("Connected at server Handshaking port " + clientSocket.getPort());
- System.out.println("Client is connecting at Communication Port " + clientSocket.getLocalPort());
- System.out.println("Client is Connected");
- Scanner scn = new Scanner(System.in);
- DataOutputStream dos = new DataOutputStream(clientSocket.getOutputStream());
- DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
- while(true){
- String inLine = dis.readUTF();
- System.out.println(inLine);
- String outLine = scn.nextLine();
- dos.writeUTF(outLine);
- if(outLine.equalsIgnoreCase("Exit")){
- System.out.println("Closing the connection "+ clientSocket);
- clientSocket.close();
- System.out.println("Connection Closed");
- break;
- }
- String received = dis.readUTF();
- System.out.println(received);
- }
- dos.close();
- dis.close();
- } catch (Exception ex) {
- System.err.println("Error: " + ex.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement