Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Extra bytes being read into file.

 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Iam using sockets to transfer a file from one system to another...The file is being sent correctly but at the receiving end the file being received is of larger size than the one i'm sending...why is this happening and what can i do to rectify this error???
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hard to guess. If you're using any Readers or Writers, it could be a character encoding problem, or a line break transformation if you're using readLine(). Could you show the code you're using to read from the file and write to the socket?
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what i did...

At the sender side :



At the receiver side:

 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the biggest problem by far is that call to println(). The ln is an old abbreviation for "line". Every time you call it, you're printing one character followed by a line terminator (which is either one or two bytes long, depending on your operating system).

So your file is probably two or three times as big as it should be, with all those extra terminators. Additionally, there are several other possible complications from the various other classes you're using. You're mixing up Readers, Writers, InputStreams and OutputStreams in a rather haphazard fashion. Without getting into all the details of how each of these classes work, the solution is somewhat simpler than what you've got. You only need one InputStream and one OutputStream on the server, and one of each on the client. The Readers and Writers offer no extra features that you need. So:

And the same on the client, but with

Additionally, you can make the copying code considerably more efficient by reading and writing an array of bytes at once:
 
Scott Selikoff
author
Bartender
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"tr udhu"

Please re-read the naming policy and change your name to something less fictiuous. In particular, your firstname (and possibly your last name) do not appear to be valid words. You have been previously warned about your name.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,I tried what you suggested...and yes using the println was a mistake and when i used simple Input and Output Streams,the file was being transferred but still I have on junk at the receiver side.The file that I sent was of 5 KB but I received 6KB (earlier it was 17KB for 5KB).
I even executed the file transfer part of the code independently to check if it works and I found that when the code run independently the file is being sent perfectly but when I include the same in my program..I'm not getting the correct size.
I am including here my server-side and client-side of the code...See if you can give me suggestions or point out mistakes if any..

 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And this is the server side working of the peer :
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand it's completely unreasonable to expect someone to go through this huge amount of code and point out mistakes or give out suggestions.But since I have drained out my possible ways in the past week..i'm now turning to javaranch members as a last resort to solve this. (Iam not posting the entire code since that is too big.)

All the handshake process is going fine...but when it comes to file transfer part there's some problem. I even tried flushing the streams before sending out the file..even that did not help.Both the server-side and client-side code are executed as a thread.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I overlooked the second big problem here - calling print(i) when i is an int will print out a the characters in a numeric value, not raw bytes. That is, if for example you try to print the character 'a', it will print "97", which is the ascii value of 'a'. So outPeerReq.print(i) is not what you want. A quick fix would be to replace it with outPeerReq.print((char) i) or outPeerReq.write(i). That would get rid of the most serious current problem, but may leave other more subtle problems.

A better solution would be to, once again, get rid of Reader and Writer classes entirely when the objective is simply to copy bytes. An InputStream and an OutputStream are all you need. Using classes like InputStreamReader or a PrintWriter wrapped around an OutputStream just creates opportunities for further character encoding issues here.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim, if I had any character encoding problems then the file transfer code should have failed when i run it independently but that's not the case here. When I run a program having only the file transfer part of my program then the file received is the exact one I send.
I also tried using simple input and output streams...when there was no change in output...I reverted back to previous code(using readers and writers),that too after checking if it works.
I would gladly appreciate any other critical analysis.
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have working file-copying code. Why don't you just have a "copyFile" method in that code that does exactly what works, and just call that method? Then you wouldn't have to concern yourself with the rest of the code. If you have to post a large amount of code to discuss a simple-sounding problem like this, that means your code isn't modular enough.
 
tim jones
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help...but the problem is not with the file-copying code that I had written...it was in fact in the client program above where it doesn't read a string value "Authenticated" sent by server side. When I corrected that error the file being sent is fine.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!