diff --git a/src/main/java/ru/molokoin/App.java b/src/main/java/ru/molokoin/App.java index a061a18..3ada3e9 100644 --- a/src/main/java/ru/molokoin/App.java +++ b/src/main/java/ru/molokoin/App.java @@ -15,6 +15,7 @@ public class App extends Application { @Override public void start(Stage stage){ + System.out.printf("%s started... \n", Thread.currentThread().getName()); try { String linkFXML = "/face/mainframe.fxml"; String linkCSS = "/face/styles.css"; @@ -31,7 +32,9 @@ public class App extends Application { } public static void main(String[] args) { + System.out.printf("%s started... \n", Thread.currentThread().getName()); launch(); + } } diff --git a/src/main/java/ru/molokoin/ExchangeServer.java b/src/main/java/ru/molokoin/ExchangeServer.java index 1a8c505..64d33a5 100644 --- a/src/main/java/ru/molokoin/ExchangeServer.java +++ b/src/main/java/ru/molokoin/ExchangeServer.java @@ -9,41 +9,54 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.nio.charset.StandardCharsets; +/** + * + */ +public class ExchangeServer extends Thread{ + private int port = 8081; + private int num = 0; + public static boolean isActive; + public ServerSocket serverSocket; + public Socket socket; -public class ExchangeServer{ - private static int port = 8081; - private static String link = "localhost"; - private static int num = 0; - ServerSocket serverSocket; - Socket socket; - - - //подготовка компонентов - public void init(){ - + ExchangeServer(){ + isActive = true; } - - //остановка серера - public void stop(){ - try { - serverSocket.close(); - //socket.close(); - } catch (Exception e) { - System.out.println("Остановить сервер не удалось. Ошибка: " + e); - } + ExchangeServer (int port){ + isActive = true; + setPort(port); + } + /** + * @return the port + */ + public int getPort() { + return port; + } + /** + * @param port the port to set + */ + public void setPort(int port) { + this.port = port; } + //осановка сервера + public void finish(){ + isActive = false; + } + //запуск сервера - public void start(){ + @Override + public void run(){ + System.out.printf("%s started... \n", Thread.currentThread().getName()); try (ServerSocket serverSocket = new ServerSocket(port)) { System.out.println("Server started at port: " + port + " ..."); - - while (true) { + + while (isActive) { // ожидаем подключения Socket socket = serverSocket.accept(); System.out.println("Client connected ..."); num++; - // для подключившегося клиента открываем потоки + // для подключившегося клиента открываем потоки // чтения и записи try (BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); PrintWriter output = new PrintWriter(socket.getOutputStream())) { @@ -87,17 +100,17 @@ public class ExchangeServer{ } output.flush(); } - catch (IOException e) { e.printStackTrace(); } - - // по окончанию выполнения блока try-with-resources потоки, + + // по окончанию выполнения блока try-with-resources потоки, // а вместе с ними и соединение будут закрыты System.out.println("Client disconnected!"); System.out.println("num: " + num); } } + System.out.printf("%s finished... \n", Thread.currentThread().getName()); } catch (IOException ex) { ex.printStackTrace(); } @@ -106,5 +119,5 @@ public class ExchangeServer{ public static void main(String[] args) { new ExchangeServer().start(); } - + } diff --git a/src/main/java/ru/molokoin/MainFrameController.java b/src/main/java/ru/molokoin/MainFrameController.java index f10b37d..67a5a8e 100644 --- a/src/main/java/ru/molokoin/MainFrameController.java +++ b/src/main/java/ru/molokoin/MainFrameController.java @@ -18,21 +18,19 @@ public class MainFrameController implements Initializable{ @Override public void initialize(URL location, ResourceBundle resources) { - // TODO Auto-generated method stub System.out.println("Инициализация компонентов интерфейса ..."); server = new ExchangeServer(); } @FXML public void buttonClicked(Event e){ - System.out.println("Button clicked"); if (btn.getText().equals("START")){ btn.setText("STOP"); server.start(); }else { btn.setText("START"); - server.stop(); + server.finish(); } } diff --git a/target/classes/ru/molokoin/App.class b/target/classes/ru/molokoin/App.class index 2ab806e..14603a5 100644 Binary files a/target/classes/ru/molokoin/App.class and b/target/classes/ru/molokoin/App.class differ diff --git a/target/classes/ru/molokoin/ExchangeServer.class b/target/classes/ru/molokoin/ExchangeServer.class index f1b35b0..5240a0c 100644 Binary files a/target/classes/ru/molokoin/ExchangeServer.class and b/target/classes/ru/molokoin/ExchangeServer.class differ diff --git a/target/classes/ru/molokoin/MainFrameController.class b/target/classes/ru/molokoin/MainFrameController.class index 7cf00c0..666da0b 100644 Binary files a/target/classes/ru/molokoin/MainFrameController.class and b/target/classes/ru/molokoin/MainFrameController.class differ