Browse Source

multithread-try

master
esoe 2 years ago
parent
commit
db39f985c0
  1. 3
      src/main/java/ru/molokoin/App.java
  2. 59
      src/main/java/ru/molokoin/ExchangeServer.java
  3. 4
      src/main/java/ru/molokoin/MainFrameController.java
  4. BIN
      target/classes/ru/molokoin/App.class
  5. BIN
      target/classes/ru/molokoin/ExchangeServer.class
  6. BIN
      target/classes/ru/molokoin/MainFrameController.class

3
src/main/java/ru/molokoin/App.java

@ -15,6 +15,7 @@ public class App extends Application {
@Override @Override
public void start(Stage stage){ public void start(Stage stage){
System.out.printf("%s started... \n", Thread.currentThread().getName());
try { try {
String linkFXML = "/face/mainframe.fxml"; String linkFXML = "/face/mainframe.fxml";
String linkCSS = "/face/styles.css"; String linkCSS = "/face/styles.css";
@ -31,7 +32,9 @@ public class App extends Application {
} }
public static void main(String[] args) { public static void main(String[] args) {
System.out.printf("%s started... \n", Thread.currentThread().getName());
launch(); launch();
} }
} }

59
src/main/java/ru/molokoin/ExchangeServer.java

@ -9,35 +9,48 @@ import java.io.PrintWriter;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; 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{ ExchangeServer(){
private static int port = 8081; isActive = true;
private static String link = "localhost";
private static int num = 0;
ServerSocket serverSocket;
Socket socket;
//подготовка компонентов
public void init(){
} }
ExchangeServer (int port){
//остановка серера isActive = true;
public void stop(){ setPort(port);
try {
serverSocket.close();
//socket.close();
} catch (Exception e) {
System.out.println("Остановить сервер не удалось. Ошибка: " + e);
}
} }
/**
* @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)) { try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server started at port: " + port + " ..."); System.out.println("Server started at port: " + port + " ...");
while (true) { while (isActive) {
// ожидаем подключения // ожидаем подключения
Socket socket = serverSocket.accept(); Socket socket = serverSocket.accept();
System.out.println("Client connected ..."); System.out.println("Client connected ...");
@ -87,7 +100,6 @@ public class ExchangeServer{
} }
output.flush(); output.flush();
} }
catch (IOException e) { catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -98,6 +110,7 @@ public class ExchangeServer{
System.out.println("num: " + num); System.out.println("num: " + num);
} }
} }
System.out.printf("%s finished... \n", Thread.currentThread().getName());
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

4
src/main/java/ru/molokoin/MainFrameController.java

@ -18,21 +18,19 @@ public class MainFrameController implements Initializable{
@Override @Override
public void initialize(URL location, ResourceBundle resources) { public void initialize(URL location, ResourceBundle resources) {
// TODO Auto-generated method stub
System.out.println("Инициализация компонентов интерфейса ..."); System.out.println("Инициализация компонентов интерфейса ...");
server = new ExchangeServer(); server = new ExchangeServer();
} }
@FXML @FXML
public void buttonClicked(Event e){ public void buttonClicked(Event e){
System.out.println("Button clicked");
if (btn.getText().equals("START")){ if (btn.getText().equals("START")){
btn.setText("STOP"); btn.setText("STOP");
server.start(); server.start();
}else { }else {
btn.setText("START"); btn.setText("START");
server.stop(); server.finish();
} }
} }

BIN
target/classes/ru/molokoin/App.class

Binary file not shown.

BIN
target/classes/ru/molokoin/ExchangeServer.class

Binary file not shown.

BIN
target/classes/ru/molokoin/MainFrameController.class

Binary file not shown.
Loading…
Cancel
Save