aspose file tools*
The moose likes Java in General and the fly likes Transferring file name then file data over socket Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Transferring file name then file data over socket" Watch "Transferring file name then file data over socket" New topic
Author

Transferring file name then file data over socket

Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Hello

I need to transfer a file over a socket, what i want is to transfer the name of the file then the file data. i have a working server and client.

Server


Client


The file name is hardcoded in the server: FileOutputStream("E7060v1.2.zip"); i would really like this to be dynamic.

What is the best method to use when i need to transfer the file name (string) first then the file data (byte) ?

Kind regards Mads Nielsen
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

One approach is to use DataOutputStream on the client. You first use writeUTF to write the file name, then writeLong to write the file size (also very important).
The server then uses DataInputStream. It first uses readUTF to read the file name, then readLong to read the file size. Let's say that value is x. You then read exactly x bytes, not all until the end of the line:
Note that the while loop's condition has changed quite a bit. First of all, there's the additional check to see if the remaining size is not 0. Second, the read is limited to the minimum of the buffer length and the remaining size. For most loop iterations this size is the buffer length (and therefore identical to read(buffer)), but the last block(s) will only read as many bytes as necessary; size will be smaller than buffer.length (and therefore can safely be cast to int).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Rob Spoor wrote:One approach is to use DataOutputStream on the client. You first use writeUTF to write the file name, then writeLong to write the file size (also very important).
The server then uses DataInputStream. It first uses readUTF to read the file name, then readLong to read the file size. Let's say that value is x. You then read exactly x bytes, not all until the end of the line:
Note that the while loop's condition has changed quite a bit. First of all, there's the additional check to see if the remaining size is not 0. Second, the read is limited to the minimum of the buffer length and the remaining size. For most loop iterations this size is the buffer length (and therefore identical to read(buffer)), but the last block(s) will only read as many bytes as necessary; size will be smaller than buffer.length (and therefore can safely be cast to int).


Thank you very much for the server code. It helps a lot getting help on specific code.

I am unsure how to implement the DataOutputStream stream in the client code.?



Kind regards Mads Nielsen
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Mads Nielsen wrote:I am unsure how to implement the DataOutputStream stream in the client code.?

Also, there is a flaw in your reading code: This single read operation is not guaranteed to read all of the bytes in one go. You can again use DataInputStream for that:
Unlike a regular read, readFully is guaranteed to read exactly the requested number of bytes. It will throw an EOFException if there aren't enough bytes to read, but in this case that should not occur.
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Thank you so very much.

Do you know of a good site where i can read about the many different streams in Java ?

I have tried the java documentation but that is a little heavy.

Kind regards Mads Nielsen
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28


How do i connect InputStream in = clientSocket.getInputStream(); with in = new DataInputStream(in); ??

I get this error with the above code at compile time:



Kind regards Mads Nielsen
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Check the last piece of code in my previous post. I admit I made a mistake by not creating a new variable myself in my first post.
Mads Nielsen
Greenhorn

Joined: Aug 13, 2011
Posts: 28
Rob Spoor wrote:Check the last piece of code in my previous post. I admit I made a mistake by not creating a new variable myself in my first post.


I got that sorted now.

The server is working now.



Client


But i am now facing a little problem with the client code, it seems that it is never returning to the prompt. I have tried with dos.close(); but it did not help.

Kind regards Mads Nielsen
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19718
    
  20

Remove lines 30-32 from FileClient. You are now sending the file data twice, but the server is only expecting it once.

I tried your original code with a 200+MB file and it didn't finish. I then removed those three lines, tried again, and it finished just fine.
Mauricio Corrêa
Greenhorn

Joined: Feb 28, 2014
Posts: 1
Rob Spoor wrote:Check the last piece of code in my previous post. I admit I made a mistake by not creating a new variable myself in my first post.


For the server code to work correctly, you must close the variables: in and clientData

For example:






I noticed that the client is also necessary to close some variables.

For example:







I hope I have contributed.

Hugs...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Mauricio Corrêa wrote:I hope I have contributed...

Maybe so. However, I suspect that Elvis (or Mads) has left the room, considering the thread is more than 2 years old.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39385
    
  28
But … welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transferring file name then file data over socket