0

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()

2 Answers 2

5

There is no compatibility between java and python objects. You must use something that is understood with both languages. I would send the object from java as a JSON string and ingest it in python as JSON, then convert it back to a python object. To convert a java object to JSON I found this

Sign up to request clarification or add additional context in comments.

Comments

0

Just to add to Mauricio's answer, The translation from the in-memory representation to a byte is called encoding/serialization/marshaling. This encoding is often tied to a particular programming language and reading the data in another language is very difficult.

If you transmit data in an encoding, you are committing yourself to your current programming language for potentially a very long time and a very bad idea.

So when we want to write data to a file or send it over the network, We should encode it as some kind of self-contained sequence of bytes that are language agnostic. The popular options are Json and XML

Other popular binary encoding option's are Bson, BISON, Messagepack

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.