I want to send from Python an object to Java by Sockets being Python (client) and Java (server), however I can not do it and I only see this:
invalid stream header: 8003635F
I try with Python-Python and Java-Java, and I did not have any inconvenience. I would like to know if my code is wrong or there is something I am forgetting. I will be attentive to any response. Attached code:
Java code
public static void main(String[] args) {
int port = 10002;
try {
ServerSocket server = new ServerSocket(port);
System.out.println("Waiting for clients...");
Socket client = server.accept();
System.out.println("Connect: " + client.getInetAddress());
Vehicle car;
ObjectInputStream ois = new ObjectInputStream(client.getInputStream());
car = (Vehicle) ois.readObject();
System.out.println("Brand: " + car.getBrand() + "Model: " + coche.getModel());
} catch (IOException ex) {
System.out.println(ex.getMessage());
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}
}
}
class Vehicle implements Serializable {
private String brand;
private String model;
public Vehicle(String brand, String model) {
this.brand = brand;
this.model = model;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
@Override
public String toString() {
return "Vehicle{" + "brand=" + brand + ", model=" + model + '}';
}
}
Python code
class Vehicle():
def __init__(self,brand,model):
self.brand = brand
self.model = model
def show(self):
print("Brand: ",self.brand,"\nModel: ",self.model)
car = Vehicle("Mazda","3")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverAddress = ("172.16.4.18",10002)
sock.connect(serverAddress)
sock.send(pickle.dumps(car))
sock.close()