/* * Copyright (C) 2021 eichehome * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package server; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.*; import java.util.logging.Level; import java.util.logging.Logger; import utils.Client; /** * Hauptklasse des Servers * * @version 0.0.1 * @author eichehome */ public class MainServer { /** * Einstiegsklasse des Servers. Diese wartet auf Anfragen der Clients und erstellt dann die nötigen Resourcen (Streams und Threads). * @param args the command line arguments */ public static void main(String[] args) { try (ServerSocket serverSocket = new ServerSocket(1236)) { ClientStore clientStore = new ClientStore(); while (true) { Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Warte auf Clients")); Socket client = serverSocket.accept(); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Client verbunden")); PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream); ObjectOutputStream pipedObjectOutputStream = new ObjectOutputStream(pipedOutputStream); ObjectInputStream pipedObjectInputStream = new ObjectInputStream(pipedInputStream); ObjectInputStream in = new ObjectInputStream(client.getInputStream()); ObjectOutputStream out = new ObjectOutputStream(client.getOutputStream()); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Streams erstellt")); Thread threadRecive = new ClientReciveMessageThread(in, clientStore); Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads erstellt")); threadPush.start(); threadRecive.start(); Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads gestartet")); clientStore.addClient(new Client(pipedObjectOutputStream, threadPush, threadRecive)); } } catch (Exception ex) { Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, String.format("Main: Fehler: %s", ex)); } } }