android - issue in reading a serialized object -


i trying create client-server android app in want transfer file using udp protocol. till able transfer file , receive acknowledgements packets.

now want add sequence numbers data in packet. have tried following:

  • create bytearrayoutputstream.
  • wrap in objectoutputstream
  • write data object using writeobject()

serialized class includes:

public class message implements serializable {  private int seqno; private byte[] data; private boolean ack;  public message(int seqno, byte[] data, boolean ack) {     this.seqno = seqno;     this.data = data;     this.ack = ack; } 

client side

byte[] filebytes = new byte[500]; bytearrayoutputstream outstream = new bytearrayoutputstream(); objectoutputstream os = new objectoutputstream(outstream);  while((numbytesread = inputbuf.read(filebytes)) != -1) { //datagrampacket packet = new datagrampacket(filebytes, filebytes.length); if (os == null) {     os = new objectoutputstream(outstream); }         message msg = new message(++seqno, filebytes, false);        os.writeobject(msg);     os.flush();     os.reset();      byte[] data = outstream.tobytearray();     atagrampacket packet = new datagrampacket(data, data.length);     clientsocket.send(packet);     } 

server side

byte[] incomingdata = new byte[1024]; while (true) { try{     datagrampacket incomingpacket = new datagrampacket(incomingdata, incomingdata.length);                                                               serversocket.receive(incomingpacket);     byte[] data = incomingpacket.getdata();                      bytearrayinputstream in = new bytearrayinputstream(data);                                      objectinputstream = new objectinputstream(in);      if (is == null) {            = new objectinputstream(in);     }     message msg = (message) is.readobject();     system.out.println(msg.getseqno());     out.write(msg.getdata(),0,msg.getdata().length);       } 

the problem facing is

  1. i receiving same sequence number each packet (i.e. 1)
  2. i not sure buffer size incoming packet, using 500 bytes @ client side , 1024 at
    sever. , if take 500 bytes @ both sides eofexception.

i appreciate if suggest better ways implement same thing! :)

message msg = new message(++seqno, filebytes, false);

here assuming prior read() filled buffer. on last read() before end of file won't, , isn't guaranteed fill time, transfer @ least 1 byte.

you should passing read count 'numbytes' constructor, , should create byte array of size, , copy many bytes it.

other issues:

  • it impossible 'os' null @ point you're testing it.
  • ditto 'is'.
  • you should creating new objectoutputstream , bytearrayoutputstream per datagram.
  • java datagrams keep shrinking size of shortest datagram payload received far. must either create new 1 per receive, or @ least reset length before each receive.
  • you need larger buffer @ receiver because of objectoutputstream overheads.
  • i don't believe code presently works @ all, let alone keep getting same sequence number. more keep getting same message, because you're ignoring exception somewhere.

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -