Chat-Applikation/src/server/MainServer.java

83 lines
3.1 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.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PipedReader;
import java.io.PipedWriter;
import java.lang.invoke.MethodHandles;
import java.net.*;
import utils.Client;
import utils.FifoPipe;
/**
* Hauptklasse des Servers
*
* @version 0.0.1
* @author eichehome
*/
public class MainServer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
boolean continueLoop = true;
try (ServerSocket serverSocket = new ServerSocket(1236)) {
ClientStore clientStore = new ClientStore();
while (continueLoop) {
System.out.println("Warte auf Clients");
Socket client = serverSocket.accept();
System.out.println("client connected");
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());
System.out.println("Streams created");
Thread threadRecive = new ClientReciveMessageThread(in, clientStore);
Thread threadPush = new ClientPushMessageThread(out, pipedObjectInputStream, clientStore);
System.out.println("Threads created");
threadPush.start();
threadRecive.start();
System.out.println("Threads started");
Client client = new Client(pipedObjectOutputStream, threadPush, threadRecive);
System.out.println(client.getPusher() + "/" + client.getReciver() + "/" + client.getOutputStream());
clientStore.addClient(client);
}
} catch (Exception ex) {
System.err.println("Fehler: " + ex);
}
}
}