Chat-Applikation/src/server/ServerHandelClientsThread.java

79 lines
2.9 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.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.*;
import utils.Message;
/**
*
* @author eichehome
*/
public class ServerHandelClientsThread extends Thread {
private Socket clientSocket = null;
private ClientMessageStore centralMessageStore = null;
private PipedReader pipe = null;
@Override
public void run() {
boolean continueLoop = true;
while (continueLoop) {
try (ObjectInputStream in = new ObjectInputStream(clientSocket.getInputStream());
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream())) {
Message empfangen = null;
while ((empfangen = (Message) in.readObject()) != null) {
System.out.println("client: " + empfangen);
Message message = empfangen;
out.writeObject(message);
out.flush();
}
} catch (ClassNotFoundException ex) {
System.err.println(ex);
//for(String clientInput = in.readLine(); clientInput != null; clientInput = in.readLine()) {
//}
} catch (InterruptedException ex) {
System.err.println("test gegkückt");
System.err.println(ex);
pr.write("Test");
Thread.currentThread().interrupt();
} catch (SocketException ex) {
System.err.println("Socket geschlossen");
centralMessageStore.removeClient(this);
continueLoop = false;
} catch (EOFException ex) {
System.err.println("Fehler: " + ex);
continueLoop = false;
} catch (IOException ex) {
System.err.println("Exeption: " + ex);
Logger.getLogger(ServerHandelClientsThread.class.getName()).log(Level.SEVERE, null, ex);
centralMessageStore.removeClient(this);
continueLoop = false;
}
}
}
public ServerHandelClientsThread(Socket socket, ClientMessageStore messageStore, PipedReader pipe) {
this.clientSocket = socket;
this.centralMessageStore = messageStore;
this.pipe = pipe;
}
}