i'm trying to write and read an object between a client and a server, but when i receive the object in the output of the client i receive server.Coordinates, and then the server crash.
With the debug i found that this function doesen't work, but the class of coordinates is the same :
void readCoordinates() throws IOException, ClassNotFoundException {
//creazione e ricezione oggetto coordinate
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
This is the server :
public class Server {
static Server server;
//socket server
ServerSocket serverSocket;
//stream output
OutputStream outputStream;
ObjectOutputStream objectOutputStream;
//stream input
InputStream inputStream ;
ObjectInputStream objectInputStream;
public static void main (String[] args) {
//Server start
server = new Server();
server.start();
}
public void start(){
try {
//creazione server socket
serverSocket = new ServerSocket(8080);
System.out.println("ServerSocket awaiting connections...");
//accept connection
Socket socket = serverSocket.accept();
System.out.println("Connection from " + socket + "!");
//output
outputStream = socket.getOutputStream();
objectOutputStream = new ObjectOutputStream(outputStream);
//input
inputStream = socket.getInputStream();
objectInputStream = new ObjectInputStream(inputStream);
//receive coordinates
readCoordinates();
//write coordinates
writeCoordinates(1, 1);
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("Error");
System.exit(1);
}
}
void readCoordinates() throws IOException, ClassNotFoundException {
//creazione e ricezione oggetto coordinate
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
public void writeCoordinates(int x, int y) throws IOException{
//creazione e invio oggetto coordinate
Coordinates coordinates = new Coordinates(x, y);
objectOutputStream.writeObject(coordinates);
}
}
This is the client :
public class ConnectionManager {
//server
String serverIP = "127.0.0.1";
int serverPort = 8080;
Socket socket;
// stream output
OutputStream outputStream;
ObjectOutputStream objectOutputStream;
// stream input
InputStream inputStream;
ObjectInputStream objectInputStream;
public ConnectionManager() {
try {
//creation socket
System.out.println("Socket creation...");
this.socket = new Socket(serverIP,serverPort);
//creation input e output stream
System.out.println("Creation input and output stream...");
this.inputStream = this.socket.getInputStream();
this.objectInputStream = new ObjectInputStream(this.inputStream);
this.outputStream = socket.getOutputStream();
this.objectOutputStream = new ObjectOutputStream(this.outputStream);
} catch (UnknownHostException e){
System.err.println("Unreacheable host");
} catch (IOException e) {
System.out.println(e.getMessage());
System.out.println("Error");
System.exit(1);
}
}
public void writeCoordinates(int x, int y) throws IOException{
//send coordinates
Coordinates coordinates = new Coordinates(x, y);
objectOutputStream.writeObject(coordinates);
}
void readCoordinates() throws IOException, ClassNotFoundException {
//read coordinates
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
}
The class "Coordinates" is the same :
public class Coordinates implements Serializable{
//coordinate
int x;
int y;
Coordinates(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Where i'm going wrong? I can't write from the server to the client too.
Have a nice day
ServerSocket awaiting connections... Connection from Socket[addr=/127.0.0.1,port=61595,localport=8080]! client.Coordinates Error Java Result: 1 BUILD SUCCESSFUL (total time: 6 seconds)I find that in the server when i reach the line of objectInputStream, the variable is empty and the server crash, but on the client objectInputStream works