Ryuzaki13 3 years ago
commit
d8aca15aa8

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Datasource local storage ignored files
+/../../../../../../:\Users\alexey\IdeaProjects\Server\.idea/dataSources/
+/dataSources.local.xml
+# Editor-based HTTP Client requests
+/httpRequests/

+ 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>

+ 9 - 0
.idea/misc.xml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectKey">
+    <option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="15" 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$/Server.iml" filepath="$PROJECT_DIR$/Server.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>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 11 - 0
Server.iml

@@ -0,0 +1,11 @@
+<?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>

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

@@ -0,0 +1,68 @@
+package com.company;
+
+import java.io.InputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.List;
+
+public class Client extends Thread {
+
+    private List<Client> clientList;
+    private Socket socket;
+    private String name;
+
+    public Client(Socket socket, List<Client> clientList) {
+        this.socket = socket;
+        this.clientList = clientList;
+
+        clientList.add(this);
+    }
+
+    @Override
+    public void run() {
+
+        try {
+            OutputStream os = socket.getOutputStream();
+            ObjectOutputStream oos = new ObjectOutputStream(os);
+
+            InputStream is = socket.getInputStream();
+            ObjectInputStream ois = new ObjectInputStream(is);
+
+            Object object = ois.readObject();
+
+            if (object instanceof String) {
+                name = (String) object;
+                System.out.println(
+                        socket.getInetAddress().getHostAddress() +
+                                " " + name);
+
+                oos.writeObject("Ok");
+                oos.flush();
+            }
+
+            while (!isInterrupted()) {
+                object = ois.readObject();
+                if (object instanceof String) {
+                    for (Client c : clientList) {
+                        if (c == this) continue;
+
+                        if (c.socket.isConnected()) {
+                            ObjectOutputStream o = new ObjectOutputStream(
+                                    c.socket.getOutputStream()
+                            );
+                            o.writeObject(name + " | " + object);
+                            o.flush();
+                        }
+                    }
+                }
+            }
+
+            clientList.remove(this);
+            socket.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

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

@@ -0,0 +1,24 @@
+package com.company;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Main {
+
+    public static void main(String[] args) throws Exception {
+
+        List<Client> clientList = new ArrayList<>();
+
+        ServerSocket serverSocket = new ServerSocket(8888);
+
+        while (true) {
+            Socket clientSocket = serverSocket.accept();
+
+            Client client = new Client(clientSocket, clientList);
+            client.start();
+        }
+    }
+}