Results 1 to 10 of 10
  1. #1
    reza1990 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default How to Send Multiple File from Server to Client

    Hi..
    i am just a newbie here,
    i want send multiple file (ex.100 files) from server to client using loop.
    but the client side just get only 1 file and the connection direct to close.
    this is make me confused.

    plis help me :

    Server Code :
    Java Code:
    import java.net.*;
    import java.io.*;
    public class Server {
      public static void main (String [] args ) throws IOException {
          DataInputStream in;
         DataOutputStream out;
         int i =1;
        ServerSocket servsock = new ServerSocket(13267);
        while (true) {
          System.out.println("Waiting...");
          i++;
          Socket sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);
          File myFile = new File ("C:/Test/namafile-"+i+".txt");
          byte [] mybytearray  = new byte [(int)myFile.length()];
          FileInputStream fis = new FileInputStream(myFile);
          BufferedInputStream bis = new BufferedInputStream(fis);
          bis.read(mybytearray,0,mybytearray.length);
          OutputStream os = sock.getOutputStream();
          System.out.println("Sending...");
          os.write(mybytearray,0,mybytearray.length);
          os.flush(); 
          sock.close();
          }
        }
    }
    Client Code :
    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Client{
    
        public static void main(String[] args) throws UnknownHostException, IOException{
        int i=0;
            while(true){
        int filesize=6022386;
        DataOutputStream out = null;
        DataInputStream in =null;
        long start = System.currentTimeMillis();
        int bytesRead;
        int current = 0;
        Socket sock = new Socket("127.0.0.1",13267);
        System.out.println("Connecting...");
            try{
            i++;
            byte [] mybytearray  = new byte [filesize];
            InputStream is = sock.getInputStream();
            FileOutputStream fos = new FileOutputStream("C:/Hasil/"+i+"-");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray,0,mybytearray.length);
            current = bytesRead;
            do {
               bytesRead =
                  is.read(mybytearray, current, (mybytearray.length-current));
               if(bytesRead >= 0) current += bytesRead;
            } while(bytesRead > -1);
                    bos.write(mybytearray, 0 , current);
                    bos.flush();
                    long end = System.currentTimeMillis();
                    System.out.println(end-start);
                    bos.close();
                }
                catch(EOFException e){
                    e.printStackTrace();
                } 
            sock.close();
            }
    }
    }

    thanks for moderator..please help me to get client code can receive multiple file from server

    the client code can receive first file, and then exception show

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
    at java.lang.System.arraycopy(Native Method)
    at java.io.BufferedOutputStream.write(BufferedOutputS tream.java:111)
    at javasocket.Client.ClientSet(Client.java:55)
    at javasocket.Client.main(Client.java:72)
    Java Result: 1
    Last edited by reza1990; 08-01-2011 at 04:22 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Please edit your code and wrap it in code tags to preserve its formatting. Use the #icon above the input box.

    If you want anyone to compile and test your code, you will need to make a small simpler program that compiles and executes to demonstrate your problem. Your code is tied to DB stuff.
    Last edited by Norm; 07-31-2011 at 06:40 PM.

  3. #3
    reza1990 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    please help me... just tell me what should i do..

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    If you want anyone to compile and test your code, you will need to make a small simpler program that compiles and executes to demonstrate your problem.
    You need to remove ALL of the DB code and make the program as simple as possible.
    For example use an array as the source of the filenames to be sent.

  5. #5
    reza1990 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    You need to remove ALL of the DB code and make the program as simple as possible.
    For example use an array as the source of the filenames to be sent.
    i just make my code very simple now...please help me,
    i want to the client can receive multiple file from server..
    but the exception always show..
    ArrayIndexOutOfBound at the client code -> bos.write

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Your need to debug your code.
    Add some printlns to show the values of the variables as they change.
    For example:
    what is the value of current and bytesRead in the do{} while loop?
    How many bytes are read by each read() method call?
    Why do you always write the full array instead of the number of bytes read into the array?
    Add a println to show the name of each file you are writing to.

    Have you looked in the file(s) that were written to see what they contain?

  7. #7
    reza1990 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    This is debug from my code..

    Byte Read : 4
    namafile-1.txt : Current :5 BytesRead :-1 MyByteArray: [B@19821f
    Byte Read : -1
    namafile-2.txt : Current :-1 BytesRead :-1 MyByteArray: [B@190d11
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    Yes a -1 would cause ArrayIndexOutOfBoundsException.
    You need to fix the logic to test for the -1 and not use it as an index.

    Have you looked in the file(s) that were written to see what they contain?

  9. #9
    reza1990 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Norm, can you give me an advice, what should i do and change to my client code. So the Exception didn't show anymore.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,403
    Rep Power
    25

    Default

    For example, test the index value and do NOT use it if invalid:
    Java Code:
    if(arrayIndex a valid value) {
      // use the index in the array
    } // end if
    Have you looked at the line where the exception occurs?
    Where did the index get a value of -1? Why does it get that value? Read the API doc for the read() method.
    What should you do when it has a value of -1?

Similar Threads

  1. Replies: 8
    Last Post: 09-24-2010, 03:34 AM
  2. send file via client - server model
    By spasavvas in forum Networking
    Replies: 15
    Last Post: 08-13-2010, 11:45 AM
  3. send bytes from client to server
    By 0xHexaDecimal in forum Networking
    Replies: 41
    Last Post: 06-04-2010, 02:17 PM
  4. how to send mp3 file from server to client
    By Jigga008 in forum Networking
    Replies: 0
    Last Post: 12-01-2009, 12:02 PM
  5. Replies: 3
    Last Post: 07-29-2009, 04:52 AM

Posting Permissions

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