it is my first time using java networking. I have to use it for a project i am working on which is a simple card game. My server and client will communicate as the "Connection Received" does get printed. But after this line the following error occurs:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at ClientConnectionManager.reciveData(ClientConnectionManager.java:49)
at LogicThread.getHandSize(GameLogic.java:22)
at LogicThread.run(GameLogic.java:16)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-4" java.lang.NullPointerException
at LogicThread.getHandSize(GameLogic.java:22)
at LogicThread.run(GameLogic.java:16)
at java.lang.Thread.run(Unknown Source)
the classes in question are:
static ObjectInputStream input;
static ObjectOutputStream output;
static Socket socket;
public static void connectToServer(){
try {
System.out.println("connecting");
socket = new Socket(ip, port);
input = new ObjectInputStream(socket.getInputStream());
output = new ObjectOutputStream(socket.getOutputStream());
output.flush();
System.out.println("connected");
reciveData();
logic.startLogic();
//listenForData();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Object reciveData(){
try {
Object obj = input.readObject(); - Error is on this line
System.out.println(obj);
return obj;
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return null;
}
The server code for sending data:
ServerSocket server;
server = new ServerSocket(2302);
Socket connection;
connection = server.accept();
output = new ObjectOutputStream(connection.getOutputStream());
public void sendData(Object data){
try {
output.writeObject(data);
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
I am not sure what causes this because the server sends a "Connection Received" which the client receives with no issue and prints. But any call from outside the connectToServer() Method cause the error above.