Я пытаюсь записать и прочитать объект между клиентом и сервером, но когда я получаю объект на выходе клиента, я получаю server.Coordinates, а затем сервер cra sh.
При отладке я обнаружил, что эта функция не работает, но класс координат тот же:
void readCoordinates() throws IOException, ClassNotFoundException {
//creazione e ricezione oggetto coordinate
Coordinates coordinates = (Coordinates) objectInputStream.readObject();
System.out.println("" + coordinates.getX() + " - " + coordinates.getY());
}
Это сервер:
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);
}
}
Это клиент:
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());
}
}
Класс "Координаты" тот же:
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;
}
}
Где я ошибаюсь? Я тоже не могу писать с сервера клиенту.
Хорошего дня