14.03.2021, 03:50. Показов 1930. Ответов 0
Необходимо написать программу на архитектуре Клиент-сервер, чтобы с клавиатуры к клиенту вводилось два числа, находилась их сумма (в идеале найти их НОД, но пока бы просто найти сумму) и передавалась серверу.
Вроде как читает два символа, передаёт клиенту, но сумма нормальная не находится, лишь какое-то непонятное число, не могу понять, в чем проблема.
код прикрепляю
Посмотрите пожалуйста, очень надеюсь на вашу помощь
Мейн (клиент?)
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| //
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
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 {
public Main() {
}
public void start(Stage primaryStage) throws Exception {
Parent root = (Parent)FXMLLoader.load(this.getClass().getResource("sample.fxml"));
primaryStage.setTitle("My Application");
primaryStage.setScene(new Scene(root, 684, 476 ));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
} |
|
сервер
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| //
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package sample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private static Socket clientSocket;
private static ServerSocket server;
private static BufferedReader in;
private static BufferedWriter out;
private static BufferedReader in1;
private static BufferedWriter out1;
public Server() {
}
public static void main(String[] args) {
try {
try {
server = new ServerSocket(4004);
System.out.println("Сервер запущен!");
clientSocket = server.accept();
System.out.println("Клиент подключен!");
try {
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
int word = Integer.parseInt(in.readLine());
in1 = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out1 = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
int word1 = Integer.parseInt(in.readLine());
int c = word + word1;
String temp = String.valueOf(c);
String myStr = temp;
out.write(myStr);
out.flush();
} finally {
clientSocket.close();
System.out.println("Сокет клиента закрыт!");
in.close();
out.close();
}
} finally {
server.close();
System.out.println("Сокет сервера закрыт!");
}
} catch (IOException var14) {
var14.printStackTrace();
}
}
} |
|
котроллер, тут как я понимаю, числа передаются клиенту
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
| //
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package sample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Controller {
Socket clientSocket = null;
BufferedReader in;
BufferedWriter out;
BufferedReader in1;
BufferedWriter out1;
@FXML
private ResourceBundle resources;
@FXML
private URL location;
@FXML
private TextField PortTextfield;
@FXML
private TextField IPTextfield;
@FXML
private TextField resultTextField;
@FXML
private TextField sendTextField;
@FXML
private TextField sendTextField1;
@FXML
private Button connectButton;
@FXML
private Button sendButton;
public Controller() {
}
@FXML
void initialize() {
this.connectButton.setOnAction((event1) -> {
try {
this.clientSocket = new Socket(InetAddress.getByName(this.IPTextfield.getText()), Integer.parseInt(this.PortTextfield.getText()));
} catch (IOException var3) {
var3.printStackTrace();
}
this.sendButton.setOnAction((event2) -> {
if (this.clientSocket != null) {
try {
this.in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.out = new BufferedWriter(new OutputStreamWriter(this.clientSocket.getOutputStream()));
int word = Integer.parseInt(this.sendTextField1.getText());
this.out.write(word + "\n");
this.in1 = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
this.out1 = new BufferedWriter(new OutputStreamWriter(this.clientSocket.getOutputStream()));
int word1 = Integer.parseInt(this.sendTextField1.getText());
this.out1.write(word1 + "\n");
this.out.flush();
this.out1.flush();
String serverWord = this.in.readLine();
this.resultTextField.setText(serverWord);
} catch (IOException var4) {
var4.printStackTrace();
}
}
});
});
}
} |
|
sample.fxml (настройка окна)
| Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| <?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<!-- Задаем настройки окна приложения,размеры,цвет,тектсы и т.д. -->
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="476.0" prefWidth="684.0" style="-fx-background-color: #FFFFFF;" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<children>
<Label alignment="CENTER" layoutX="209.0" layoutY="14.0" prefHeight="40.0" prefWidth="267.0" text="Клиент">
<font>
<Font name="Times New Roman Bold" size="37.0" />
</font>
</Label>
<Button fx:id="connectButton" ellipsisString="" layoutX="416.0" layoutY="86.0" mnemonicParsing="false" prefHeight="84.0" prefWidth="216.0" text="Подключить">
<font>
<Font name="Times New Roman" size="21.0" />
</font>
</Button>
<Button fx:id="sendButton" layoutX="416.0" layoutY="218.0" mnemonicParsing="false" prefHeight="84.0" prefWidth="216.0" text="Подсчитать НОД">
<font>
<Font name="Times New Roman" size="21.0" />
</font>
</Button>
<TextField fx:id="IPTextfield" alignment="CENTER" layoutX="185.0" layoutY="100.0" text="127.0.0.1" />
<Label layoutX="43.0" layoutY="97.0" prefHeight="31.0" prefWidth="108.0" text="IP адрес">
<font>
<Font name="Times New Roman" size="18.0" />
</font>
</Label>
<Label alignment="BOTTOM_LEFT" layoutX="43.0" layoutY="136.0" prefHeight="31.0" prefWidth="55.0" text="Порт">
<font>
<Font name="Times New Roman" size="18.0" />
</font>
</Label>
<TextField fx:id="PortTextfield" alignment="CENTER" layoutX="185.0" layoutY="139.0" prefHeight="25.0" prefWidth="149.0" text="4004" />
<TextField fx:id="sendTextField" alignment="CENTER" layoutX="185.0" layoutY="224.0" prefHeight="25.0" prefWidth="149.0" />
<TextField fx:id="resultTextField" alignment="CENTER" layoutX="416.0" layoutY="344.0" prefHeight="84.0" prefWidth="216.0" />
<Label layoutX="242.0" layoutY="344.0" prefHeight="31.0" prefWidth="99.0" text="Результат" textAlignment="CENTER">
<font>
<Font name="Times New Roman Bold" size="20.0" />
</font>
</Label>
<Label layoutX="43.0" layoutY="224.0" prefHeight="25.0" prefWidth="128.0" text="Введите число a" textAlignment="CENTER">
<font>
<Font name="Times New Roman" size="18.0" />
</font>
</Label>
<TextField fx:id="sendTextField1" alignment="CENTER" layoutX="185.0" layoutY="265.0" prefHeight="25.0" prefWidth="149.0" />
<Label layoutX="43.0" layoutY="265.0" prefHeight="25.0" prefWidth="128.0" text="Введите число b" textAlignment="CENTER">
<font>
<Font name="Times New Roman" size="18.0" />
</font>
</Label>
</children>
</AnchorPane> |
|