Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default Reading from ObjectInputStream

    I am unable to read all objects written to the outputstream.

    There are 5 different instances of an object.. yet i can read only one.

    This is my code...

    public void pktReceiver()
    {
    try
    {
    serversocket=new ServerSocket(3000);

    System.out.println("$$$$");
    while(true)
    {
    socket=serversocket.accept();
    // socket1=serversocket.accept();
    System.out.println("Test-1");

    System.out.println("Test-2");
    // dis=new DataInputStream(socket1.getInputStream());
    //pkt_len=dis.readInt();
    Tpn t= (Tpn)Naming.lookup("//localhost/Server");



    // DLL pkt = (DLL)Naming.lookup("//localhost/Server");
    InputStream is = socket.getInputStream();
    ObjectInputStream ois = new ObjectInputStream(is);
    Link pktrecv = (Link) ois.readObject();

    /* arr=new String[pkt_len];
    System.out.println("Test-3"); */
    for(int tot=0;tot<pktrecv.tot_pkt;tot++)
    {


    if(pktrecv!=null)
    {
    System.out.println("PacketReceived");
    System.out.println("Encoded:"+pktrecv.en+"\nPacket no:"+pktrecv.pktno+"\nSource:"+pktrecv.src+"\nDest :"+pktrecv.dest+"\nData:"+pktrecv.data+"\nDist:"+p ktrecv.dist);

    }



    count++;


    }
    is.close();


    // socket.close();
    // serversocket.close();

    }



    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    }



    Please help!!

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    well, what problem is actually occurring (e.g. what exception?), and what is your code to write the objects to the stream at the other end?

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    You are only attempting to read 1 object, and you are reading 1 object. You then go back and wait for a new connection again.

    To read 5 objects, you would ned 5 lines (or a loop) similar to

    Java Code:
    Link pktrecv = (Link) ois.readObject();
    within your greater while loop.

  4. #4
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    This is how i send the packets...


    socket1=new Socket("localhost",3000);
    // socket=new Socket("localhost",9001);
    // DataOutputStream dos=new DataOutputStream(socket1.getOutputStream());
    OutputStream os = socket1.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(os);


    gh=s.length;




    // dos.writeInt(gh);



    for(int h=0;h<gh;h++)
    {

    //JOptionPane.showMessageDialog(this,s.length);
    double ch= Math.random();

    //double ch=(int)(assign);
    //int h1+=h;
    String pcnt=pktcnt+inc;
    Tpn t =(Tpn)Naming.lookup("//localhost/Server");
    t.pktEncode(pcnt,ch);

    if(ch<xx)
    {



    String destt="R-101"; //if available, else call check router and add appropriately

    String app="\n\n@@\nSA:"+source+"\nPacket-"+inc+"::$"+s[h]+"$\nDA:"+dest;

    jTextArea2.append(app);
    System.out.println("%%%%");

    String sym="@";
    String data=s[h];

    System.out.println("$$$");
    // DLL node = (DLL)Naming.lookup("//localhost/Server");
    System.out.println("Lookedup");
    Link node= new Link(sym,inc,source,destt,data,1);

    node.insertlast(sym, inc, source, destt, data, 1);
    node.tot_pkt=gh;

    oos.writeObject(node);
    System.out.println("packets inserted");

    // jTextArea2.append("\n\nSA:"+source+"\nPacket-"+inc+"::"+str+""+s[h]+"\nDA:"+dest);

    // socket=new Socket("localhost",9001);
    //DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
    // dos.writeUTF(app);


    inc++;
    Thread.sleep(1000);
    }
    else
    {

    // String str=Integer.toBinaryString(ch);

    String symb="$";
    String destin="###";
    String dataa=s[h];
    //.println("SA:"+source+"Packet-"+inc+""+s[h]+"::"+str+"\n\n");

    String a="\n\nSA:"+source+"\nPacket-"+inc+"::$"+s[h]+"$::\nDA:"+dest+"\n";
    jTextArea2.append(a);

    //DLL node = (DLL)Naming.lookup("//localhost/Server");
    System.out.println("Lookedup");
    Link node= new Link(symb,inc,source,destin,dataa,0);
    // Link node= new Link(symb,inc,source,destin,dataa,0);
    node.tot_pkt=gh;
    node.insertlast(symb, inc, source, destin, dataa, 0);
    oos.writeObject(node);


    //socket=new Socket("localhost",9001);
    // DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
    // dos.writeUTF(a);



    // socket.close();

    inc++;
    Thread.sleep(1000);
    }


    }
    oos.close();
    os.close();
    //socket1.close();
    }



    catch(Exception e)
    {
    e.printStackTrace();
    }



    The error i get is...

    java.net.SocketException: Broken pipe

    This seems to happen after i send the first packet and receive the same followed by seconding the second packet..when the exception is generated.

  5. #5
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    Thanks for your help.

    Adding Link pktrecv = (Link)ois.readObject(); in a loop did work.

    Only now i get the following error..

    java.io.EOFException
    at java.io.ObjectInputStream$BlockDataInputStream.pee kByte(ObjectInputStream.java:2498)
    at java.io.ObjectInputStream.readObject0(ObjectInputS tream.java:1273)
    at java.io.ObjectInputStream.readObject(ObjectInputSt ream.java:348)
    at Router.pktReceiver(Router.java:242)
    at Router.<init>(Router.java:48)
    at Router.main(Router.java:397)


    Is this because i am not closing the inputstream properly?

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You didn't exit the loop soon enough. If you continue trying to read past the end of the stream you will throw that exception.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    Ok. I use readObject() twice. Once outside the for loop and then inside the for loop. But i do close the stream and i do not read beyond the for loop either.

    I just dont understand why the error!

  8. #8
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Then you don't exit the for loop soon enough. The exception means you tried to read past the end of the file.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  9. #9
    Join Date
    Apr 2009
    Posts
    15
    Rep Power
    0

    Default

    Thank you very very much! It is now working without any errors! Thank you! :)

Similar Threads

  1. Reading XML from file
    By chriscant in forum New To Java
    Replies: 0
    Last Post: 03-17-2009, 01:34 PM
  2. Reading a doc file
    By thehero in forum Advanced Java
    Replies: 0
    Last Post: 03-15-2009, 06:48 AM
  3. Stops when opening ObjectInputStream
    By Norberhuis in forum Networking
    Replies: 4
    Last Post: 01-09-2009, 04:19 PM
  4. Reading cookies from JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 05:37 PM
  5. Reading a file for use
    By peachyco in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 03:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •