# Textchat Funktioniert nur halb



## MrXeth (8. Mai 2018)

moin, mein Textchat funktioniert nur halb, also, wenn ich einen Client sich in den Server einwählen lasse, funktioniert das ganze bei der ersten Nachricht, aber dann nicht mehr.


```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

public class Server {
   
    private int port;
    private ServerSocket server;
    private ArrayList<PrintWriter> clientWriterList;
   
    public Server() {
        this(5555);
    }
    public Server(int port) {
        this.port = port;
    }

    public static void main(String[] args) {
        Server s = new Server();
        if(s.runServer()) {
            s.listenToClients();
        } else {
            System.out.println("Fehler!");
        }
    }
    private class Handler implements Runnable {
       
        Socket client;
        BufferedReader reader;
       
        public Handler(Socket client) {
            try {
                this.client = client;
                reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        @Override
        public void run() {
            String message;
            try {
                while((message = reader.readLine()) != null) {
                    System.out.println(message);
                    sendToAllClients(message);
                    message ="";
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private boolean runServer() {
        try {
            server = new ServerSocket(port);
            System.out.println("Server wurde gestartet!");
            clientWriterList = new ArrayList<PrintWriter>();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }
    private void listenToClients() {
        while(true) {
            try {
                Socket client = server.accept();
                PrintWriter writer = new PrintWriter(client.getOutputStream());
                clientWriterList.add(writer);
                Thread clientThread = new Thread(new Handler(client));
                clientThread.start();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    private void sendToAllClients(String message) {
        Iterator it = clientWriterList.iterator();
       
        while(it.hasNext()) {
            PrintWriter writer = (PrintWriter) it.next();
            writer.println(message);
            writer.flush();
               
        }
    }
}
```


```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    private String address;
    private int port;
    private Socket client;
    private PrintWriter writer;
    private BufferedReader reader;
   
    public Client() {
        this("localhost", 5555);
    }
   
    public Client(String address, int port) {
        this.address = address;
        this.port = port;
    }
   
    public static void main(String[] args) {
        Client c = new Client();
        c.start();
    }
    public class MessagesFromServerListener implements Runnable {

        @Override
        public void run() {
            String message;
            try {
                while((message = reader.readLine()) != null ) {
                    System.out.println(message);
                }
            } catch (IOException e) {
                System.out.println("Nachricht konnte nicht empfangen werden");
                e.printStackTrace();
            }
        }
       
    }
    private void start() {
        if(!connectedToServer()) {
            System.out.println("Nicht verbunden!");
        }
        Scanner sc = new Scanner(System.in);
        sendMessageToServer(new Scanner(System.in).nextLine());
       
        Thread t = new Thread(new MessagesFromServerListener());
        t.start();
    }

    private boolean connectedToServer() {
        try {
            client = new Socket(address, port);
            reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
            writer = new PrintWriter(client.getOutputStream());
            System.out.println("Verbindung hergestellt");
            return true;
        } catch (IOException e) {
            System.out.println("Verbindung fehlgeschlagen");
            e.printStackTrace();
            return false;
        }
    }
   
    private void sendMessageToServer(String message) {
        writer.println(message);
        writer.flush();
    }
}
```
Danke für eure hilfe


----------



## fhoffmann (9. Mai 2018)

MrXeth hat gesagt.:


> sendMessageToServer(*new* Scanner(System.in).nextLine());


Das rufst du ja auch nur einmal auf. Der Client kann also keine zweite Nachricht schicken.


----------



## MrXeth (15. Mai 2018)

```
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;

public class server {
    private BufferedReader fromClient;
  
    private ArrayList<Socket> clientList;
    private ServerSocket server;
  
    public server() {
        this(9898);
    }
    public server(int port) {
        try {
            server = new ServerSocket(port);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  
    public void main(String []args) {
        new server().start();
    }
  
    public void start() {
        waitForClients(5);
    }
  
    private void waitForClients(int amount) {
        int i = 0;
        while(i <= 5) {
            Socket client = null;
          
            try {
                client = server.accept();
                clientList.add(client);
                Thread clientThread = new Thread(new messageWriter(client));
                i += 1;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
  
    private void sendToAllClients(String message) {
        Iterator it = clientList.iterator();
      
        while(it.hasNext()) {
            messageWriter client = (messageWriter) it.next();
            client.sendMessage(message);
        }
    }
  
    public void receiverQueue() {
        while(true) {
            for(int i = 0; i < clientList.size(); i++) {
                try {
                    fromClient = new BufferedReader(new InputStreamReader(clientList.get(i).getInputStream()));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                String message = null;
                try {
                    while((message = fromClient.readLine()) == null) {}
                } catch (IOException e) {
                    break;
                }
                sendToAllClients(message);
            }
        }
    }
  
    private class messageWriter implements Runnable {
        private Socket clientSocket;
        private PrintWriter toClient;
      
        public messageWriter(Socket clientSocket) {
          
            this.clientSocket = clientSocket;
            try {
                fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                toClient = new PrintWriter(clientSocket.getOutputStream(), true);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
      
        public void run() {
        }
      
        protected void sendMessage(String message) {
            toClient.println(message);
            toClient.flush();
        }
      

    }
}
```

Funktioniert das ? Die Queue soll bewirken, dass immer nur ein Client senden kann. Habe leider noch keinen Client dazu geschrieben

Danke für antworten


----------

