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.