File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes file as a byte[] over UDP problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "file as a byte[] over UDP problem" Watch "file as a byte[] over UDP problem" New topic
Author

file as a byte[] over UDP problem

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692



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!


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41885
    
  63
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.


Ping & DNS - my free Android networking tools app
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1059
    
  10

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

Joined: Jul 03, 2011
Posts: 692

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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: file as a byte[] over UDP problem