• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

file as a byte[] over UDP problem

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


client code


Server code


Client sending a file in a packet as a byte[] and server receives the packet get the byte[] from it & write to a file
but after running when i open the file , it says file has been corrupted and all...so can you point out where there is a problem ..i am not well familiar with streams and all!
 
Rancher
Posts: 43027
76
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few comments in no particular order.

1) In the client code, the ByteArrayOutputStream is unused and unnecessary.

2) If the file size exactly 1024 bytes? If it is larger, the code is sending partial content that will be corrupted. If it is smaller then the code sends too many bytes, again possibly corrupting the contents - the number of bytes in the packet should be what fis.read returns, not the size of the byte[].

3) Have you gotten this client/server pair to work? If not, start with that.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

naved momin wrote:

client code


You ignore the number of bytes read by the fis.read(clientBuffer) statement. Also that statement does not guarantee to read the whole of the buffer (check the Javadoc).
You assume 1024 bytes have been read when you write the data to the ByteArrayOutptuStream but it may not be that long.
If your file is longer than 1024 bytes you will read at most 1024 bytes of the file and ignore the rest of the file.


Server code


You only read one datagram and a datagram packet and that has a maximum length of 65,507 bytes. If your file is longer than this you will guarantee to lose bytes. Also, the packet may be fragmented into 2 or more sub-packets and since you read only one you will ignore all but the first.



Client sending a file in a packet as a byte[] and server receives the packet get the byte[] from it & write to a file
but after running when i open the file , it says file has been corrupted and all...so can you point out where there is a problem ..i am not well familiar with streams and all!



UDP is an unreliable protocol so you can't guarantee that every datagram packet sent will be received since datagram packets can be discarded if the system becomes congested. If you want a reliable channel then you need to use TCP or create your own protocol on top of UDP.

I suggest that

a) you first learn to read and write files without worrying about UDP.
b) you then spend time with UDP just sending and receiving short messages without worrying about files.
c) then when you have both a) and b) going you think about combining them and creating a reliable protocol.
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:A few comments in no particular order.

1) In the client code, the ByteArrayOutputStream is unused and unnecessary.

2) If the file size exactly 1024 bytes? If it is larger, the code is sending partial content that will be corrupted. If it is smaller then the code sends too many bytes, again possibly corrupting the contents - the number of bytes in the packet should be what fis.read returns, not the size of the byte[].

3) Have you gotten this client/server pair to work? If not, start with that.



silly mistake ..filesize in kbs * 1024 = buffersize ...lolz...thanks mate!
 
reply
    Bookmark Topic Watch Topic
  • New Topic