Hi!
I have a multi-threaded server waiting for incoming connections. When a client connects, an input and an output thread are created to handle I/O to that client. The server's output thread starts to send a Vector<Tank> to the client.
The client, when constructed, joins the server and creates a thread to handle all input coming from the server. The thread starts when constructed and waits for the Vector<Tank> to be sent. After joining, the client creates a GameBoard, giving it a reference to the input thread. The GameBoard, when constructed, tries to retrieve the Vector<Tank> from the input thread.
The issue is, the GameBoard is constructed before the input thread can receive the Vector<Tank>. I need the GameBoard constructor to wait for the input thread to receive it's first update of the vector, or have the client wait for the thread to receive it before constructing the GameBoard. I hope this makes sense!
If there's an easier solution to this mess, then please tell me. Also, if you notice anything else that's wrong or could be better, don't hesitate to mention it. Thanks!
-CLIENT-1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package tw.client;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataOutputStream; import java.io.ObjectOutputStream; import java.net.InetAddress; import java.net.Socket;
import javax.swing.JButton; import javax.swing.JFrame;
public class Client implements ActionListener {
private JFrame frame = null; public static final int W_WIDTH = 800; public static final int W_HEIGHT = 600; private JButton joinButton = null; private static final int PORT = 2720; private Socket socket = null; private InputThread input = null; private DataOutputStream output = null; private static final int FRAMES_PER_SECOND = 25; private static final int SKIP_TICKS = 1000 / FRAMES_PER_SECOND; private GameBoard gameBoard; private boolean running = true; public Client() { frame = new JFrame("Tank Wars - Client"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(W_WIDTH, W_HEIGHT); frame.setLocationRelativeTo(null); frame.setResizable(false); frame.setVisible(true); joinServer(); gameBoard = new GameBoard(input, output); frame.add(gameBoard); gameBoard.requestFocusInWindow(); run(); } @Override public void actionPerformed(ActionEvent e) { if (e.getActionCommand() == "Join") { joinButton.setEnabled(false); joinButton.setVisible(false); gameBoard.setVisible(true); frame.validate(); run(); } } private void run() { long nextGameTick = System.nanoTime() / 1000000; long sleepTime; while (running) { gameBoard.updateGame(); gameBoard.repaint(); nextGameTick += SKIP_TICKS; sleepTime = nextGameTick - System.nanoTime() / 1000000;
if (sleepTime > 0) { try { Thread.sleep(sleepTime); } catch(Exception e){ e.getStackTrace(); } } } } public void joinServer() { try { System.out.println("Initializing ..."); InetAddress addr = InetAddress.getLocalHost(); socket = new Socket(addr, PORT); System.out.println("The new socket: " + socket); input = new InputThread(socket); output = new DataOutputStream(socket.getOutputStream()); } catch(Exception e) { e.printStackTrace(); } } public void stop() { running = false; } public static void main(String[] args) { new Client(); } } |
-InputThread-1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package tw.client;
import java.io.IOException; import java.io.ObjectInputStream; import java.net.Socket; import java.util.Vector;
import tw.Projectile; import tw.Tank;
public class InputThread extends Thread { private ObjectInputStream in = null; private boolean running = true; private int playerNr = 0; private Vector<Tank> updatedTanks = null; private Vector<Projectile> updatedProjectiles = null; public InputThread(Socket socket) { try { in = new ObjectInputStream(socket.getInputStream()); } catch (IOException e) { e.printStackTrace(); } updatedTanks = new Vector<Tank>(); start(); } @SuppressWarnings("unchecked") public void run() { try { playerNr = in.readInt(); System.out.println("playerNr " + playerNr); } catch (IOException e1) { e1.printStackTrace(); } try { while (running) { updatedTanks = (Vector<Tank>) in.readObject(); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public void stopRunning() { running = false; } public int getPlayerNr() { return playerNr; } public Vector<Tank> getUpdatedTanks() { return updatedTanks; } } |
-GameBoard-1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
| package tw.client;
import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.io.DataOutputStream; import java.io.IOException; import java.util.Vector;
import javax.swing.JComponent;
import tw.Projectile; import tw.Tank;
public class GameBoard extends JComponent implements KeyListener { private InputThread input = null; private DataOutputStream output = null; private StatusPanel statusPanel = null; private int playerNr = 0; private Vector<Tank> tanks = null; private Vector<Projectile> projectiles = null; private Tank pTank = null; public GameBoard(InputThread input, DataOutputStream output) { this.input = input; this.output = output; setFocusable(true); setBackground(Color.LIGHT_GRAY); setDoubleBuffered(true); setSize(Client.W_WIDTH, Client.W_HEIGHT);
addKeyListener(this); playerNr = input.getPlayerNr(); tanks = input.getUpdatedTanks(); for (int i = 0; i < tanks.size(); i++) if (tanks.get(i).getPlayerNr() == playerNr) pTank = tanks.get(i); statusPanel = new StatusPanel(pTank, playerNr); add(statusPanel); this.revalidate(); } public void updateGame() entitieset updated entities tanks = input.getUpdatedTanks(); int activeTank = 0; for (int i = 0; i < tanks.size(); i++) if (tanks.get(i).isActive()) activeTank = tanks.get(i).getPlayerNr(); statusPanel.update(activeTank); } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; if (tanks != null) { for (int i = 0; i < tanks.size(); i++) { if (tanks.get(i).isAlive()) tanks.get(i).draw(g2d); } } } public void keyPressed(KeyEvent e) { int key = e.getKeyCode(); if (pTank.isActive()) if (key == KeyEvent.VK_UP || key == KeyEvent.VK_DOWN || key == KeyEvent.VK_LEFT || key == KeyEvent.VK_RIGHT) { try { output.writeInt(key); System.out.println("Client output: " + key); } catch (IOException e1) { e1.printStackTrace(); } } } public void keyReleased(KeyEvent e) { int key = e.getKeyCode(); if (pTank.isActive()) if (key == KeyEvent.VK_SPACE) try { output.writeInt(key); System.out.println("Client output: " + key); } catch (IOException e1) { e1.printStackTrace(); } } public void keyTyped(KeyEvent e) { } } |