|
@@ -0,0 +1,50 @@
|
|
|
+package com.company;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.Socket;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Scanner;
|
|
|
+
|
|
|
+public class Client extends Thread {
|
|
|
+ private Socket socket;
|
|
|
+ private InputStream inputStream;
|
|
|
+ private OutputStream outputStream;
|
|
|
+ // Список всех подключенных слиентов
|
|
|
+ private List<Client> clientList;
|
|
|
+
|
|
|
+ public static int READ_BUFFER_SIZE = 1024;
|
|
|
+
|
|
|
+ public Client(Socket socket, List<Client> clientList) throws IOException {
|
|
|
+ this.socket = socket;
|
|
|
+ this.clientList = clientList;
|
|
|
+
|
|
|
+ inputStream = socket.getInputStream();
|
|
|
+ outputStream = socket.getOutputStream();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ byte []buffer = new byte[READ_BUFFER_SIZE];
|
|
|
+ while (!isInterrupted()) {
|
|
|
+ try {
|
|
|
+ inputStream.read(buffer);
|
|
|
+
|
|
|
+ for (Client client : clientList) {
|
|
|
+ if (client != this) {
|
|
|
+ client.outputStream.write(buffer);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ System.out.print("Клиент " + socket.getLocalAddress().getHostAddress() + " ");
|
|
|
+ System.out.println(new String(buffer, StandardCharsets.UTF_8));
|
|
|
+
|
|
|
+ } catch (IOException exception) {
|
|
|
+ System.out.println("Не смог прочитать данные клиента " + socket.getLocalAddress().getHostAddress());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|