Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading binary file in Java through Socket

807607Sep 28 2006 — edited Oct 2 2006
Hello,

I'm having a very confusing problem with my socket implementation.

I have a Socket Client written in C that is sending a binary file and a Socket Server written in Java that is receiving that binary file.

Unfortunately, using methods by my own creation and ones seen elsewhere in this forum, I have been unable to read that binary file to its entirety. I have some code posted below. Further troubleshoot tactics include verifying that the client is sending the data properly (as in it is reading and sending the bytes properly using a bit comparison tool), verifying that text files work properly and that I can send a file from the Socket Server (in Java) to the Socket Client (in C) properly.

Scenario:
70 MB zip file transfer through the socket.

Results:
20 MB of file is copied and then the read function blocks for more data in the buffer, but the buffer is not empty because I can use a BufferedReader to read more data out of the buffer.

Any help would be greatly appreciated.

Server code:
BufferedOutputStream bo = null;
BufferedInputStream bis = null;
byte[] barray = new byte[8192];
int read = 0

bo = new BufferedOutputStream (new FileOutputStream("foo.zip"));
bis = new BufferedInputStream(socket.getInputStream ());

while ((read = bis.read(barray)) != -1){
    bo.write(barray, 0, read);
}
bo.close();
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 30 2006
Added on Sep 28 2006
14 comments
3,305 views