sending a file

I have to send a file over a network but I have looked at so many examples that are similar to what I am doing but not quite that I have become thoroughly confused!

Can anyone tell me what classes I need to use? I was using the following code:

FileOutputStream fileout =new FileOutputStream(f);

DataOutputStream dataout =new DataOutputStream(socket.getOutputStream());

dataout.write(reader);

But now I am not sure that I am going about it the right way. Could anyone help me this please?

[593 byte] By [nashera] at [2007-11-26 20:38:23]
# 1
I HAVE SOLVED THIS NOW.
nashera at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 2

I also need to send a file over a network.

The server is able to read all the control messages sent by the client. For this purpose, it is using

BufferedReader inFromServer ;

inFromServer.readLine();

However when the client sends the actual file, the server uses

DataInputStream inFromServerDI;

int bytesRead = 0;

byte[] fileContent = new byte[1000];

while(bytesRead < fileSize)

{

int numBytes = inFromServerDI.read(filecontent, 0, 1000)

if(numBytes == -1) break;

// write out the numBytes in fileContent to a file

}

For some reason, the read call blocks for ever. My client properly sends the file but the server is not getting it. The control messages are getting across so I cant seem to understand whu the server hangs when the actual file is coming through the socket. I cannot use readUTF or readLine for the file since the file could be any file. not neccessarily ASCII.

}

aybhavea at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 3
The reason will be that some of the data is already in the BufferedReader. Get rid of it and use DataInputStream.readLine(), and use the same DataInputStream for reading the binary data.And yes I know it's deprecated ...
ejpa at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 4

Thanks very much for your reply.

I dont think that could be a problem. My message structure sent by client is as follows

Backup

Sending File

FIle Name = test1.txt

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx // the actual file

As can be seen, each header is an ASCII string followed by a carriage return and line feed. My server is getting the "File Name = test1.txt" message properly (Im using the BufferedReader.readLine() for this). Since readLine returns on encountering /r/n, the file is not getting buffered. I switch to DataInputStream.read() ony for reading the file.

Secondly, the funny part is that the code works perfectly on another computer when both server and client are on the same machine. It doesnt work if they are on different machines. On my machine, it doesnt work either way :(....I do have the latest Java compiler and JRE (1.5 to be exact).

Any help would be greatly appreciated

Thanks

aybhavea at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 5
> Since readLine returns on encountering /r/n,> the file is not getting buffered.The BufferedReader fills its buffer with as much data is available whenever it needs data. So the file almost certainly is being buffered. As I said. Did you try my suggestion?
ejpa at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 6
Ok I was mistaken :) and I am very happy about that. I tried DataInputStream.readLine and it works!! Thanks for the suggestion.Can you please tell me why this is so (I have given my explanation in the prev post)
aybhavea at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 7

Didn't I already do that? And doesn't the result prove the point?

Obviously it is DataInputStream.readLine() that behaves the way you said, not BufferedReader.readLine(). DataInputStream is not a buffered stream so it has no buffer so it does no buffering.

If BufferedReader didn't have a buffer, or didn't fill it, it wouldn't be a buffered reader, would it?

ejpa at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 8

Oh sorry, I typed in my reply before the page refreshed and your reply showed up.

DataInputStream provides a very useful class to read bytes directly from a stream (in my case the TCP receive buffer). If it is so useful any ideas on why they have made it deprcated? Is there another class in Java that provides the convenience of a readLine() type semantic yet always talks directly to the TCP receive buffer instead of buffering in the application layer?

aybhavea at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 9

If BufferedReader buffers bytes into the application layer, can you show me some example code on how it is supoosed to be used the right way. I suspect it goes something like this

while(true)

{

BufferedReader.readLine();

while(application layer buffer contains something)

{

extract from buffer a few bytes

process the bytes

}

}

Can you please give me the classes and methods that are to be used? This is just for interest sake. Im gonna use DataInputStream from now on.

Many thanx for your help.

aybhavea at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...
# 10

Whatever input stream or reader you are using, you can only sensibly use one per data source, unless you know that all the readers and input streams you're using do not buffer.

So if you want to use BufferedReader, use whichever of its methods apply. But if you're using a Reader to read binary data you're embedded in a self-contradiction, to which the only solution is 'don't'.

The code would be more like this:

while (true)

{

String line = reader.readLine();

if (line == null) break; // EOF

int length = getLength(line); // some function to parse the line

char[] buffer = new char[length];

int count = reader.read(buffer);

if (count < 0) break; // EOF

// etc

}

noting that there is no test for when the reader's buffer is empty other than getting an EOF condition.

Because of the paucity of its methods, BufferedReader isn't good for much except reading lines and individual chars. Contrast DataInputStream ...

ejpa at 2007-7-10 1:55:10 > top of Java-index,Core,Core APIs...