Ryuzaki13 4 жил өмнө
parent
commit
428081d89b

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


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


+ 10 - 12
src/client/Main.java

@@ -1,26 +1,24 @@
 package client;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import com.company.Transport;
+
+import java.io.*;
 import java.net.Socket;
 import java.nio.charset.StandardCharsets;
 
 public class Main {
     public static void main(String[] args) {
         try {
-            Socket socket = new Socket("127.0.0.1", 6000);
-
-            OutputStream os = socket.getOutputStream();
-            InputStream is = socket.getInputStream();
+            Socket socket = new Socket("192.168.10.81", 9125);
 
-            os.write("Hello".getBytes());
-            os.flush();
+            ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
+            ObjectInputStream is = new ObjectInputStream(socket.getInputStream());
 
-            byte []input = new byte[1024];
-            is.read(input);
+            Transport t = new Transport();
+            t.Name = "Hello";
+            t.Message = "World";
 
-            System.out.println(new String(input, StandardCharsets.UTF_8));
+            os.writeObject(t);
 
         } catch (IOException e) {
             e.printStackTrace();

+ 21 - 15
src/com/company/Client.java

@@ -1,8 +1,7 @@
 package com.company;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import javax.management.Attribute;
+import java.io.*;
 import java.net.Socket;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
@@ -10,8 +9,8 @@ import java.util.Scanner;
 
 public class Client extends Thread {
     private Socket socket;
-    private InputStream inputStream;
-    private OutputStream outputStream;
+    private ObjectInputStream inputStream;
+    private ObjectOutputStream outputStream;
     // Список всех подключенных слиентов
     private List<Client> clientList;
 
@@ -21,30 +20,37 @@ public class Client extends Thread {
         this.socket = socket;
         this.clientList = clientList;
 
-        inputStream = socket.getInputStream();
-        outputStream = socket.getOutputStream();
+        inputStream = new ObjectInputStream(socket.getInputStream());
+        outputStream = new ObjectOutputStream(socket.getOutputStream());
     }
 
     @Override
     public void run() {
-       byte []buffer = new byte[READ_BUFFER_SIZE];
        while (!isInterrupted()) {
+
            try {
-               inputStream.read(buffer);
+               Transport t = (Transport) inputStream.readObject();
 
                for (Client client : clientList) {
-                   if (client != this) {
-                       client.outputStream.write(buffer);
+                   if (client.socket != this.socket) {
+                       client.outputStream.writeObject(t);
+                       client.outputStream.flush();
                    }
                }
 
-               System.out.print("Клиент " + socket.getLocalAddress().getHostAddress() + " ");
-               System.out.println(new String(buffer, StandardCharsets.UTF_8));
+               System.out.print("\033[1;31m\033[40m " + t.Name + ": \u001B[0m ");
+               System.out.println(t.Message);
 
            } catch (IOException exception) {
-               System.out.println("Не смог прочитать данные клиента " + socket.getLocalAddress().getHostAddress());
+               System.out.println(exception.getMessage());
+               clientList.remove(this);
+               break;
+           } catch (ClassNotFoundException exception) {
+               System.out.print("Не смог прочитать данные клиента " + socket.getRemoteSocketAddress().toString() + ": ");
+               System.out.println(exception.getMessage());
+               clientList.remove(this);
+               break;
            }
        }
-
     }
 }

+ 8 - 5
src/com/company/Main.java

@@ -3,7 +3,6 @@ package com.company;
 import java.io.IOException;
 import java.net.ServerSocket;
 import java.net.Socket;
-import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -14,14 +13,18 @@ public class Main {
         List<Client> clients = new ArrayList<>();
 
         try {
-            ServerSocket ss = new ServerSocket(6000);
+            ServerSocket ss = new ServerSocket(9125);
 
             while (true) {
                 Socket clientSocket = ss.accept();
 
-                Client client = new Client(clientSocket, clients);
-                clients.add(client);
-                client.start();
+                try {
+                    Client client = new Client(clientSocket, clients);
+                    clients.add(client);
+                    client.start();
+                } catch (IOException e) {
+                    System.out.println(e.getMessage());
+                }
             }
 
         } catch (IOException e) {

+ 11 - 0
src/com/company/Transport.java

@@ -0,0 +1,11 @@
+package com.company;
+
+import java.io.Serializable;
+
+/**
+ * Структура для передачи данных
+ */
+public class Transport implements Serializable {
+    public String Name;
+    public String Message;
+}