Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

File Transfer through Sockets Help

Thread view: 
RickyMyrna@gmail.com - 09 Dec 2005 05:15 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.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.