Java serialization over network

Discussion in 'Java' started by elbaid, Apr 2, 2009.

  1. elbaid

    elbaid Guest

    Just want to know if there's a tutorial or an how-to for serializing
    object, put it in a stream over network, and deserialize it on the
    other point. I understand the principles of serialization, I/O,
    streams, sockets and so on, just want an example (client sending object
    to a server) to start with.

    Thank you.
     
    elbaid, Apr 2, 2009
    #1
    1. Advertisements

  2. elbaid

    Arne Vajhøj Guest

    Just wrap the InputStream/OutputStream from the Socket in
    a ObjectInputStream/ObjectOutputStrem and it should be very easy.

    Code snippets:

    Socket s = new Socket("localhost", 12345);
    ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
    oos.writeObject(someobject);
    oos.close();
    s.close();

    ServerSocket ss = new ServerSocket(12345);
    Socket s = ss.accept();
    ObjectInputStream ios = new ObjectInputStream(s.getInputStream());
    SomeClass someobject = (SomeClass)ios.readObject();
    ios.close();
    s.close();
    ss.close();

    Arne
     
    Arne Vajhøj, Apr 2, 2009
    #2
    1. Advertisements

  3. elbaid

    elbaid Guest

    Thank you very much!


     
    elbaid, Apr 2, 2009
    #3
  4. elbaid

    Roedy Green Guest

    You normally would use RMI for that.

    There is nothing to it conceptually. Both ends must have the code for
    all the classes used. It is so different that saving to disk and
    restoring.

    You probably want to GZIP the stream.

    See http://mindprod.com/applet/fileio.html for the code.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Danish studies of 10,000 birds killed revealed that almost all died in
    collisions with buildings, cars and wires; only 10 were killed by windmills.
    Alternative energy sources are absolutely necessary.
    Global warming will kill birds and bats, as well as other species,
    in much greater numbers than wind power."
    ~ Dr. David Suzuki
     
    Roedy Green, Apr 2, 2009
    #4
  5. elbaid

    Roedy Green Guest

    see http://mindprod.com/jgloss/rmi.html
    http://mindprod.com/jgloss/serialization.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    "Danish studies of 10,000 birds killed revealed that almost all died in
    collisions with buildings, cars and wires; only 10 were killed by windmills.
    Alternative energy sources are absolutely necessary.
    Global warming will kill birds and bats, as well as other species,
    in much greater numbers than wind power."
    ~ Dr. David Suzuki
     
    Roedy Green, Apr 2, 2009
    #5
  6. elbaid

    Tom Anderson Guest

    Good idea. And make sure it's buffered - i think GZIPOutputStream does
    some buffering internally, but ObjectOutputStream doesn't (i think!), so
    if you're using the latter, add a BufferedOutputStream to the stack.

    tom
     
    Tom Anderson, Apr 4, 2009
    #6
  7. elbaid

    Nilshan Guest

    -----------------------------------------------------------------------------

    1. Example class to be serialized.

    public class RuleInfo implements Serializable
    {

    private String data="";

    public RuleInfo() {
    }

    public String getData() {
    return data;
    }

    public void setData(String data) {
    this.data = data;
    }

    }

    2. User servlet to serialize...

    protected void processRequest(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    try {
    RuleInfo info = new RuleInfo();
    info.setData("1");

    RuleInfo info1 = new RuleInfo();
    info1.setData("2");

    List<RuleInfo> list = new ArrayList<RuleInfo>();
    list.add(info);
    list.add(info1);

    ObjectOutputStream out1 = new ObjectOutputStream
    (response.getOutputStream());
    out1.writeObject(list);
    out1.flush();
    out1.close();

    System.out.println("This is done");

    } finally {
    }
    }

    3. Get Serialized Object at another place using ..

    public void test() throws ClassNotFoundException
    {

    String urlString = "http://localhost:8084/WebApplication1/
    BinaryPkgServlet";

    try
    {
    InputStream stream = null;
    URL url = new URL(urlString);

    HttpURLConnection httpURLConnection = (HttpURLConnection)
    url.openConnection();

    /**
    * If Response Code is 200 [OK] then get InputStream of
    * Compiled binary Package remotely and store in local
    file
    * system.
    */

    int httpResponseCode = httpURLConnection.getResponseCode
    ();


    if (httpResponseCode == 200)
    {
    stream = httpURLConnection.getInputStream();

    ObjectInputStream st = new ObjectInputStream(stream);
    List<RuleInfo> list = (List<RuleInfo>)st.readObject();

    System.out.println("List "+list +"and size "+list.size
    ());

    for(RuleInfo info : list){
    System.out.println("Info "+info.getData());
    }
    }

    }
    catch (IOException e)
    {
    }

    }


    Thanks,
    Nilshan.
     
    Nilshan, Apr 23, 2009
    #7
  8. elbaid

    Roedy Green Guest

    Roedy Green, Apr 24, 2009
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.