Note that there are some explanatory texts on larger screens.

plurals
  1. POServer with Multiple Clients (Java) - sending strings
    primarykey
    data
    text
    <p>I'm writing a program that's basically a card game involving a server that handles the game logic and multiple clients (up to four) that are essentially the players. The clients all of GUIs made using JFrame. Anyway, one part involves pressing a button on one of the JFrames. This should send a string to the server, and the server should return a new string to ALL the clients. However, my code is not working. Here's a brief overview what I have so far:</p> <p>SERVER CODE:</p> <pre><code>public class Server { ServerSocket ss = null; ArrayList&lt;Game&gt; clients; //'Game' is essentially my Handle A Client class //bunch of other variables that store things like players, etc public Server() { try { ss = new ServerSocket(7777); } catch (Exception e) { e.printStackTrace(); System.exit(0); } idGen = 0; numPs = 0; tPlayers = new ArrayList&lt;Player&gt;(); cPlayers = new ArrayList&lt;Player&gt;(); clients = new ArrayList&lt;Game&gt;(); while (true) { try { Socket s = ss.accept(); Game g = new Game(s, this, idGen); clients.add(g); Thread thr = new Thread(g); thr.start(); idGen++; } catch (Exception e) { System.out.println("got an exception" + e.getMessage()); } System.out.println("got a connection"); try { Player obj = null; FileInputStream fis = new FileInputStream("User_Database.sav"); //serializes the player object and reads from file ObjectInputStream oi = new ObjectInputStream(fis); while ((obj = (Player) oi.readObject()) != null) { tPlayers.add(obj); } oi.close(); } catch (IOException ie) { } catch (ClassNotFoundException e) { System.out.println("Class not found exception."); } } } public class Game implements Runnable { int id; Socket mySocket; Server serv; PrintWriter out; BufferedReader in; public Game(Socket s, Server ser, int i) { mySocket = s; id = i; serv = ser; try { out = new PrintWriter(mySocket.getOutputStream(), true); in = new BufferedReader(new InputStreamReader(mySocket.getInputStream())); } catch (Exception e) { e.printStackTrace(); System.exit(0); } } public void run() { while (true) { try { String msg = in.readLine(); if (msg == "p") { serv.paintGame(); //this is the thing that should send out multiple strings } else { //does some other stuff } } catch (Exception e) { e.printStackTrace(); } } } } } </code></pre> <p>And here is the paintGame() within server. It creates some strings and then sends those to the client:</p> <pre><code>public void paintGame(){ String tCards = ""; System.out.println("Adding words to strings"); if(numPs == 2){ tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + ""; } else if(numPs == 3){ tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "_" + h3.get(0).gif + ""; } else if(numPs == 4){ tCards = "" + h1.get(0).gif + "_" + h2.get(0).gif + "_" + h3.get(0).gif + "_" + h4.get(0).gif + ""; } System.out.println("Finished adding"); for(int i = 0; i &lt; clients.size(); i++){ try{ clients.get(i).out.println(tCards); clients.get(i).out.flush(); } catch (Exception e){} } } </code></pre> <p>And finally here is the part of the client that should send out and read the strings:</p> <pre><code> public void paintGame() { String s = "p"; String[] c; String d = "_"; try { out5 = new PrintWriter(socket.getOutputStream(), true); in5 = new BufferedReader(new InputStreamReader(socket.getInputStream())); out5.println(s); } catch (IOException e) { e.printStackTrace(); } while (s != null) { try { System.out.println(s); s = in5.readLine(); } catch (Exception e) { e.printStackTrace(); } } System.out.println("Separating string!"); if (s != null) { c = s.split(d); for (int i = 0; i &lt; c.length; i++) { System.out.println(c[i]); cPaint.add(c[i]); } System.out.println("Strings separated!"); } else { System.out.println("ERROR! Null string"); } } </code></pre> <p>I'm mostly not sure about that last part. I actually have no idea how the other clients are going to get the strings that are sent and read them, since none of the clients are continuously sending/reading strings (they only send strings if a button is pressed).</p> <p>EDIT: Sorry about the weird formatting ._.</p>
    singulars
    1. This table or related slice is empty.
    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.
    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