Загрузил polozyasha

4 лаба ост

Реклама
Министерство образования Республики Беларуси
Белорусский государственный университет
информатики и радиоэлектроники
ОТЧЕТ
по теме
«Создание параллельного многопроцессного сервера с установлением
логического соединения ТСР »
Проверил
Котковец Г.П.
Выполнила студентка гр. 073903
Минск 2022
Полоз А. А.
8. Сервер хранит два одномерных массива строк фиксированной длины.
Клиент имеет возможность обращения к отдельным строкам массивов по
индексам, выполнения операций поэлементного сцепления двух массивов с
образованием нового массива, слияния двух массивов с исключением
повторяющихся элементов, вывода на экран элемента массива по заданному
индексу и всех массивов, редактирования массивов. Программа клиента
должна содержать меню, позволяющее осуществлять указанные действия на
сервере.
Код:
package Sample;
import
import
import
import
import
import
import
import
java.io.DataInputStream;
java.io.DataOutputStream;
java.io.IOException;
java.net.Socket;
java.util.ArrayList;
java.util.Arrays;
java.util.Collections;
java.util.List;
import static java.lang.Integer.getInteger;
import static java.lang.Integer.parseInt;
public class ClientConnection implements Runnable {
private Socket clientSocket;
ClientConnection(Socket clientSocket) {
this.clientSocket = clientSocket;
}
@Override
public void run() {
try {
DataInputStream inputStream = new
DataInputStream(clientSocket.getInputStream());
DataOutputStream outputStream = new
DataOutputStream(clientSocket.getOutputStream());
String[] client1 = {"1", "Vait", "TV", "1500"};
String[] client2 = {"2", "Bardugo", "Coffee machine", "2300"};
String[] client3 = {"3", "Mars", "Laptop", "3000"};
String[] client4 = {"4", "Lues", "Washing machine", "900"};
String[] client5 = {"5", "Gerbet", "Tires", "800"};
ArrayList<String> array = new ArrayList<>();
boolean flag = false;
while (true) {
String s = (inputStream.readUTF());
if (s.equals("1")){
String response = Arrays.toString(client1);
String response1 = Arrays.toString(client2);
String response2 = Arrays.toString(client3);
String response3 = Arrays.toString(client4);
String response4 = Arrays.toString(client5);
String all = response + "\n" + response1 + "\n" +
response2 + "\n" + response3 + "\n" + response4 + "\n";
if (flag){
all = response + "\n" + response1 + "\n" + response2
+ "\n" + response3 + "\n" + response4 + "\n" + array + "\n";
}
outputStream.writeUTF(all);
}
else if (s.equals("2")){
flag = true;
array.add("6");
String s1 = (inputStream.readUTF());
array.add(s1);
String s2 = (inputStream.readUTF());
array.add(s2);
String s3 = (inputStream.readUTF());
array.add(s3);
outputStream.writeUTF(String.valueOf(array));
}
else if (s.equals("3")){
String s1 = (inputStream.readUTF());
if (s1.equals("1")) {
String s2 = (inputStream.readUTF());
for (int i = 0; i < client1.length; i++){
if (s2.equals(Integer.toString(i + 1))) {
String s3 = (inputStream.readUTF());
client1[i] = s3;
String response = Arrays.toString(client1);
outputStream.writeUTF(response);
}
}
}
else if (s1.equals("2")) {
String s2 = (inputStream.readUTF());
for (int i = 0; i < client2.length; i++){
if (s2.equals(Integer.toString(i + 1))) {
String s3 = (inputStream.readUTF());
client2[i] = s3;
String response = Arrays.toString(client2);
outputStream.writeUTF(response);
}
}
}
else if (s1.equals("3")) {
String s2 = (inputStream.readUTF());
for (int i = 0; i < client3.length; i++){
if (s2.equals(Integer.toString(i + 1))) {
String s3 = (inputStream.readUTF());
client3[i] = s3;
String response = Arrays.toString(client3);
outputStream.writeUTF(response);
}
}
}
else if (s1.equals("4")) {
String s2 = (inputStream.readUTF());
for (int i = 0; i < client4.length; i++){
if (s2.equals(Integer.toString(i + 1))) {
String s3 = (inputStream.readUTF());
client4[i] = s3;
String response = Arrays.toString(client4);
outputStream.writeUTF(response);
}
}
}
else if (s1.equals("5")) {
String s2 = (inputStream.readUTF());
for (int i = 0; i < client5.length; i++){
if (s2.equals(Integer.toString(i + 1))) {
String s3 = (inputStream.readUTF());
client5[i] = s3;
String response = Arrays.toString(client5);
outputStream.writeUTF(response);
}
}
}
else if (s1.equals("6")) {
String s2 = (inputStream.readUTF());
for (int i = 0; i < 6; i++){
if (s2.equals(Integer.toString(i + 1))) {
String s3 = (inputStream.readUTF());
array.set(i, s3);
outputStream.writeUTF(String.valueOf(array));
}
}
}
}
else if (s.equals("4")){
String s1 = (inputStream.readUTF());
if (s1.equals("1")) {
String s2 = (inputStream.readUTF());
Integer i = Integer.valueOf(s2);
i -= 1;
client1[i] = null;
String response = Arrays.toString(client1);
outputStream.writeUTF(response);
}
else if (s1.equals("2")) {
String s2 = (inputStream.readUTF());
Integer i = Integer.valueOf(s2);
i -= 1;
client2[i] = null;
String response = Arrays.toString(client2);
outputStream.writeUTF(response);
}
else if (s1.equals("3")) {
String s2 = (inputStream.readUTF());
Integer i = Integer.valueOf(s2);
i -= 1;
client3[i] = null;
String response = Arrays.toString(client3);
outputStream.writeUTF(response);
}
else if (s1.equals("4")) {
String s2 = (inputStream.readUTF());
Integer i = Integer.valueOf(s2);
i -= 1;
client4[i] = null;
String response = Arrays.toString(client4);
outputStream.writeUTF(response);
}
else if (s1.equals("5")) {
String s2 = (inputStream.readUTF());
Integer i = Integer.valueOf(s2);
i -= 1;
client5[i] = null;
String response = Arrays.toString(client5);
outputStream.writeUTF(response);
}
else if (s1.equals("6")) {
String s2 = (inputStream.readUTF());
Integer i = Integer.valueOf(s2);
i -= 1;
array.remove(array.get(i));
String response = String.valueOf(array);
outputStream.writeUTF(response);
}
}
else if (s.equals("5")){
String s1 = (inputStream.readUTF());
if (s1.equals("1")) {
List<Integer> numList = new ArrayList<Integer>();
numList.add(parseInt(client1[0]));
numList.add(parseInt(client2[0]));
numList.add(parseInt(client3[0]));
numList.add(parseInt(client4[0]));
numList.add(parseInt(client5[0]));
numList.add(parseInt(array.get(0)));
Collections.sort(numList);
String response = String.valueOf(numList);
outputStream.writeUTF(response);
}
else if (s1.equals("2")){
List<String> strList = new ArrayList<String>();
strList.add(client1[1]);
strList.add(client2[1]);
strList.add(client3[1]);
strList.add(client4[1]);
strList.add(client5[1]);
strList.add(array.get(1));
Collections.sort(strList);
String response = String.valueOf(strList);
outputStream.writeUTF(response);
}
else if (s1.equals("3")){
List<String> strList = new ArrayList<String>();
strList.add(client1[2]);
strList.add(client2[2]);
strList.add(client3[2]);
strList.add(client4[2]);
strList.add(client5[2]);
strList.add(array.get(2));
Collections.sort(strList);
String response = String.valueOf(strList);
outputStream.writeUTF(response);
}
else if (s1.equals("4")){
List<Integer> numList = new ArrayList<Integer>();
numList.add(parseInt(client1[3]));
numList.add(parseInt(client2[3]));
numList.add(parseInt(client3[3]));
numList.add(parseInt(client4[3]));
numList.add(parseInt(client5[3]));
numList.add(parseInt(array.get(3)));
Collections.sort(numList);
String response = String.valueOf(numList);
outputStream.writeUTF(response);
}
}
else if (s.equals("6")){
String s1 = (inputStream.readUTF());
if (s1.equals("1")) {
String s2 = (inputStream.readUTF());
if (s2.equals(client1[0])){
String response = Arrays.toString(client1);
outputStream.writeUTF(response);
}
else if (s2.equals(client2[0])){
String response = Arrays.toString(client2);
outputStream.writeUTF(response);
}
else if (s2.equals(client3[0])){
String response = Arrays.toString(client3);
outputStream.writeUTF(response);
}
else if (s2.equals(client4[0])){
String response = Arrays.toString(client4);
outputStream.writeUTF(response);
}
else if (s2.equals(client5[0])){
String response = Arrays.toString(client5);
outputStream.writeUTF(response);
}
else if (s2.equals(array.get(0))){
String response = String.valueOf(array);
outputStream.writeUTF(response);
}
}
else if (s1.equals("2")){
String s2 = (inputStream.readUTF());
if (s2.equals(client1[1])){
String response = Arrays.toString(client1);
outputStream.writeUTF(response);
}
else if (s2.equals(client2[1])){
String response = Arrays.toString(client2);
outputStream.writeUTF(response);
}
else if (s2.equals(client3[1])){
String response = Arrays.toString(client3);
outputStream.writeUTF(response);
}
else if (s2.equals(client4[1])){
String response = Arrays.toString(client4);
outputStream.writeUTF(response);
}
else if (s2.equals(client5[1])){
String response = Arrays.toString(client5);
outputStream.writeUTF(response);
}
else if (s2.equals(array.get(1))) {
String response = String.valueOf(array);
outputStream.writeUTF(response);
}
}
else if (s1.equals("3")){
String s2 = (inputStream.readUTF());
if (s2.equals(client1[2])){
String response = Arrays.toString(client1);
outputStream.writeUTF(response);
}
else if (s2.equals(client2[2])){
String response = Arrays.toString(client2);
outputStream.writeUTF(response);
}
else if (s2.equals(client3[2])){
String response = Arrays.toString(client3);
outputStream.writeUTF(response);
}
else if (s2.equals(client4[2])){
String response = Arrays.toString(client4);
outputStream.writeUTF(response);
}
else if (s2.equals(client5[2])){
String response = Arrays.toString(client5);
outputStream.writeUTF(response);
}
else if (s2.equals(array.get(2))) {
String response = String.valueOf(array);
outputStream.writeUTF(response);
}
}
else if (s1.equals("4")){
String s2 = (inputStream.readUTF());
if (s2.equals(client1[3])){
String response = Arrays.toString(client1);
outputStream.writeUTF(response);
}
else if (s2.equals(client2[3])){
String response = Arrays.toString(client2);
outputStream.writeUTF(response);
}
else if (s2.equals(client3[3])){
String response = Arrays.toString(client3);
outputStream.writeUTF(response);
}
else if (s2.equals(client4[3])){
String response = Arrays.toString(client4);
outputStream.writeUTF(response);
}
else if (s2.equals(client5[3])){
String response = Arrays.toString(client5);
outputStream.writeUTF(response);
}
else if (s2.equals(array.get(3))) {
String response = String.valueOf(array);
outputStream.writeUTF(response);
}
}
}
}
} catch (IOException e) {
System.out.println("Connection adorted by client");
}
}
}
package Sample;
import java.net.ServerSocket;
import java.net.Socket;
public class Main {
public static void main(String[] args) {
Sample.Server server = new Sample.Server();
server.start();
}
}
package Sample;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
private ServerSocket socket;
public void start() {
try {
this.socket = new ServerSocket(8050);
System.out.println("Server has been started on localhost:8050");
} catch (Exception e) {
System.out.println("Can't host server on localhost:8050 " +
e.getLocalizedMessage());
}
while (true) {
try {
Socket clientSocket = socket.accept();
System.out.println("Client has connected: " +
clientSocket.getInetAddress().toString());
ClientConnection clientConnection = new
ClientConnection(clientSocket);
Thread clientThread = new Thread(clientConnection);
clientThread.start();
} catch (Exception e) {
System.out.println("Something went wrong: " +
e.getLocalizedMessage());
}
}
}
}
package sample;
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
try (
Socket socket = new Socket("localhost", 8050);
DataInputStream inputStream = new
DataInputStream(socket.getInputStream());
DataOutputStream outputStream = new
DataOutputStream(socket.getOutputStream());
)
{
System.out.println("Connected to server");
while (true) {
Scanner in = new Scanner(System.in);
System.out.print("number:(1 - view; 2 - add; 3 - editing; 4 delete; 5 - sort; 6 - search): ");
String request = in.nextLine();
outputStream.writeUTF(request);
if (request.equals("2")) {
System.out.print("name client: ");
String name = in.nextLine();
outputStream.writeUTF(name);
System.out.print("order: ");
String nazv = in.nextLine();
outputStream.writeUTF(nazv);
System.out.print("cost: ");
String year = in.nextLine();
outputStream.writeUTF(year);
}
if (request.equals("3")) {
System.out.print("enter number client: ");
String name = in.nextLine();
outputStream.writeUTF(name);
System.out.print("editing?: (1 - number client; 2 - name;
3 - наименование order; 4 - cost): ");
String nom = in.nextLine();
outputStream.writeUTF(nom);
System.out.print("enter new data: ");
String dan = in.nextLine();
outputStream.writeUTF(dan);
}
if (request.equals("4")) {
System.out.print("enter data for removing: ");
String name = in.nextLine();
outputStream.writeUTF(name);
System.out.print("delete?: (1 - number client; 2 - name;
3 - наименование order; 4 - cost): ");
String nom = in.nextLine();
outputStream.writeUTF(nom);
}
if (request.equals("5")) {
System.out.print("by what criterion and sort?(1 - number
client; 2 - name; 3 - наименование order; 4 - cost): ");
String nom = in.nextLine();
outputStream.writeUTF(nom);
}
if (request.equals("6")){
System.out.println("by what criterion and search?: (1 number client; 2 - name; 3 - наименование order; 4 - cost):");
String nom = in.nextLine();
outputStream.writeUTF(nom);
System.out.println("enter data: ");
String dan = in.nextLine();
outputStream.writeUTF(dan);
}
String response = inputStream.readUTF();
System.out.println(response);
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
Пример запуска программы:
Скачать