Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Sending objects over object streams...

Options
  • 16-08-2003 11:03am
    #1
    Posts: 0


    hey all,

    I'm programming an application which will connect to another application over a network.

    The networking code is not the issue, what I'm trying to do is send an object from one application to the other and then show that object in the graphical area.

    Example:
    In a GUI, I have a JList and a JTextArea.
    When one either types into the text area, I want to send that text area to the other program and have the receiving program display the received text area in its own GUI.

    When data is added to the list, I want to send the list object and display that graphical object in the GUI of the receiving program.

    I think the objects are sending (ie. the data representing the state of the objects) but is it possible to display these elements in the receiving GUI?

    I'm sure i'm missing something buried within the Java graphical complexities, if anyone has any idea, help would be greatly appreciated.

    Thanks, some code below

    To send the text area:
    JTextArea new_ta = new JTextArea(50, 50);
    new_ta.setText("SENDING THIS");

    ObjectOutputStream obj_out = new ObjectOutputStream(connSock.getOutputStream());

    obj_out.writeObject(new_ta);

    System.out.println("Object should have sent now!");

    To receive the text area and show it:
    System.out.println("Receiving...");
    ObjectInputStream obj_in = new ObjectInputStream(conn.getInputStream());

    JTextArea temp = (JTextArea)obj_in.readObject();

    if(temp != null)
    System.out.println("Should have received now!");


Comments

  • Closed Accounts Posts: 94 ✭✭boo-boo


    I'm a bit rusty on Java but is the JTextArea a serialized object, or does it need to implement a serialized interface ?


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Err, I'm not sure how java objects get represented in memory, but, if they're anything like C++ objects or C pointers, you won't be able to send them over TCP, without first encapsulating the data in a format like BASE64 encoding, by virtue of the fact '\0' will imply the end of a 'string' on a socket , whilst '\0' may be any component of say a struct or a pointer or arbitrary binary data one might wish to send over a network.

    If you catch my drift.....
    Of course... maybe java just magics away such transport problems....


  • Posts: 0 [Deleted User]


    >I'm a bit rusty on Java but is the JTextArea a serialized object, or does it need to implement a serialized interface ?

    That's a good point, I'll try creating my own subclass of the area, implement serializable and see what happens, cheers.

    >Err, I'm not sure how java objects get represented in memory, but, if they're anything like C++ objects or C pointers, you won't be able to send them over TCP, without first encapsulating the data in a format like BASE64 encoding, by virtue of the fact '\0' will imply the end of a 'string' on a socket , whilst '\0' may be any component of say a struct or a pointer or arbitrary binary data one might wish to send over a network.

    In Java, every object is passed by reference, which led me to think that maybe if I passed it by reference to another program, then any changes in one will pass on to the rest of the references, but that doesn't sound right. The encoding is a problem in c++ alright, when I was coding a small basic app to send data over sockets in c ++, but I think Java gets rid of things like that, but then I'm not too sure myself, thanks for the insight, i'll look that up.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    I know this sounds incredibly obvious, but why send a JTextArea at all?

    Why not just send its content, plus the values of any other properties you may wish to mirror (size, behaviour, etc.).

    Its lighter, and you're dealing with simple data (a String, and some primitives)...which means that there is less overhead.

    jc


  • Posts: 0 [Deleted User]


    Originally posted by bonkey
    I know this sounds incredibly obvious, but why send a JTextArea at all?

    Why not just send its content, plus the values of any other properties you may wish to mirror (size, behaviour, etc.).

    Its lighter, and you're dealing with simple data (a String, and some primitives)...which means that there is less overhead.

    jc

    yeah I hear what you're saying, but this is just for an example. But Strings are Objects too and, if i could get a JTextArea to send (state and all), then that same way would work for every other object, like JLists and the like. But then you're right, in that i could just get the content of each and send that content and let the receiving program deal with the contents. I'm just trying out options, see which is less hassle and works well. thanks for the input.


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    The main reason I'm suggesting using String objects etc. is that they are inherently serializable. If you build "lightweight" classes of basic datatypes to carry your info, they too will be serializable.

    With anything serializable, you can serialize it, transfer the resultant stream, and deserialize on the other side.

    This should prove far simpler and easier than trying to figure out how to pass the JTextArea (etc.) objects. These, IIRC, are not serializable, and from what I remember offhand you can't easily subclass into a serializable form.

    Best of luck with it...I'd be interested in knowing how you resolve it at the end of the day.

    jc


Advertisement