Alec 3 years ago
commit
3fee5f8e4a

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

+ 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_14" default="true" project-jdk-name="1.8" 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$/MyServer.iml" filepath="$PROJECT_DIR$/MyServer.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>

+ 12 - 0
MyServer.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/MyServer/com/company/Client.class


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


BIN
out/production/MyServer/com/company/Server.class


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

@@ -0,0 +1,54 @@
+package com.company;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.List;
+
+public class Client extends Thread {
+
+    private Socket socket;
+    private List<Client> clientList;
+
+    public Client(Socket socket, List<Client> clientList) {
+        this.socket = socket;
+        this.clientList = clientList;
+    }
+
+    @Override
+    public void run() {
+
+        byte[] data = new byte[2048];
+
+        while (!isInterrupted()) {
+
+            try {
+                InputStream is = socket.getInputStream();
+
+                is.read(data);
+
+                System.out.println(new String(data));
+
+                for (Client client : clientList) {
+                    // Отправить сообщение
+
+                    if (client.socket != this.socket) {
+                        OutputStream os = client.socket.getOutputStream();
+
+                        os.write(data);
+                        os.flush();
+                    }
+                }
+
+            } catch (IOException exception) {
+//                exception.printStackTrace();
+
+                System.out.println(socket.getInetAddress().getHostAddress() + " disconnected");
+
+                clientList.remove(this);
+                interrupt();
+            }
+        }
+    }
+}

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

@@ -0,0 +1,18 @@
+package com.company;
+
+import java.io.IOException;
+
+public class Main {
+
+    public static void main(String[] args) {
+
+        Server server = new Server();
+
+        try {
+            server.Run();
+        } catch (IOException exception) {
+            exception.printStackTrace();
+        }
+
+    }
+}

+ 32 - 0
src/com/company/Server.java

@@ -0,0 +1,32 @@
+package com.company;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.ArrayList;
+import java.util.List;
+
+public class Server {
+    ServerSocket serverSocket;
+    List<Client> clientList = new ArrayList<>();
+
+    public void Run() throws IOException {
+
+        serverSocket = new ServerSocket();
+        serverSocket.bind(new InetSocketAddress("192.168.0.2", 3243));
+
+        while (true) {
+            System.out.println("Server is reading");
+            Socket socket = serverSocket.accept();
+
+            System.out.println(socket.getInetAddress().getHostAddress() + " connected");
+
+            Client client = new Client(socket, clientList);
+            client.start();
+
+            clientList.add(client);
+        }
+
+    }
+}