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???
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?
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:
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.
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..
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.
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.
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.
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.
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.
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad: