Results 1 to 7 of 7
  1. #1
    bockymurphy is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default File Transfer with Sockets

    Hi anybody like to tell me what I'm doing wrong here:
    I'm trying to transfer a file with sockets and set the socket with the relevant IP and Port number, but while the client seems to send all the data , the server receives all but the very last buffer (approximately). The file is not corrupted, and the client code executes fine without exception.

    Client side relevant code, which executes fine and exits the while loop:

    FileInputStream fileInputStream = new FileInputStream(file);

    byte[] buffer = new byte[socket.getSendBufferSize()];
    int read = 0;
    int readtotal = 0;

    while ((read = fileInputStream.read(buffer)) != -1) {
    readtotal = read + readtotal;
    System.out.println("Writing :" + read + ", Total written:" + readtotal);
    socket.getOutputStream().write(buffer, 0, read);
    }




    Server code is using ServerSocket to spawn a new thread, then

    InputStream inputStream = socket.getInputStream();
    OutputStream outputStream = socket.getOutputStream();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));

    FileOutputStream fileOutputStream = new FileOutputStream(file);
    byte[] buffer = new byte[socket.getReceiveBufferSize()];
    int read = 0;
    int readtotal = 0;
    while ((read = inputStream.read(buffer))!= -1) {
    readtotal = readtotal + read;
    System.out.println("Writing :" + read + ", Total written:" + readtotal);
    fileOutputStream.write(buffer, 0, read);
    }
    //Then I do some processing on the file here and close the i/o streams and socket but the program never exits the while loop, it reads up until about the last buffer size from the inputStream (according to the readTotal output) then nothing


    Any help appreciated....

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: File Transfer with Sockets

    Try having the client send the file size first and use that as the exit condition of the while loop on the server.

  3. #3
    bockymurphy is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: File Transfer with Sockets

    Yeah good idea, but i'm still dropping data some where as I've checked the file size on client versus file size on server...
    it should work, so that's what's puzzling me.. not clearing it up would leave a massive potential glitch..

    But thanks for your advice...I'll test that now

  4. #4
    bockymurphy is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: File Transfer with Sockets

    Tested... and it's still stalling on the "read = inputStream.read(buffer))!= -1" part
    The readtotal variable never gets to the original file size because it needs to be "read" first and then added to, and this will always happen after the inputStream.read() method.......


    So I think the -1 part is getting lost ...Any other suggestions ?

    Interestingly if I break off the client and try again (a few times), though i get an error.. I can break the while loop with
    if (readtotal == filesize) break;, .. Though its totally impractical to have to keep re uploading the file

    below is the output from the client and server.

    Client :

    Writing :49152, Total written:49152
    Writing :49152, Total written:98304
    Writing :49152, Total written:147456
    Writing :49152, Total written:196608
    Writing :49152, Total written:245760
    Writing :49152, Total written:294912
    Writing :49152, Total written:344064
    Writing :49152, Total written:393216
    Writing :49152, Total written:442368
    Writing :49152, Total written:491520
    Writing :49152, Total written:540672
    Writing :49152, Total written:589824
    Writing :49152, Total written:638976
    Writing :49152, Total written:688128
    Writing :49152, Total written:737280
    Writing :49152, Total written:786432
    Writing :49152, Total written:835584
    Writing :49152, Total written:884736
    Writing :49152, Total written:933888
    Writing :16328, Total written:950216
    Finished :950216

    Server:

    Writing :65536, Total written:65536
    Writing :65536, Total written:131072
    Writing :65536, Total written:196608
    Writing :50612, Total written:247220
    Writing :47692, Total written:294912
    Writing :49152, Total written:344064
    Writing :49152, Total written:393216
    Writing :49152, Total written:442368
    Writing :49152, Total written:491520
    Writing :49152, Total written:540672
    Writing :49152, Total written:589824
    Writing :49152, Total written:638976
    Writing :49152, Total written:688128
    Writing :49152, Total written:737280
    Writing :65536, Total written:802816
    Writing :65536, Total written:868352
    Writing :65536, Total written:933888
    Writing :16328, Total written:950216


    Note the file tally at the end is identical.......... baffled?

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default Re: File Transfer with Sockets

    Java Code:
    read = inputStream.read(buffer))!= -1
    It looks like that client never sends the -1. Try removing the check and just read the buffer.

  6. #6
    bockymurphy is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: File Transfer with Sockets

    I don't think you can do that as how else are you to know the end of the file, as (readTotal == filesize) is based on inputStream.read(buffer) being used to add read to the readtotal.. Any way I've tried what you suggested a few ways and there's no change.
    I've also changed port, changed IP, shutdown any servers running, turned off firewall, and tried to read the whole file into a byte[filesize] and send the whole thing in one go. I also tried with a simple txt file.. all these made no difference...

    Is this windows thing , or a netbeans glitch?

    Again thanks for the suggestions.. this is really bugging me now, as it's pretty standard java stuff

  7. #7
    bockymurphy is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: File Transfer with Sockets

    OK....

    Anybody fancy running this code and figuring out why the server threads while loop never exits???
    I slimmed it down to just the bare bones.....

    3 classes and create a .txt file and name it "UPLOAD_ME.txt" or just download the attached src.zip (3 java source files and "UPLOAD_ME.txt")

    Java Code:
    //////////////////////////////////////////////////////////////UPLOADER (CLIENT) CLASS - BOCKYMURPHY
    
    package uploadtest;
    
    import java.io.FileInputStream;
    import java.net.Socket;
    
    public class Uploader {
    
        Socket socket;
    
        public static void main(String[] args) {
            try {
               Socket socket = new Socket("127.0.0.1", 9000);
                FileInputStream fileInputStream = new FileInputStream("src/uploadTest/UPLOAD_ME.txt");
    
                byte[] buffer = new byte[socket.getSendBufferSize()];
                int read = 0;
                int readtotal = 0;
    
                while ((read = fileInputStream.read(buffer)) != -1) {
                    readtotal = read + readtotal;
                    System.out.println("Writing :" + read + ", Total written:" + readtotal);
                    socket.getOutputStream().write(buffer, 0, read);
                }
                socket.getOutputStream().flush();
               System.out.println("Client Finished :"+ read + ", Total written:" + readtotal);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    
    //////////////////////////////////////////////////////// END OF UPLOADER  (CLIENT) CLASS - BOCKYMURPHY
    
    /////////////////////////////////////////////////////UPLOAD CLASS (SERVER) - BOCKYMURPHY
    
    
    package uploadtest;
    
    import java.net.ServerSocket;
    
    public class UploadTest implements Runnable {
    
        private ServerSocket serverSocket = null;
        private boolean RUN_SERVER = true;
    
        public UploadTest() {
            System.out.println("Server: initialized:");
        }
    
        @Override
        public void run() {
            System.out.println("Server: running");
    
            try {
                serverSocket = new ServerSocket(9000);
    
                while (RUN_SERVER) {
                    new UploadTestThread(serverSocket.accept()).start();
                }
                serverSocket.close();
            } catch (Exception ex) {
            }
        }
    
        public static void main(String[] args) {
            new UploadTest().run();
        }
    }
    
    ///////////////////////////////////////////////END OF UPLOAD CLASS (SERVER) - BOCKYMURPHY
    
    //////////////////////////////////////////////////////UPLOAD THREAD CLASS (SERVER) - BOCKYMURPHY
    
    
    package uploadtest;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.Socket;
    
       public class UploadTestThread extends Thread{
    
            public Socket socket = null;
    
            public UploadTestThread(Socket accept) {
                this.socket = accept;
            }
    
        @Override
            public void run () {
                try {
                    InputStream inputStream = socket.getInputStream();
                    FileOutputStream fileOutputStream = new FileOutputStream(new File("src/uploadTest/I_VE_BEEN_UPLOADED.txt"));
                    byte[] buffer = new byte[socket.getReceiveBufferSize()];
                    int read = 0;
                    int readtotal = 0;
                    while ((read = inputStream.read(buffer)) != -1) {
                        readtotal = readtotal + read;
                        System.out.println("Writing :" + read + ", Total written:" + readtotal);
                        fileOutputStream.write(buffer, 0, read);
                    }
                    System.out.println("Server Finished :"+ read + ", Total written:" + readtotal);
                } catch (IOException ex) {
                }
            }
        }
     
    //////////////////////////////////////////////////////UPLOAD THREAD CLASS (SERVER) - BOCKYMURPHY
    Attached Files Attached Files

Similar Threads

  1. Replies: 6
    Last Post: 02-08-2011, 07:08 AM
  2. file transfer
    By prashant in forum Networking
    Replies: 3
    Last Post: 12-10-2009, 06:46 AM
  3. Regarding File Transfer in TCP/IP
    By manabendra in forum Networking
    Replies: 2
    Last Post: 08-07-2009, 07:51 AM
  4. tcp file transfer
    By ddj in forum Networking
    Replies: 2
    Last Post: 03-20-2009, 02:10 PM
  5. File and Message transfer over sockets!
    By rameshraj in forum Networking
    Replies: 3
    Last Post: 05-14-2008, 08:13 PM

Tags for this Thread

Posting Permissions

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