diff --git a/6.1Time/Client.jar b/6.1Time/Client.jar new file mode 100644 index 0000000..43ebd44 Binary files /dev/null and b/6.1Time/Client.jar differ diff --git a/6.1Time/Server.jar b/6.1Time/Server.jar new file mode 100644 index 0000000..20a51af Binary files /dev/null and b/6.1Time/Server.jar differ diff --git a/6.1Time/pom.xml b/6.1Time/pom.xml new file mode 100644 index 0000000..86f4f52 --- /dev/null +++ b/6.1Time/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + niit + 6.1Time + 1.0-SNAPSHOT + + + + + src/main/resources + + + + + + \ No newline at end of file diff --git a/6.1Time/src/main/java/sample/Controller.java b/6.1Time/src/main/java/sample/Controller.java new file mode 100644 index 0000000..19afdc7 --- /dev/null +++ b/6.1Time/src/main/java/sample/Controller.java @@ -0,0 +1,112 @@ +package sample; + +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.Button; +import javafx.scene.control.TextField; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.PrintWriter; +import java.net.*; +import java.util.ResourceBundle; + +public class Controller implements Initializable { + + @FXML + private TextField tfTime; + @FXML + private TextField tfDate; + @FXML + private Button btnGet; + @FXML + private Button btnStop; + + private Socket server; + BufferedReader in=null; + PrintWriter out=null; + private String streamTime; + + @Override + public void initialize(URL location, ResourceBundle resources) { + streamTime=new String("go"); + tfTime.setText("HH:MM:SS"); + tfDate.setText("dd.mm.yyyy"); + } + + @FXML + private void getTimeDate() throws IOException { + + try{ + server = new Socket("127.0.0.1", 2345); + }catch (UnknownHostException e){ + System.out.println("Unknown host"); + System.exit(-1);} + catch (NoRouteToHostException e){ + System.out.println("No connection"); + System.exit(-1);} + catch (ConnectException e){ + System.out.println("Connection error"); + System.exit(-1); } + catch (IOException e){ + System.out.println("Output-input error"); + System.exit(-1); } + + try { + in=new BufferedReader(new InputStreamReader(server.getInputStream())); + out=new PrintWriter(server.getOutputStream(),true); + + }catch (IOException e){ + System.out.println("Error creating threads"); + System.exit(-1); } + + new Thread(()->{ + String fserver; + String time=new String("HH:mm:ss"); + String date=new String("dd.MM.yyyy"); + + while(streamTime.equals("go")) { + try { + out.println(streamTime); + + fserver = in.readLine(); + time = fserver.substring(0, 8); + date = fserver.substring(8); + System.out.println(time); + System.out.println(date); + }catch (IOException e){ + System.out.println(e); + } + + String time1=time; + String date1=date; + Platform.runLater(()->setTimeDate(time1,date1));//change data to scene + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); } + } + }).start(); + } + + private void setTimeDate(String time, String date){ //for set time on scene + tfTime.setText(time); + tfDate.setText(date); + } + + private void setStreamTime(String world){ //for stop of time + this.streamTime=world; + } + + @FXML + private void stop() throws IOException { + + setStreamTime("stop"); + tfTime.setText(""); + tfDate.setText(""); + out.close(); + in.close(); + server.close(); + } +} diff --git a/6.1Time/src/main/java/sample/Main.java b/6.1Time/src/main/java/sample/Main.java new file mode 100644 index 0000000..9f277cf --- /dev/null +++ b/6.1Time/src/main/java/sample/Main.java @@ -0,0 +1,25 @@ +package sample; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.stage.Stage; + +public class Main extends Application { + + @Override + public void start(Stage primaryStage) throws Exception{ + Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml")); + Scene scene=new Scene(root); + scene.getStylesheets().add(getClass().getResource("/myCSS.css").toExternalForm()); + primaryStage.setTitle("Time and date"); + primaryStage.setScene(scene); + primaryStage.show(); + } + + + public static void main(String[] args) { + launch(args); + } +} diff --git a/6.1Time/src/main/java/sample/Server.java b/6.1Time/src/main/java/sample/Server.java new file mode 100644 index 0000000..fddcd80 --- /dev/null +++ b/6.1Time/src/main/java/sample/Server.java @@ -0,0 +1,59 @@ +package sample; + +import java.io.*; +import java.net.*; +import java.text.SimpleDateFormat; +import java.util.Calendar; + +public class Server { + public static void main(String[] args) throws IOException{ + + BufferedReader in=null; + PrintWriter out=null; + ServerSocket server = null; + Socket client=null; + + try { + server=new ServerSocket(2345); + } catch (IOException e){ + System.out.println("Connection error with port 2345"); + System.exit(-1); + } + + try{ + System.out.println("Waiting connection"); + client=server.accept(); + System.out.println("Client connected"); + }catch (IOException e){ + System.out.println("Unable to establish connection"); + System.exit(-1); + } + + in = new BufferedReader(new InputStreamReader(client.getInputStream())); + out=new PrintWriter(client.getOutputStream(),true); + String input; + + while((input=in.readLine())!=null) { + + if(input.equalsIgnoreCase("stop")) + break; + + String date = new SimpleDateFormat("dd.MM.yyyy").format(Calendar.getInstance().getTime()); + String time = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime()); + out.println(time + date); + out.flush(); + System.out.println(time + " " + date); + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); } + } + out.close(); + in.close(); + client.close(); + server.close(); + } + +} + diff --git a/6.1Time/src/main/resources/1.jpg b/6.1Time/src/main/resources/1.jpg new file mode 100644 index 0000000..0b626c7 Binary files /dev/null and b/6.1Time/src/main/resources/1.jpg differ diff --git a/6.1Time/src/main/resources/2.jpg b/6.1Time/src/main/resources/2.jpg new file mode 100644 index 0000000..0db9b06 Binary files /dev/null and b/6.1Time/src/main/resources/2.jpg differ diff --git a/6.1Time/src/main/resources/myCSS.css b/6.1Time/src/main/resources/myCSS.css new file mode 100644 index 0000000..087b05c --- /dev/null +++ b/6.1Time/src/main/resources/myCSS.css @@ -0,0 +1,18 @@ +.root{ + -fx-font: bold italic 10pt "LucidaBrightDemiBold"; + -fx-background-image: url("2.jpg"); +} + +.text-field{ + -fx-font: bold italic 20pt "LucidaBrightDemiBold"; + -fx-padding: 6 6 6 6; + -fx-text-fill: green; + -fx-background-color: white; +} + +.button{ + -fx-border-color: rgba(255, 255, 255, 1.80); + -fx-border-radius: 8; + -fx-background-color: white; + -fx-text-fill: green; +} diff --git a/6.1Time/src/main/resources/sample.fxml b/6.1Time/src/main/resources/sample.fxml new file mode 100644 index 0000000..9b4aec8 --- /dev/null +++ b/6.1Time/src/main/resources/sample.fxml @@ -0,0 +1,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + +