Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file as a byte[] over UDP problem

 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic