Page 1 of 2 12 LastLast
Results 1 to 20 of 25
Like Tree1Likes

Thread: Sending multiple files to client

  1. #1
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Sending multiple files to client

    Hello,

    I'm trying to send multiple files to the client through sockets. Sending the first file works perfectly, but follow-up files are messed up. Here is an abstract class which handles both file sending and receiving.

    Java Code:
    public abstract class FileTransfer {
    
        public static void sendFile(Socket socket, String filePath) {
            File file = new File(filePath);
            byte[] fileData = new byte[(int) file.length()];
            FileInputStream fis;
            BufferedInputStream bis;
            BufferedOutputStream toClient;
    
            try {
                toClient = new BufferedOutputStream(socket.getOutputStream());
            } catch (IOException ex) {
                System.out.println("Trying to send file to " + socket.getInetAddress() + " but client is disconnected");
                return;
            }
    
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
            } catch (FileNotFoundException ex) {
                System.out.println("Trying to send file but file not found");
                return;
            }
    
            try {
                toClient.write(toByteArray(fileData.length)); //write file size to buffer
                bis.read(fileData, 0, fileData.length); //write file data to array
                toClient.write(fileData, 0, fileData.length); //write array to buffer
                toClient.flush();
                
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    
        public static void receiveFile(Socket socket, String toPath) {
            InputStream fromServer;
            try {
                fromServer = socket.getInputStream();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                return;
            }
    
            FileOutputStream fos;
            BufferedOutputStream bos;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] intByte = new byte[4];
            int fileSize;
    
            try {
                fos = new FileOutputStream(toPath);
                bos = new BufferedOutputStream(fos);
                fromServer.read(intByte, 0, 4); //read size from server
                fileSize = toInt(intByte);
                System.out.println(fileSize);
    
                for (int i = 0; i < fileSize; i++) {
                    baos.write(fromServer.read()); //store received file-data to baos
                }
    
                bos.write(baos.toByteArray()); //write to file
                bos.flush();
                bos.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    
        private static int toInt(byte[] byteArr) {
            return ByteBuffer.wrap(byteArr).getInt();
        }
    
        private static byte[] toByteArray(int value) {
            return ByteBuffer.allocate(4).putInt(value).array();
        }
    }
    The file-size received from the second file is 192401739 when it should be 30412. The server sends the correct file size, but the client receives a false one. I have no idea why this happens.

    Thank you for your help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Sending multiple files to client

    I'm no pro at doing this, so please wait and see what other smarter folks suggest, but my suggestions include,
    • Are you printing out the sizes of the files from the sending code to see if the error occurs there?
    • For my money, I'd use NIO and channels to send and receive files. Have you considered looking into this?

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

    Default Re: Sending multiple files to client

    file-size received from the second file is 192401739 when it should be 30412.
    Can you explain the logic/protocol that your code uses to send multiple files?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    @Fubarable Yes I am printing the file sizes. Sending seems to work fine. I'll have a look at your suggestion if this doesn't work out. Thank you.

    @Norm I'm sending multiple files by calling the method multiple times. Like this:
    Java Code:
    FileTransfer.sendFile(clientSocket, "orange.jpg");
    FileTransfer.sendFile(clientSocket, "orange1.jpg");
    FileTransfer.sendFile(clientSocket, "orange2.jpg");
    And from the client size I'm receiving the files the same way:
    Java Code:
    FileTransfer.receiveFile(serverSocket, "orange.jpg");
    FileTransfer.receiveFile(serverSocket, "orange1.jpg");
    FileTransfer.receiveFile(serverSocket, "orange2.jpg");

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

    Default Re: Sending multiple files to client

    Can you explain the logic/protocol that your code uses to send multiple files?
    What bytes are sent and received? In What order are the bytes sent/received?

    What debug info is printed when the code executes?

    Can you post a small, complete program that compiles, executes and shows the problem?
    Last edited by Norm; 10-07-2012 at 03:46 PM.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    @Norm I'm sorry but I don't understand what you mean. For sending the file, I'm reading the file's data, store them in an array, then first send the size of the data and then the data itself. For receiving the file, I first read the size of the data, then read as many bytes as the size of the data and then save the data in the file.

    Since it's TCP, the data must be arriving at the correct order. A more basic idea on how I'm sending/receiving the files:
    - Send Size
    - Send Data
    - Read Size
    - Read Data

    I apologize if this is not what you meant.

    Edit: Ok I'll post a small program. Thank you for your help. Give me a minute
    Last edited by po105; 10-07-2012 at 03:55 PM.

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

    Default Re: Sending multiple files to client

    How many bytes are sent for the Size?
    What does the debug print outs for the sending side and receiving side show?

    For testing and debugging try sending small files that can be printed out to see where the problem is.
    What happens when you send several 20 byte files?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    Ok I've made a small program which acts as a server and as a client. The problem is, that this small program works perfectly. I will have another look at my original program to see what else could be causing the problem. If you have any ideas of anything that could be causing this behaviour please let me know. This is the small program if anyone wants it:
    Java Code:
    package Test;
    
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Test {
    
        static int port = 3000;
        static final int filesToSend = 5;
        
        public static void main(String[] args) {
            startServer();
            startClient();
        }
    
        public static void startServer() {
            Thread thread = new Thread() {
    
                @Override
                public void run() {
                    try {
                        ServerSocket serverSocket = new ServerSocket(port);
                        Socket clientSocket;
                        clientSocket = serverSocket.accept();
                        System.out.println("Server: Accepted connection from client");
                        for (int i = 0; i < filesToSend; i++) {
                            FileTransfer.sendFile(clientSocket, "orange.jpg");
                            System.out.println("Server: Sent file " + i);
                        }
                    } catch (IOException ex) {
                    }
                }
            };
            thread.start();
        }
    
        public static void startClient() {
            Socket serverSocket;
            try {
                serverSocket = new Socket("127.0.0.1", port);
                System.out.println("Client: Connected to server");
                    for (int i = 0; i < filesToSend; i++) {
                    FileTransfer.receiveFile(serverSocket, "orangeReceived" + i +".jpg");
                    System.out.println("Client: File " + i + " received");
                    }
            } catch (UnknownHostException ex) {
            } catch (IOException ex) {
            }
        }
    }

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Sending multiple files to client

    • Try the small program with larger files to be sure that it still works OK.
    • Keep adding functionality to the small program until either you can demonstrate the error, or it matches the current one in functionality but has no error.

  10. #10
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    The size of the files of the small program are of the same size of the original program. I'm going to try and debug my original program with smaller files as Norm suggested and post my results.

    Edit: I've found out what the problem is. I'm using a print writer to send the output path of the file to be received, in order to save the file at the correct location. This seemed to be causing all the trouble. This is what I'm using at the sending method:

    Java Code:
    sender = new PrintWriter(clientSocket.getOutputStream(), true);
    for (int i = 0; i < 3; i++) {
         sender.println("TestFile" + i + ".test");
         FileTransfer.sendFile(clientSocket, "TestFile.test");
    }
    And for receiving the files:

    Java Code:
    receiver = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
    for(int i = 0; i < 3; i++) {
          String toPath = receiver.readLine();
          FileTransfer.receiveFile(serverSocket, toPath);
    }
    Without sending the path of each file the data the client receives are 148, 148, 148 (1 is the size and 48 is the byte) which are correct. When I'm using the printwriter to send the path of the file just before sending it the data the client receives are 148, Read timed out,  0TestFile2.test (Including the space before the last file's data).

    Is it not possible to use a PrintWriter before sending/receiving the files? Or is there something else that might be causing this?

    Thanks again
    Last edited by po105; 10-07-2012 at 04:46 PM.

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

    Default Re: Sending multiple files to client

    . I'm using a print writer to send the output path
    Your post about the design/protocol for the program did not mention this.

    You needed to include ALL parts of the programs in the testing program that shows everything being written and read.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    I'm sorry I never thought that would be a problem; lesson learnt. This can be fixed by sending all the paths first and all the files later but I still don't understand why this was a problem. Could you explain please?

    Thank you very much

    Edit: Nevermind It can't be fixed. After a few times it gets messed up again (even with the test program). Is there a way to fix this?
    Last edited by po105; 10-07-2012 at 05:44 PM.

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

    Default Re: Sending multiple files to client

    Is there a way to fix this?
    Yes, I would think there would be.
    Post a small, complete program that compiles, executes and shows the problem.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    Here is the program. The class FileTransfer (posted in my first post) is required. An "orange.jpg" is also required in the working directory. This program sends 2 files twice (4 files will be sent in total). Each time a file's length is received it prints out its size. You can see that the first two sizes are correct while the other two are false.

    Java Code:
    package Test;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.net.ServerSocket;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Test {
    
        static int port = 3000;
        static final int filesToSend = 2;
        static final int timesToSend = 2;
    
        public static void main(String[] args) {
            startServer();
            startClient();
        }
    
        public static void startServer() {
            Thread thread = new Thread() {
    
                @Override
                public void run() {
                    try {
                        ServerSocket serverSocket = new ServerSocket(port);
                        Socket clientSocket;
                        clientSocket = serverSocket.accept();
                        PrintWriter sender = new PrintWriter(clientSocket.getOutputStream(), true);
                        for (int k = 0; k < timesToSend; k++) {
                            for (int i = 0; i < filesToSend; i++) {
                                sender.println("orangeReceived" + i + ".jpg"); //send output paths
                            }
                            for (int i = 0; i < filesToSend; i++) {
                                FileTransfer.sendFile(clientSocket, "orange.jpg"); //send files
                            }
                        }
                    } catch (IOException ex) {
                    }
                }
            };
            thread.start();
        }
    
        public static void startClient() {
            Socket serverSocket;
            try {
                serverSocket = new Socket("127.0.0.1", port);
                BufferedReader receiver = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
                String[] toPath = new String[filesToSend];
                for (int k = 0; k < timesToSend; k++) {
                    for (int i = 0; i < filesToSend; i++) {
                        toPath[i] = receiver.readLine(); //read output paths
                    }
                    for (int i = 0; i < filesToSend; i++) {
                        FileTransfer.receiveFile(serverSocket, toPath[i]); //read files
                    }
                }
            } catch (UnknownHostException ex) {
            } catch (IOException ex) {
            }
        }
    }
    Thanks again for your help

    Note: Even when I only send the 2 files once (and it works), if I re-run the program 3-4 times it will result in the same error.
    Last edited by po105; 10-07-2012 at 06:04 PM.

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

    Default Re: Sending multiple files to client

    Your test program is poorly designed. It writes over files, instead of creating new ones each time it writes out a file.

    There are some catch blocks without calls to printStackTrace().

    Does the program exit and the end of its execution?
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    The FileTransfer class writes over files because in my main program it's supposed to replace older cache.

    Do you mean if the program ends at the end of the main method? If so, no it doesn't end since there is an active thread. I'm using the 'Stop' button on net beans to terminate the program.

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

    Default Re: Sending multiple files to client

    The FileTransfer class writes over files
    For testing you need to see what happens every time. You can use a static int to generate a unique name for each file so you can see what the code is doing:
    Java Code:
        static int cntr = 0;      //  for unique filenames<<<<<<<<<<<<<
      .......
    
                fos = new FileOutputStream(toPath+cntr++ + ".rcvd.txt");    //<<<<<<<<<<<< destroys old


    If so, no it doesn't end
    It should end and exit. If it does not end then it is hung somewhere. Add more printlns to see what is happening.
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    I've tested more with some more printlns. I understand that only 2 files were being saved because the last 2 would overwrite the first two (fixed using a file ID as you mentioned above). Additionally I found out that it hungs when reading data of the third file but this is only because the file-size received is not correct (so it keeps reading, either for a long time or until there are enough bytes in the buffer to exit the loop). However I still haven't found out why the correct size isn't being sent for the last 2 files.

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

    Default Re: Sending multiple files to client

    why the correct size isn't being sent
    Can you post the debug prints that show what is received?
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    po105 is offline Member
    Join Date
    Oct 2012
    Posts
    12
    Rep Power
    0

    Default Re: Sending multiple files to client

    For a file with a size of 30KB the size received for the first two files is 30412 (Correct) and for the third file (where it hungs) is 388575532. I can't post the data because the file-size is too big, but for files with a size of 2 bytes, the size is never received for the third file. It hungs waiting for the size.
    Last edited by po105; 10-07-2012 at 08:16 PM.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. sending byte from server to C client
    By rnvrnv in forum Networking
    Replies: 11
    Last Post: 07-02-2010, 03:20 PM
  3. Sending array of data to client
    By k80sg in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 07:48 AM
  4. sending to client
    By rob in forum New To Java
    Replies: 1
    Last Post: 02-22-2009, 11:07 AM
  5. sending jar files from client to server?
    By gobinathm in forum New To Java
    Replies: 2
    Last Post: 11-13-2007, 05:12 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
  •