Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ru.otus.java.basic.homeworks.homework13.client;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class client {
private final DataInputStream in;
private final PrintStream out;

public static void main(String[] args) {
try(Socket socket = new Socket("localhost", 8888)) {
client client = new client(socket);
String result = client.read();
System.out.print(result);
result = client.read();
System.out.print(result);

System.out.print("Введите математическую операцию: ");

Scanner scanner = new Scanner(System.in);
String userInput = scanner.nextLine();
scanner.close();

client.send(userInput);
result = client.read();
System.out.println(result);
} catch (Exception e) {
System.out.println(e.getMessage());
}

}

public client(Socket socket) throws IOException {
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
out = new PrintStream(socket.getOutputStream());
}

public void send(String message) {
out.println(message);
out.flush();
}

public String read() throws IOException {
return in.readUTF();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ru.otus.java.basic.homeworks.homework13.server;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;

public class server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8888);
Socket client = serverSocket.accept();
System.out.println("Client connected");
printStartInfo(client);
while(processRequest(client)) {}
}

public static void printStartInfo(Socket client) throws IOException {
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
out.writeUTF("available math operations: +,-,*,/ enter exit for exit\n");
out.writeUTF("example: 1 + 2\n");
out.flush();
}

public static boolean processRequest(Socket client) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String userInput = in.readLine();
if (Objects.equals(userInput, "exit")) {
return false;
}
System.out.println("Original: " + userInput);
String result = makeMath(userInput);
System.out.println("Result: " + result);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(client.getOutputStream()));
out.writeUTF(result);
out.flush();
return true;
}

private static String makeMath(String expression) {
String[] parts = expression.split(" ");
if (parts.length != 3) {
return "input error\n";
}
try {
int n1 = Integer.parseInt(parts[0]);
String operation = parts[1];
int n2 = Integer.parseInt(parts[2]);

if (Objects.equals(operation, "+")) {
return (n1 + n2) + "\n";
}
if (Objects.equals(operation, "-")) {
return (n1 - n2) + "\n";
}
if (Objects.equals(operation, "*")) {
return (n1 * n2) + "\n";
}
if (Objects.equals(operation, "/")) {
return (n1 / n2) + "\n";
}
return "input error\n";
} catch (NumberFormatException e) {
System.out.println(e.getMessage());
}

return "input error\n";
}
}