-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerClientApp.java
More file actions
199 lines (161 loc) · 5.87 KB
/
Copy pathServerClientApp.java
File metadata and controls
199 lines (161 loc) · 5.87 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.mathslove.server_client;
import java.io.*;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import static com.mathslove.server_client.ServerClientApp.Fibonacci;
class Server{
private final ServerSocket serverSocket;
public static Server create(String host, int port) throws IOException {
return new Server(new ServerSocket(port, 100, InetAddress.getByName(host)));
}
private Server(ServerSocket ss){
serverSocket = ss;
}
public void start() throws IOException {
try {
while (true){
Socket socket = serverSocket.accept();
try {
var cl = new ClientHandler(socket);
System.out.println("Client connected " + socket.getRemoteSocketAddress().toString());
cl.start();
} catch (IOException e){
socket.close();
System.err.println("Client connection was failed" + socket.toString());
e.printStackTrace(System.err);
}
}
} finally {
serverSocket.close();
}
}
public void stop() throws IOException {
serverSocket.close();
}
}
class ClientHandler extends Thread{
private final Socket clientSocket;
private final PrintWriter out;
private final BufferedReader in;
public ClientHandler(Socket socket) throws IOException {
clientSocket = socket;
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(clientSocket.getOutputStream())), true);
}
@Override
public void run() {
try {
String line;
while ((line = in.readLine()) != null) {
try {
var n = Integer.parseInt(line);
out.println(Fibonacci(n));
} catch (NumberFormatException e) {
out.println("You send a wrong formation");
}
}
} catch (IOException e){
e.printStackTrace();
this.closeHandler();
}
finally {
System.out.println("Client disconnected " + clientSocket.getRemoteSocketAddress().toString());
this.closeHandler();
}
}
private void closeHandler() {
try {
if(!clientSocket.isClosed()) {
clientSocket.close();
in.close();
out.close();
this.interrupt();
}
} catch (IOException e) {
e.printStackTrace();
this.interrupt();
}
}
}
class Client {
private final Socket clientSocket;
private final PrintWriter out;
private final BufferedReader in;
public Client(String ip, int port) throws IOException {
clientSocket = new Socket(ip, port);
out = new PrintWriter(new BufferedWriter( new OutputStreamWriter(clientSocket.getOutputStream())), true);
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
public String getFibonacciNum(String msg) throws IOException {
out.println(msg);
return in.readLine();
}
public void stopConnection() throws IOException {
try {
if (!clientSocket.isClosed()) {
clientSocket.close();
in.close();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class ServerClientApp {
// Метод Фиббоначи
// 0,1,1,2,3,5,8,...
static BigInteger Fibonacci(Integer n){
BigInteger a = BigInteger.valueOf(0);
BigInteger b = BigInteger.valueOf(1);
for (int i = 1; i <= n; i++) {
var tmp_a = a;
a = a.add(b);
b = tmp_a;
}
return a;
}
// -<s or c> --host <ip> --port <number>
static final int TEMPLATE_NUM_OF_ARGS = 5;
public static void main(String[] args) {
try {
if (args.length != TEMPLATE_NUM_OF_ARGS) {
throw new IllegalArgumentException();
}
if (args[0].equals("-s") && args[1].equals("--host") && args[3].equals("--port")) {
try {
var s = Server.create(args[2], Integer.parseInt(args[4]));
s.start();
} catch (IOException e) {
e.printStackTrace();
}
} else if (args[0].equals("-c") && args[1].equals("--host") && args[3].equals("--port")) {
try {
var client = new Client(args[2], Integer.parseInt(args[4]));
var scanner = new Scanner(System.in);
while (true){
var line = scanner.nextLine();
if (line.equals("")){
client.stopConnection();
break;
}
System.out.println(client.getFibonacciNum(line));
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e) {
System.out.println("""
Please choose the option:
\t-s --host <ip> --port <number> : start a server
\t-c --host <ip> --port <number> : start a client
""");
}
}
}