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
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) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
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) {
// TODO Auto-generated catch block
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
public class Client extends JFrame {
private static final long serialVersionUID = 1L;
// window dimensions
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) {
// TODO Auto-generated catch block
System.out.println("Did Not Connect...Retrying");
//e.printStackTrace();
}
}
}
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 ;D
Edit: I forgot to mention that the client does connect to the host