• Post Reply Bookmark Topic Watch Topic
  • New Topic

How this java client printed file contents perfectly without any knowledge of the file size?  RSS feed

 
Alysiah Equinox
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Screenshot of current results:

https://www.dropbox.com/s/iest2xo4azgyobx/aaaanewone.png?dl=0

Summary of screenshot:

Notice on the left window (server) of the screenshot it says "File Hi.txt sent with -1 bytes read." It's supposed to say "File Hi.txt sent with 19 bytes read", I would also like to know why this is broken but anyways the contents of the Hi.txt file are "sup Hi how are you" (see right window on screenshot) and the file size is 19 bytes (see left window on screenshot). The random numbers on the right window are print statements used for debugging/diagnostics. There are two threads with Datainputstreams for both the client and the server endlessly calling input.readUTF() and System.out.println(response) which allows for messages to be exchanged once the user types in m. However, once the user types in f, the file transfer process begins (see right window). In this case, after the line "Which file do you want?" (see right window) the client user types in the name of the file which is sent to the server, the server then gets the size of the file and sends it to the client on this line: output.writeLong( file_size ); However, the client executes System.out.println("flushed"); which is why you see the "flushed" output on the right window, but never executes this next line: long file_size= input.readLong(); and the next line after that would print the file size: System.out.println("The file size is: " + file_size); but it didn't (it printed the contents instead) so there's definitely a problem here.

TL;DR: I'm trying to transfer files, but in order to do that I have to send the file size ahead of the contents but input.readLong() on the client side doesn't work and I have tried flushing and I have tried input.readLine()/input.readUTF() (to receive strings, also casting/converting the size to string then sending it doesn't seem to work either for me but if it does then I would love to know how)

Pastebin of code that is sending the file size/sending the file (part of ServerRecieve.java):

https://pastebin.com/LaYyR5p5

Pastebin of code that is receiving the file / receiving the file size (part of Client.java):

https://pastebin.com/U77XUcRB


If you already know the answer by this point, SKIP reading the rest, scroll down and post your answer below, the rest of this is the full code which is a lot of lines.


Command line arguments to run Server:

java MessengerWithFiles -l 6001

Command line arguments to run Client:

java MessengerWithFiles -l 6002 -p 6001

Full code here:

https://www.dropbox.com/s/lk1o4aryxts5v2h/AMessengerWithFiles.zip?dl=0

Full code in pastebin format here:

MessengerWithFiles.java (main method):

https://pastebin.com/AaCrQxqL

ServerRecieve.java:

https://pastebin.com/DcHLLWUG

Server.java:

https://pastebin.com/AaCrQxqL

ClientRecieve.java:

https://pastebin.com/K2CHX7Nr

Client.java:

https://pastebin.com/aC1Nr8RK
 
Dave Tolls
Ranch Foreman
Posts: 3068
37
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the relevant code here, in code tags.
Many people won't (or can't, due to firewalls and security) go to the likes of dropbox or pastebin.

Can you do a really cutdown version of your code that simply sends a long across that connection and nothing else?
That would show if there's a general problem with how you are doing things, and it would also be a lot less code.
 
Alysiah Equinox
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:Please post the relevant code here, in code tags.
Many people won't (or can't, due to firewalls and security) go to the likes of dropbox or pastebin.

Can you do a really cutdown version of your code that simply sends a long across that connection and nothing else?
That would show if there's a general problem with how you are doing things, and it would also be a lot less code.


Sure here is the code that sends does the sending:

   


Here is the part that receives the file:

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alysiah Equinox wrote:Notice on the left window (server) of the screenshot it says "File Hi.txt sent with -1 bytes read." It's supposed to say "File Hi.txt sent with 19 bytes read", I would also like to know why this is broken


But your sender code doesn't calculate the size of the file being sent. The value you are printing is the size of the last buffer read by the sender, and that's always going to be -1 because the loop doesn't end until that value is seen. To compute the size of the file you'd need a different variable in which you sum up the sizes of all the buffers you send.
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also have far too much code, which makes it harder to debug. For example this block of code:



could be replaced by this:


 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And... you need to flush the OutputStream in your sending code after it sends the file contents. Otherwise the last buffer in the OutputStream just sits there waiting to be sent by some other action. (And close the FileInputStream as well.)

As for your receiving code, since you're sending the size of the file, the receiving code should just read that many bytes and then stop reading. Right now it just reads until there's no more data to read. And don't forget to close the FileOutputStream as well.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!