79 lines
3.3 KiB
Java
79 lines
3.3 KiB
Java
/*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
package server;
|
|
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.io.PipedInputStream;
|
|
import java.io.PipedOutputStream;
|
|
import java.net.ServerSocket;
|
|
import java.net.Socket;
|
|
import java.util.logging.Level;
|
|
import java.util.logging.Logger;
|
|
import utils.Client;
|
|
|
|
/**
|
|
* Hauptklasse des Servers
|
|
*
|
|
* @version 1.0.0
|
|
* @author eichehome
|
|
*/
|
|
public class MainServer {
|
|
|
|
/**
|
|
* Einstiegsfunktion des Servers, welche auf Anfragen der Clients wartet und dann die nötigen Resourcen (Streams und Threads) erstellt.
|
|
* @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();//Hier bleibt der server stehen und geht erst weiter wenn ein Client sich verbinden will.
|
|
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 threadSend = new ClientSendMessageThread(out, pipedObjectInputStream, clientStore);
|
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads erstellt"));
|
|
|
|
threadSend.start();
|
|
threadRecive.start();
|
|
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.INFO, String.format("Main: Threads gestartet"));
|
|
|
|
clientStore.addClient(new Client(pipedObjectOutputStream, threadSend, threadRecive));
|
|
|
|
}
|
|
} catch (Exception ex) {
|
|
Logger.getLogger(MainServer.class.getName()).log(Level.SEVERE, String.format("Main: Fehler: %s", ex));
|
|
}
|
|
}
|
|
|
|
}
|