Skip to content
Open

Lab6 #23

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added 6.1Time/Client.jar
Binary file not shown.
Binary file added 6.1Time/Server.jar
Binary file not shown.
20 changes: 20 additions & 0 deletions 6.1Time/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>niit</groupId>
<artifactId>6.1Time</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>


</project>
112 changes: 112 additions & 0 deletions 6.1Time/src/main/java/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
25 changes: 25 additions & 0 deletions 6.1Time/src/main/java/sample/Main.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
59 changes: 59 additions & 0 deletions 6.1Time/src/main/java/sample/Server.java
Original file line number Diff line number Diff line change
@@ -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();
}

}

Binary file added 6.1Time/src/main/resources/1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 6.1Time/src/main/resources/2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions 6.1Time/src/main/resources/myCSS.css
Original file line number Diff line number Diff line change
@@ -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;
}
51 changes: 51 additions & 0 deletions 6.1Time/src/main/resources/sample.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="650.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<VBox alignment="CENTER" prefHeight="650.0" prefWidth="750.0" spacing="10.0">
<children>
<TextField fx:id="tfTime" alignment="CENTER" prefHeight="50.0" prefWidth="300.0">
<VBox.margin>
<Insets left="125.0" right="125.0" />
</VBox.margin></TextField>
<TextField fx:id="tfDate" alignment="CENTER" prefHeight="50.0" prefWidth="269.0">
<VBox.margin>
<Insets left="125.0" right="125.0" />
</VBox.margin></TextField>
<VBox alignment="BOTTOM_CENTER" nodeOrientation="LEFT_TO_RIGHT" prefHeight="103.0" prefWidth="300.0" spacing="5.0">
<children>
<Button fx:id="btnGet" mnemonicParsing="false" onAction="#getTimeDate" prefHeight="30.0" prefWidth="170.0" text="What time is it now?" />
<Button fx:id="btnStop" mnemonicParsing="false" onAction="#stop" prefHeight="30.0" prefWidth="170.0" text="Stop" />
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<VBox.margin>
<Insets top="50.0" />
</VBox.margin>
<padding>
<Insets bottom="5.0" />
</padding>
</VBox>
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets left="100.0" right="100.0" top="30.0" />
</padding>
</VBox>
</children>
<opaqueInsets>
<Insets />
</opaqueInsets>
<padding>
<Insets left="50.0" />
</padding>
</Pane>
20 changes: 20 additions & 0 deletions 6.2MessageInaBottle/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>niit</groupId>
<artifactId>6.2Aphorism</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>


</project>
34 changes: 34 additions & 0 deletions 6.2MessageInaBottle/src/main/java/sample/ClientObserve.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package sample;

import java.io.*;
import java.net.Socket;

public class ClientObserve {

private Socket server;
private BufferedReader in;

public ClientObserve(Socket socket){
this.server =socket;
try{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
}catch (IOException ex){
System.out.println("Клиент: Ошибка инициализации потоков");
}
}


public String getMessage(){
String message = null;
try{
message=in.readLine();
System.out.println(message);

}catch (IOException ex){
System.out.println("Клиент: Ошибка передачи данных");
}

return message;
}

}
49 changes: 49 additions & 0 deletions 6.2MessageInaBottle/src/main/java/sample/Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package sample;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;

public class Controller {

@FXML
private TextField tfMessage;
@FXML
private Button btnGetMessage;

private Socket socket;
private ClientObserve client;
static final int PORT=1234;
static final String HOST="127.0.0.1";
Media media;


@FXML
private void getMessage() throws IOException {

try{
socket=new Socket(HOST,PORT);
}catch (Exception ee){
System.out.println("Не удается соединиться с сервером!");
return;
}
System.out.println("Соединение с сервером установлено!");


client=new ClientObserve(socket);

String newMessage = client.getMessage();
socket.close();

URL resource = sample.Controller.class.getResource("/1.mp3");
media = new Media(resource.toString());
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
tfMessage.setText(newMessage);
}
}
Loading