Ryuzaki13 3 years ago
commit
ec3f6ad2cf

+ 3 - 0
.idea/.gitignore

@@ -0,0 +1,3 @@
+# Default ignored files
+/shelf/
+/workspace.xml

+ 1 - 0
.idea/description.html

@@ -0,0 +1 @@
+<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

+ 6 - 0
.idea/encodings.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="Encoding">
+    <file url="PROJECT" charset="UTF-8" />
+  </component>
+</project>

+ 12 - 0
.idea/misc.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="EntryPointsManager">
+    <entry_points version="2.0" />
+  </component>
+  <component name="ProjectKey">
+    <option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_14" default="true" project-jdk-name="14" project-jdk-type="JavaSDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/ClientClient.iml" filepath="$PROJECT_DIR$/ClientClient.iml" />
+    </modules>
+  </component>
+</project>

+ 3 - 0
.idea/project-template.xml

@@ -0,0 +1,3 @@
+<template>
+  <input-field default="com.company">IJ_BASE_PACKAGE</input-field>
+</template>

+ 12 - 0
ClientClient.iml

@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$">
+      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>
+

BIN
out/production/ClientClient/com/company/Client.class


BIN
out/production/ClientClient/com/company/Main.class


BIN
out/production/ClientClient/com/company/OverData.class


BIN
out/production/ClientClient/com/company/ReData.class


+ 79 - 0
src/com/company/Client.java

@@ -0,0 +1,79 @@
+package com.company;
+
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.net.Socket;
+import java.util.Random;
+
+public class Client extends Thread {
+    private final String[] clientAddressList = new String[]{
+            "192.168.10.82",
+            "192.168.10.83",
+            "192.168.10.84",
+            "192.168.10.85",
+            "192.168.10.86",
+            "192.168.10.87",
+            "192.168.10.88",
+            "192.168.10.89",
+            "192.168.10.90",
+            "192.168.10.91",
+    };
+
+    private final char []smiles = new char[] {
+            '☺', '☻', '♥', '♦', '♣', '♠',
+    };
+
+    private final Random random = new Random(System.currentTimeMillis());
+
+    @Override
+    public void run() {
+        for (String address : clientAddressList) {
+            int success = 0;
+            String studentName = "";
+            for (int iteration = 0; iteration < 5; iteration++) {
+                try {
+                    Socket socket = new Socket(address, 9125);
+                    socket.setSoTimeout(2000);
+
+                    System.out.println("Connected to " + address + "... " + (iteration + 1));
+
+                    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
+                    char smile = smiles[random.nextInt(smiles.length)];
+                    dos.writeChar(smile);
+                    dos.flush();
+
+                    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
+                    Object o = ois.readObject();
+
+                    if (o instanceof ReData) {
+                        ReData reData = (ReData) o;
+
+                        studentName = reData.lastName + " " + reData.firstName;
+                        if (reData.firstName.indexOf(smile) != -1) {
+                            success++;
+                        }
+
+                        reData.PrintName();
+                        System.out.print(" хочет оценку " + reData.appraisal);
+                        System.out.println(" по дисциплине " + reData.discipline);
+                    }
+
+                    socket.close();
+                } catch (IOException exception) {
+                    if (exception.getMessage().contains("Read timed out")) {
+                        System.out.println(address + " read timed out... " + (iteration + 1));
+                    } else {
+                        System.out.println(address + " connecting fail... " + (iteration + 1));
+                    }
+                } catch (ClassNotFoundException exception) {
+                    System.out.println(address + " read data not supported... " + (iteration + 1));
+                }
+            }
+
+            if (success == 5) {
+                System.out.println("\033[32m " + studentName + "\033[0m WIN!");
+            }
+        }
+    }
+}

+ 16 - 0
src/com/company/Main.java

@@ -0,0 +1,16 @@
+package com.company;
+
+public class Main {
+
+    public static void main(String[] args) {
+        while (true) {
+            Client client = new Client();
+            client.start();
+            try {
+                client.join();
+            } catch (InterruptedException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+}

+ 8 - 0
src/com/company/OverData.java

@@ -0,0 +1,8 @@
+package com.company;
+
+public class OverData extends ReData {
+    @Override
+    public void PrintName() {
+        System.out.println(lastName + " " + firstName + " хочет оценку " + appraisal + " по дисциплине " + discipline);
+    }
+}

+ 12 - 0
src/com/company/ReData.java

@@ -0,0 +1,12 @@
+package com.company;
+import java.io.Serializable;
+public class ReData implements Serializable {
+    public String firstName;
+    public String lastName;
+    public String discipline;
+    public int appraisal;
+
+    public void PrintName() {
+        System.out.print(lastName + " " + firstName);
+    }
+}