Results 1 to 13 of 13
  1. #1
    amjad.88 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default how to send any file size using sockets

    hi to all,
    i am creating a server client app that will send files in local network
    using sockets but the problem is the size of the byte array in server and client what shoud i do to fix this. thx:)

  2. #2
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Split file into chunks, first send size or number of chunks. Then send/read chunks to/from socket and save them somewhere.

  3. #3
    amjad.88 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Thx ProjectKaiser

    i know what u r talking about but can u plx send me an example thx

  4. #4
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

  5. #5
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Also small example how to send objects via socket
    Java Socket Programming Example

  6. #6
    amjad.88 is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    it is no clear on how to chunk any type of files !

  7. #7
    Join Date
    Aug 2008
    Location
    Saint-Petersburg, Russia
    Posts
    66
    Rep Power
    0

    Default

    Example how to copy file by 1024 bytes chunks
    Java: copying files over TCP/IP

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Briefly, you need an input stream of some sort to retrieve the file data, and an output stream belonging to the socket. Read 1000 bytes from the file, write 1000 bytes to the socket. You will need to add some logic to signal the total bytes sent in each chunk and end of file.

    ProjectKaiser is right in saying "Read the tutorial". This isn't hard, but you can't just hack some code together expect it to work...

  9. #9
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
      String fileName = "theFile";
    
      byte[] bytes = new byte[65536];
    
      File file = new File(fileName);
    
      FileInputStream in = new FileInputStream(file);
    
      OutputStream out = socket.getOutputStream();
    
      DataOutputStream dout = new DataOutputStream(out);
    
      //First send the file name
    
      dout.writeUTF(fileName);
    
      //now send the length (as a simple long)
    
      long numToSend = file.length();
    
      dout.writeLong(numToSend);
    
      //now send the file contents
    
      long numSent = 0;
    
      while(numSent < numToSend) {
         long numThisTime = numToSend - numSent;
    
         numThisTime = numThisTime < bytes.length ? numThisTime : bytes.length;
    
         int numRead = in.read(bytes, 0, (int) numThisTime);
    
         if(numRead ==-1 ) break;
    
         dout.write(bytes,0,numRead);
    
         numSent += numRead;
      }
    
      ...
    On the client side, do the inverse. First do a DataInputStream.readUTF to get the file name, then a DataInputStream.readLong to get the expected size, followed by a loop that reads that amount of data, very similar to the one here.

  10. #10
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    This isn't hard, but you can't just hack some code together expect it to work...
    Well, OK. Toadaly can...

  11. #11
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    ...heh, part of the project I'm curretnly working involves a file mirror. This stuff is fresh on my mind.

  12. #12
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default hi,,, all

    i am newly joined member, want to explore the java.net package. any one specify the good web site.......

  13. #13
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Well, rather obviously, start here: java.net (Java Platform SE 6). There's nothing magic about sockets. What were you interested in?

Similar Threads

  1. Replies: 7
    Last Post: 10-28-2008, 02:41 PM
  2. File and Message transfer over sockets!
    By rameshraj in forum Networking
    Replies: 3
    Last Post: 05-14-2008, 09:13 PM
  3. how to send files through sockets
    By gabriel in forum Advanced Java
    Replies: 3
    Last Post: 01-12-2008, 09:10 AM
  4. File size
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 10:27 AM
  5. Problems sending file throught TCP sockets
    By Nite in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2007, 10:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •