Note that there are some explanatory texts on larger screens.

plurals
  1. POmultithread client-server chat, using sockets
    primarykey
    data
    text
    <p>Server and client communicating with my own protocol which looks like XMPP. I should to realize chat application. So when one user write String it immedeatly should be sended to other client through the server. I have method sendToAll on server. But user see the message of other user only when it press enter. <strong>How can user receive messages without pressing enter button?</strong></p> <p>So this is my client:</p> <pre><code>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(); } } </code></pre> <p>And Server with ServerThread.</p> <pre><code>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(); } } </code></pre> <p>ServerThread.</p> <pre><code>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("&lt;/XMLProtocol&gt;"); socket = s; new Thread(this); } public void run() { try { while (true) { if ((buffer = in.readLine()) != null) { if (buffer.endsWith("&lt;/XMLProtocol&gt;")) { 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()+"&lt;/XMLProtocol&gt;"); } 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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload