• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transferring file name then file data over socket

 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Mads Nielsen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20659
64
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But … welcome to the Ranch
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic