Server.java 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.company;
  2. import java.io.IOException;
  3. import java.net.ServerSocket;
  4. import java.net.Socket;
  5. import java.sql.Timestamp;
  6. import java.util.ArrayList;
  7. public class Server {
  8. private ServerSocket socket;
  9. private boolean interrupt;
  10. private ArrayList<Client> clients;
  11. public Server() throws IOException {
  12. socket = new ServerSocket(8080);
  13. interrupt = false;
  14. clients = new ArrayList<>();
  15. }
  16. public void Run() {
  17. while (!interrupt) {
  18. try {
  19. Socket clientSocket = socket.accept();
  20. Client client = new Client(clientSocket);
  21. // Добавление клиента в список всех подключенных клиентов
  22. clients.add(client);
  23. // Старт работы потока с подключенным клиентом
  24. client.start();
  25. } catch (IOException exception) {
  26. System.out.println(exception.getMessage());
  27. }
  28. }
  29. for (Client cl: clients) {
  30. cl.interrupt();
  31. try {
  32. cl.join();
  33. } catch (InterruptedException exception) {
  34. System.out.println(exception.getMessage());
  35. }
  36. }
  37. }
  38. }