First of all, hello everybody, I'm new here
I'm making communication between two java programs, server and client. Main thing that they should do is some encrypting, but that is not problem. I'v kinda "stolen" chat communication from one of the best youtube explanations and removed some unnecessary parts. Communication is working great, client is working great and server is working great AS LONG as I keep him away from GUI.
When server is class it self, and when I'm starting program from main class, it works great, writing down on console everything I want, but when i put that in swing GUI (default, created with netbeans) and try to start server from button click, I get frozen gui. Ok, I was thinking, since server has two "while(true)" loops, one for accepting connections and one for reading, I just should put him in new thread and that should do it. When I do that, I don't get frozen GUI, but I can't show anything on my server GUI from server it self. Server is working, it's printing everything in console: for example
where data[0] is ip address of client, but if i just addSystem.out.println(data[0] + " has connected.");
where console is my JTextArea in GUI, it won't append. I'v tried a lot of approaches but I can't get it working...System.out.println(data[0] + " has connected."); console.setText("User has connected");
I'll get you guys here just server code with main class:
where you can see that I'm starting server from main method. My question is, what's the best way to implement that server in GUI?import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.Iterator; import java.math.BigInteger; public class Test2Server { ArrayList clientOutputStreams; ArrayList<String> onlineUsers = new ArrayList(); public class ClientHandler implements Runnable{ BufferedReader reader; Socket sock; PrintWriter client; public ClientHandler(Socket clientSocket, PrintWriter user) { // new inputStreamReader and then add it to a BufferedReader client = user; try { sock = clientSocket; InputStreamReader isReader = new InputStreamReader(sock.getInputStream()); reader = new BufferedReader(isReader); } // end try catch (Exception ex) { System.out.println("error beginning StreamReader"); } // end catch } // end ClientHandler() public void run() { String message; String[] data; String connect = "Connect"; String disconnect = "Disconnect"; String chat = "Chat"; String diffie = "Diffie"; try { while ((message = reader.readLine()) != null) { data = message.split("¥"); if (data[2].equals(connect)) { userAdd(data[0]); System.out.println(data[0] + " has connected."); } else if (data[2].equals(disconnect)) { userRemove(data[0]); System.out.println(data[0] + " has disconnected."); } else if (data[2].equals(chat)) { tellEveryone(message); } else if (data[2].equals(diffie)){ System.out.println(data[1]); } else { System.out.println("No Conditions were met."); } } // end while } // end try catch (Exception ex) { System.out.println("lost a connection"); clientOutputStreams.remove(client); } // end catch } // end run() } // end class ClientHandler public void userAdd (String data) { String message; String add = "¥ ¥Connect"; String done = "Server¥ ¥Done"; onlineUsers.add(data); String[] tempList = new String[(onlineUsers.size())]; onlineUsers.toArray(tempList); for (String token:tempList) { message = (token + add); tellEveryone(message); } tellEveryone(done); } public void userRemove (String data) { onlineUsers.remove(data); } public void tellEveryone(String message) { Iterator it = clientOutputStreams.iterator(); while (it.hasNext()) { try { PrintWriter writer = (PrintWriter) it.next(); writer.println(message); writer.flush(); } catch (Exception ex) { System.out.println("error telling everyone"); } } } //------------------------------------main------------------------------------- public static void main(String args[]){ new Test2Server().go(); } public void go() { clientOutputStreams = new ArrayList(); try { ServerSocket serverSock = new ServerSocket(5000); while (true) { // set up the server writer function and then begin at the same // the listener using the Runnable and Thread Socket clientSock = serverSock.accept(); PrintWriter writer = new PrintWriter(clientSock.getOutputStream()); clientOutputStreams.add(writer); // use a Runnable to start a 'second main method that will run // the listener Thread listener = new Thread(new ClientHandler(clientSock, writer)); listener.start(); System.out.println("got a connection"); } // end while } // end try catch (Exception ex){ System.out.println("error making a connection"); } // end catch } // end go() }
This is my first post on this forum so I'm sorry if I'm doing something wrong.
Thanks.