Multithread-Client-Server-Chat über Sockets

Server und Client kommunizieren mit meinem eigenen Protokoll, das wie XMPP aussieht. Ich sollte Chatanwendung realisieren. Wenn ein Benutzer einen String schreibt, sollte dieser sofort über den Server an einen anderen Client gesendet werden. Ich habe die Methode sendToAll auf dem Server. Der Benutzer sieht die Nachricht eines anderen Benutzers jedoch nur, wenn er die Eingabetaste drückt.Wie kann der Benutzer Nachrichten empfangen, ohne die Eingabetaste zu drücken?

Das ist also mein Kunde:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.apache.log4j.Logger;

import dataart.practice.protocols.XMLProtocol;

public class Client {
public static final String SERVER_HOST = "localhost";
public static final Integer SERVER_PORT = 4444;
public static final Logger LOG = Logger.getLogger(Client.class);
private static BufferedReader in;
private static PrintWriter out;
private static BufferedReader inu;

public static void main(String[] args) throws IOException {

    System.out.println("Welcome to Client side");
    XMLProtocol protocol = new XMLProtocol();
    Socket fromserver = null;

    fromserver = new Socket(SERVER_HOST, SERVER_PORT);

    in = new BufferedReader(new InputStreamReader(fromserver.getInputStream()));

    out = new PrintWriter(fromserver.getOutputStream(), true);

    inu = new BufferedReader(new InputStreamReader(System.in));

    String fuser, fserver;
    while (true){
        if(in.ready()){//fserver = in.readLine()) != null) {
        System.out.println("asdasdsd");

        fuser = inu.readLine();
        if (fuser != null) {
            if (fuser.equalsIgnoreCase("close"))
                break;
            if (fuser.equalsIgnoreCase("exit"))
                break;

            protocol.setComId((long) 0);
            protocol.setContent(fuser);
            protocol.setLogin("Guest");

            try {

                JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class);
                Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
                jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
                jaxbMarshaller.marshal(protocol, out);
                out.flush();

            } catch (JAXBException e) {
                LOG.error("Error while processing protocol" + e);
            }
        }
        }

    }

    out.close();
    in.close();
    inu.close();
    fromserver.close();
}

}

Und Server mit ServerThread.

public static void main(String[] args) throws IOException {

    LOG.trace("Server started");
    ServerSocket s = new ServerSocket(SERVER_PORT);

    try {
        while (true) {
            LOG.trace("Waiting for connections...");
            Socket socket = s.accept();
            try {
                // new ServerThread(socket);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                userCounter++;
                addUser("Guest" + userCounter, out);
                LOG.trace("User " + userCounter + " has been added!");
                exec.execute(new ServerThread(socket, in, out));

            } catch (IOException e) {
                socket.close();
            }
        }
    } finally {
        s.close();
    }
}

ServerThread.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.net.Socket;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.apache.log4j.Logger;

import dataart.practice.protocols.XMLProtocol;
import dataart.practice.serverUtils.Commands;

public class ServerThread implements Runnable {
    private static final Logger LOG = Logger.getLogger(ServerThread.class);

    private XMLProtocol protocol;
    private Socket socket;
    private BufferedReader in;
    private PrintWriter out;
    private String buffer = "";// may be exist another. way but it's not working
    private Boolean login = false;

    public ServerThread(Socket s, BufferedReader in, PrintWriter out) throws IOException {
        this.in = in;
        this.out = out;
        out.println("</XMLProtocol>");
        socket = s;
        new Thread(this);       
    }

    public void run() {
        try {
            while (true) {              
                if ((buffer = in.readLine()) != null) {
                    if (buffer.endsWith("</XMLProtocol>")) {
                        protocol = getProtocol(buffer);
                        //Server.onlineUserList.put(protocol.getLogin(), out);
/*                      if (!login){
                            out.println("Maybe login first?");

                        }
*/                      
                        LOG.trace("Getting message from user: " + protocol.getLogin() + " recived message: " + protocol.getContent());
                        ///out.println(protocol.getLogin() + " says:" + protocol.getContent());
                        Server.sendToAll(protocol.getContent()+"</XMLProtocol>");


                    } else {
                        LOG.trace("Nop protocol do not send with it end");
                    }
                }
            }
        } catch (IOException e) {
            LOG.error("Error in reading from stream: " + e);
        } catch (JAXBException e) {
            LOG.error("Error in Marshalling: " + e);
        } finally {
            try {
                socket.close();
                LOG.trace("Socket closed");
            } catch (IOException e) {
                LOG.error("Socket no closed" + e);
            }
        }
    }

    public XMLProtocol getProtocol(String buffer) throws JAXBException {
        JAXBContext jaxbContext = JAXBContext.newInstance(XMLProtocol.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return (XMLProtocol) jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(buffer)));
    }

    public Boolean loginIn(XMLProtocol protocol) {

        return true;
    }
}

Antworten auf die Frage(2)

Ihre Antwort auf die Frage