Hey guys,
in my simple client hos program, my Host and Client wont create ObjectInputStreams and ObjectOutputStreams with each other.
Here is my code:
Host.java
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
| public class Host extends JFrame { static JFrame frame; private static final long serialVersionUID = 1L; ServerSocket server; ArrayList<Integer> x = new ArrayList<Integer>(); ArrayList<Integer> y = new ArrayList<Integer>(); public ArrayList<ObjectInputStream> clientIn = new ArrayList<ObjectInputStream>(); public ArrayList<ObjectOutputStream> clientOut = new ArrayList<ObjectOutputStream>(); int connected; String clientMove; Object play = null;
@SuppressWarnings("deprecation") public Host() { try {
server = new ServerSocket(2020); System.out.println("Created Socket...");
} catch (IOException e) { e.printStackTrace(); } while (true) { try { System.out.println("Waiting for Client..."); Socket clientSocket = server.accept(); System.out.println("Found Client..."); connected++; System.out.println("Establishing Input Stream With Client..."); clientIn.add(new ObjectInputStream(clientSocket.getInputStream())); System.out.println("Done!"); System.out.println("Establishing Output Stream With Client..."); clientOut.add(new ObjectOutputStream(clientSocket.getOutputStream())); System.out.println("Done!"); try { System.out.println("Wating for Player..."); try { play = clientIn.get(connected).readObject(); System.out.println("Found Player!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < clientIn.size(); i++) { System.out.println("Writing Player to Clients..."); try { clientOut.get(i).writeObject(play); clientOut.get(i).flush(); } catch (IOException e) { e.printStackTrace(); } System.out.println("Done!"); } }
}
public static void main(String[] args) {
frame = new JFrame("Host"); frame.setSize(250, 250); frame.setResizable(false); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); new Host(); }
} |
Client.java
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
| public class Client extends JFrame { private static final long serialVersionUID = 1L; static int width = 280; static int height = 280; static JFrame frame; InetAddress hostIP; public static ObjectInputStream hostIn; public static ObjectOutputStream hostOut; public static int connected;
public Client(String ip) { while (true) { try { hostIP = InetAddress.getByName(ip); System.out.println("Valid IP Trying to Connect..."); Socket hostSocket = new Socket(hostIP, 2020); System.out.println(hostSocket.getInetAddress()); System.out.println("Connected!"); System.out.println("Establishing Input Stream With Host..."); hostIn = new ObjectInputStream(hostSocket.getInputStream()); System.out.println("Done!"); System.out.println("Establishing Output Stream With Host..."); hostOut = new ObjectOutputStream(hostSocket.getOutputStream()); System.out.println("Done!");
} catch (IOException e) { System.out.println("Did Not Connect...Retrying"); } } }
public static void main(String args[]) { Game game = new Game(); frame = new JFrame("Client"); frame.setSize(width, height); frame.add(game); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); new Client("localhost");
}
} |
Thanks for any help

Edit: I forgot to mention that the client does connect to the host