Problem while transferring File Contents.........
Hi there..
I'm trying to send a file from a Client to the Server over Secured Sockets.In fact I have read a .jar file on the client machine,stored the contents in a String variable sent the string to Server side.The problem I'm having is that the file contents are not being transferred completely,,on client side the file size is 189KB whereas it reduces to 187KB on the Server Side.I'm using the BufferedReader.readLine() method and I feel the empty content in my Client Side file isn't being sent,,,can anybody plz help?
Thanks in advance...
# 1
If you use readLine() you are assuming that the file is 100% text. Is that the case?Otherwise you should use write(byte[], int offset, int count) and read(byte[]).
ejpa at 2007-7-9 21:04:41 >

# 2
Thanks but can you send me some sample code as I'm not getting which class's write method to use on the Client side and how to read on the Server side....
# 3
I note that you found BufferedReader.readLine() somehow. http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html http://java.sun.com/j2se/1.5.0/docs/api/java/io/OutputStream.html
ejpa at 2007-7-9 21:04:41 >

# 4
Thanks,,I have managed to transfer all the file contents to Server where they are being shown in my IDE Console as they had been sent but I have got another problem here,,when I try to write the received content into a file,,,the file stays empty,,it doesn't show any data,,I'm receiving the content as String and tring a PrintStream object to write to the file,,,,,,can you plz help here?
# 5
If you transfer the file as a String you are assuming it is 100% text. Is that the case? I already asked you that, where's the answer?
Are you (a) closing or (b) flushing the output stream?
Actually it sounds like you aren't writing anything at all to it. But without seeing your code it's just a guessing game.
ejpa at 2007-7-9 21:04:41 >

# 6
I'm not transferring as String,,(a)I'm flushing it (b)not closing it,,,
here's my code,,
CLIENT SIDE
Writer out = new OutputStreamWriter(socket.getOutputStream());
out.write("GET http://" + host + "/ HTTP/1.1\r\n");
out.write(fileName);
out.write("\r\n");
try {
InputStream iStream = new FileInputStream(fileLocation);
BufferedReader in = new SafeBufferedReader(new InputStreamReader(iStream));
char[] buf = new char[1024];
int len;
while ((len = in.read(buf)) >
0)
{
out.write(buf, 0, len);
}
in.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
out.write("\r\n");
out.flush();
SERVER SIDE
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
String fromServer = null;
int flag=0;
while ((fromServer = bufferedreader.readLine()) != null)
{
FileOutputStream fos=new FileOutPutStream("C:/Test.txt");
PrintStream ps=new PrintStream(fos);
ps.println(fromServer);
}
I'm not getting any data in my file on the Server Side.
hope you'll respond....
# 7
OK, I've thought about this some more. I have several questions.
1. I've asked you twice whether the file is 100% text. Are you ever going to answer this question?
2. Or don't you think that's important?
3. If not, why not?
4. I advised you not to use readLine(), and to use read() and write() in reply #1, and gave you links to them in reply #3. So why are you still using BufferedReader.readLine()?
5. What is a SafeBufferedReader?
6. Why aren't you closing the output stream when you send the file?
7. Why are you appending \r\n to the data you read from the socket?
8. Why are you creating a new file every time you read a new line?
9. Why are you asking for advice and then ignoring all of it?
10. How can you rationally expect anyone to help you when you do all that?
ejpa at 2007-7-9 21:04:41 >

# 8
Don't be angry my friend,,,I'm answering your questions,,,I thought my code would've been enough for you....
1-No it isn't 100% text
4-In fact I've set few flags in my Server Side code to be able to differentiate between file name and file data being sent from the Client side,,
5- SaffeBuffeed reader is a cla4ss in com.macfaq.io package
6-Yeh I didn't notice,,I shud close it.
7-\r\n is being used as a flag,,when one type of data has been sent,,,I send \r\n,,,like,,
(a) file name (so that I'm able to create a file with the same name on the server side,,
(b)file data
8- That's again a mistake,,,shudn't have done it.
Please cool down my friend,,,you can ignore to reply if you still feel the information isn't enough...
thanks...
# 9
Hello ejp...Finally I have managed it,,,,I was perhaps not reading the data properly,,shudn't have used readLine(),,,now it's working,,thanks my friend,,,,,
# 10
Hi there,Can you please let me know how you have managed to make this work without using the readline.Please post a sample code!!Regards,
Halaa at 2007-7-9 21:04:41 >

# 11
If his file isn't 100% text he shouldn't be using Readers and Writers at all. Personally I would use DataInputStream and DataOutputStream for all the I/O in this protocol (or practically any protocol, for that matter). I would use readUTF() and writeUTF() for the commands and read(byte[]) and write(byte[], offset, length) for the binary data.
And I wouldn't touch SafeBufferedReader with a bargepole. From a quick review it is considerably less safe than BufferedReader.readLine() and DataInputStream.readLine().
The JDK is good enough for me. I can't see the point in taking risks with packages like com.macfaq.io.* which are neither commercially supported nor open-source and about which the author makes dubious claims.
ejpa at 2007-7-9 21:04:41 >
