123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package com.company;
- import javax.xml.crypto.Data;
- import java.io.*;
- import java.net.Socket;
- import java.util.List;
- public class Client extends Thread {
- // Сокет текущего клиента
- private Socket socket;
- // Список всех подключенных слиентов
- 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;
- }
- public Socket getSocket() {
- return socket;
- }
- @Override
- public void run() {
- while (!isInterrupted()) {
- try {
- ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
- Transport t = (Transport) ois.readObject();
- for (Client client : clientList) {
- if (client.socket != this.socket) {
- ObjectOutputStream coos = new ObjectOutputStream(client.socket.getOutputStream());
- coos.writeObject(t);
- coos.flush();
- }
- }
- System.out.print("\033[1;31m\033[40m " + t.Name + ": \u001B[0m ");
- System.out.println(t.Message);
- } catch (IOException | ClassNotFoundException exception) {
- System.out.println(exception.getMessage());
- clientList.remove(this);
- System.out.println("Disconnected user " + socket.getRemoteSocketAddress().toString());
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return;
- }
- }
- }
- }
|