I have been messing with sockets and have run into a problem
transfering files. I can transfer files from client to server fine on
the same computer but when i move the client to a computer on the
network the server does not read the entire file contents.
here is the stream reading code:
DataInputStream din = new DataInputStream(sock.getInputStream());
long fileSize = Long.parseLong(din.readLine());
String fileName = din.readLine();
System.out.print("Filename : " + fileName + " " + fileSize + "\n");
FileOutputStream file = new FileOutputStream(new File("out/" +
fileName));
int c = 0;
try{
for(int i = 0; i < fileSize;i++){
c = din.read();
file.write(c);
}
System.out.print("File Received\n");
file.close();
this code get to the "File received" line on the same machine as the
server but hangs in the for loop idle on a different client machine.
I am not including the sending routine as is seems to send fine on all
machines Its this receiving routing causing the trouble.
for the loop info it gets to about its 1200 iteration while it should
iterate through about 4000 times.
Is there a buffer problem? If so how would i fix it?
Thanks a lot,
Ricky
Heiner Kücker - 09 Dec 2005 05:50 GMT
>I have been messing with sockets and have run into a problem
> transfering files. I can transfer files from client to server fine on
[quoted text clipped - 26 lines]
>
> Is there a buffer problem? If so how would i fix it?
Try on server side
outputStream.flush();
or
outputStream.close()
if the socket no longer in use

Signature
www.heinerkuecker.de
rivera - 09 Dec 2005 06:11 GMT
Perfect, thank you very much..
Perhaps a little explanation?
So there is a buffer that reads a certain amount of data from the stream and
that is all that was being read? flushing it then puts the entire file in
the buffer to be read?
>>I have been messing with sockets and have run into a problem
>> transfering files. I can transfer files from client to server fine on
[quoted text clipped - 36 lines]
>
> if the socket no longer in use
Roedy Green - 09 Dec 2005 07:03 GMT
>I have been messing with sockets and have run into a problem
>transfering files. I can transfer files from client to server fine on
>the same computer but when i move the client to a computer on the
>network the server does not read the entire file contents.
see http://mindprod.com/products1.html#FILETRANSFER

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
rivera - 10 Dec 2005 04:01 GMT
Ok actually flushing did not fix it. I fixed it by reading 256byte clusters
from the file until returning -1. I think the problem was i was sending too
much data over the socket.
Maybe this will help someone else.
Ricky