Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Question Java Socket File Transfer

    Well, I'll start by saying that I am a student that is attempting to study all of this in my free time. I have a class covering peer 2 peer networking, and the professor will not be using the class time to help students get started. Rather, he is just jumping right into breaking down all the protocols that go into a bitTorrent client, which i am completely okay with.

    Anyways, for this class I must create a P2P file transfer client. Right now I have very little code, as most of it has just been little tests here and there. I have managed to get a working socket in which I can open a socket and transfer text. The issue is that i need to transfer large files. I have looked around and I see a lot of people using byte streams(including past threads on this site) to do so. The issue that many seem to run into is sending large files. I'm not sure if I have to break the file up in order to ensure it is sent properly.

    FileInputStream
    I have seen this used but have yet to learn exactly how to use it. In a thread I read that someone was having issues with the stream only sending around 1400 bytes and then truncating the rest.



    Side note: I am only a senior and I have no experience with implementing protocols

    Example of how far I have come

    Java Code:
    public class client {
        public static void main(String[] args){
            
            try{
                Socket mySkt = new Socket("localhost",7777);
                System.out.println("Connected to host");
                String reqFile = "C:\\Users\\ME\\Pictures\\Art\\jimmies.jpg";
                PrintStream reqStream = new PrintStream(mySkt.getOutputStream());
                reqStream.print(reqFile);
                     if(reqStream.checkError()){
                         System.err.print("There was an error in making request");
                     }
                System.out.println("Transmission Complete");
                mySkt.close();
            }
            catch(IOException ex){
                System.out.println("Something went wrong");
            }
        }
    }
    Java Code:
    public class Server {
        public static void main(String[] args){ 
            try{
                ServerSocket mySocket = new ServerSocket(7777);
                Socket mySkt = messageSocket.accept(); 
                System.out.println("Client Connected");
    
                //get request for file 
                BufferedReader inComStream = new BufferedReader(new InputStreamReader(messageSkt.getInputStream()));
                String requestedFile = inComStream.readLine();
                System.out.println(requestedFile);
                      if(requestedFile == null){
                         System.out.println("Nothing was recieved");
                     }
                File selectedFile = new File (requestedFile);
                System.out.println((int)selectedFile.length());
                }catch(IOException ex){
                System.out.println("Something went wrong");
                }
    Last edited by TerminatingZero; 09-05-2012 at 09:29 PM.

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

    Default Re: Java Socket File Transfer

    When sending a file, you don't have to read it all and send it all at once. You can read some and send some and then read some more and send some more in a loop until all of the file has been read and sent. Pick a reasonable size (4k???) to read and send and then use a loop to read and send the file in chunks.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Socket File Transfer

    Thank you for the response. I just successfully sent a full text file through a stream. I have not accomplished breaking the byte stream up. I sent the whole file as one chunk. At least for now that is some progress. But, now I am looking into breaking up file and I am running into issues. I believe that I am keeping track of the offset properly. I believe the is now is how to synchronize the reads and writes. I am now trying to get notify() and wait() commands to work, but I am getting a exception that says that the waiting thread is not in a proper state.

    Issues with my file transfer:
    The file did not send 100% properly. After the file was received it needed to be "repaired" by the document launcher(OpenOffice). I closed the file stream before terminating the processes. So, I am unsure what is causing the error, but the file restores properly.

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

    Default Re: Java Socket File Transfer

    The technique I use to test my logic for copying files is to use a simple input file that is easily checked for mistakes.
    Create an array of bytes with values from 0 to 255 and put it in a ByteArrayInputStream.
    Create a ByteArrayOutputStream to write to.
    Create a small byte buffer to read into from the BAIS and write to the BAOS
    Make a loop that reads the into byte buffer and writes to the BAOS.
    After all the bytes are copied, compare the array in the BAIS to the array in the BAOS.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Socket File Transfer

    Here is a look at my code now. I keep getting a peer write error. I'm assuming it is because I am not syncing the read and writes.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    /**
     *
     * @author Dave
     */
    public class clientMain {
        public static void main(String[] args){
            try{
                Socket mySkt = new Socket("localhost",7777);
                System.out.println("Connected to server!");
                File myFile = new File("C:\\Users\\Me\\Desktop\\practiceWork\\myFile.png");
                
                if(!myFile.exists()){//check if file exists
                    myFile.delete();
                    myFile.createNewFile();
                }else{
                    System.out.println("This File exists");
                }
                
                System.out.println("New file has been created!");
                FileOutputStream myFileOut = new FileOutputStream(myFile);
                InputStream myInStream = mySkt.getInputStream();
                
                 byte[] buffer = new byte[1024];
    
                int bytesReceived = 0;
                int totalReceived =0;
                
                while(myInStream.available()>0) {   
                    bytesReceived = myInStream.read(buffer);
                    myFileOut.write(buffer);
                    
                }
                System.out.println(myInStream.available());
                myFileOut.close();
               
            }catch(IOException ex){
                ex.printStackTrace();
                System.out.println("Something went wrong");
            }
        }
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    /**
     *
     * @author Dave
     */
    public class serverMain {
        public static void main(String[] args){
            try{
                ServerSocket skt = new ServerSocket(7777);
                Socket mySkt = skt.accept();
                
                File myfile = new File("C:\\Users\\Me\\Pictures\\4qKtv.png");
                OutputStream myOutStream = mySkt.getOutputStream();
                FileInputStream myFileIn = new FileInputStream(myfile);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                int totalRead= 0;
                while(myFileIn.available()>0)
                {
                    bytesRead = myFileIn.read(buffer);
                    myOutStream.write(buffer);
                    totalRead = totalRead + bytesRead;
                    bytesRead =0;
                    System.out.println(totalRead);
                }
                myFileIn.close();        
                
            }catch(IOException ex){
                ex.printStackTrace();
                System.out.println("Something went wrong.");
            }
        }
    }

  6. #6
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Java Socket File Transfer

    Quote Originally Posted by TerminatingZero View Post
    Here is a look at my code now. I keep getting a peer write error. I'm assuming it is because I am not syncing the read and writes.

    Java Code:
    import java.io.*;
    import java.net.*;
    
    /**
     *
     * @author Dave
     */
    public class clientMain {
        public static void main(String[] args){
            try{
                Socket mySkt = new Socket("localhost",7777);
                System.out.println("Connected to server!");
                File myFile = new File("C:\\Users\\Me\\Desktop\\practiceWork\\myFile.png");
                
                if(!myFile.exists()){//check if file exists
                    myFile.delete();
                    myFile.createNewFile();
                }else{
                    System.out.println("This File exists");
                }
                
                System.out.println("New file has been created!");
                FileOutputStream myFileOut = new FileOutputStream(myFile);
                InputStream myInStream = mySkt.getInputStream();
                
                 byte[] buffer = new byte[1024];
    
                int bytesReceived = 0;
                int totalReceived =0;
                
                while(myInStream.available()>0) {   
                    bytesReceived = myInStream.read(buffer);
                    myFileOut.write(buffer);
                    
                }
                System.out.println(myInStream.available());
                myFileOut.close();
               
            }catch(IOException ex){
                ex.printStackTrace();
                System.out.println("Something went wrong");
            }
        }
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    /**
     *
     * @author Dave
     */
    public class serverMain {
        public static void main(String[] args){
            try{
                ServerSocket skt = new ServerSocket(7777);
                Socket mySkt = skt.accept();
                
                File myfile = new File("C:\\Users\\Me\\Pictures\\4qKtv.png");
                OutputStream myOutStream = mySkt.getOutputStream();
                FileInputStream myFileIn = new FileInputStream(myfile);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                int totalRead= 0;
                while(myFileIn.available()>0)
                {
                    bytesRead = myFileIn.read(buffer);
                    myOutStream.write(buffer);
                    totalRead = totalRead + bytesRead;
                    bytesRead =0;
                    System.out.println(totalRead);
                }
                myFileIn.close();        
                
            }catch(IOException ex){
                ex.printStackTrace();
                System.out.println("Something went wrong.");
            }
        }
    }

    Why are you writing the whole of a buffer rather than the number of bytes actually read?

    available() does not do what you seem to think it does. It return the number of bytes that can be read without blocking and can return zero especially on sockets. Rather than
    Java Code:
    while(myInStream.available()>0) {
    just use
    Java Code:
    while(true) {
    and break when the number of bytes read is -1.

  7. #7
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Socket File Transfer

    Okay, I made some changes. I did not know that available() was some what volatile and more of an estimate. I have tried the conditional that you suggested and that does not seem to work. But I did find out that throughout all of this the issue is that I am reaching the end of the stream and not terminating. So all I need to do is fix that conditional and ,well, then I can leave this alone for the time being.

    Scratch That
    The transmission works for now.
    Last edited by TerminatingZero; 09-11-2012 at 09:39 AM.

  8. #8
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Java Socket File Transfer

    Quote Originally Posted by TerminatingZero View Post
    Okay, I made some changes. I did not know that available() was some what volatile and more of an estimate. I have tried the conditional that you suggested and that does not seem to work. But I did find out that throughout all of this the issue is that I am reaching the end of the stream and not terminating. So all I need to do is fix that conditional and ,well, then I can leave this alone for the time being.

    Scratch That
    The transmission works for now.
    Have you fix the very serious bug where you don't write out only the actual number of bytes read?

  9. #9
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Socket File Transfer

    If I understand you quest correctly, yes. I altered the write so that it work as shown below

    Java Code:
    myFileOut.write(buffer,0,bytesReceived);
    Where bytesReceived controls the number of bytes to be written. I don't know why I altered that segment of code.

Similar Threads

  1. Transfer file with Socket class
    By Pojahn_M in forum Networking
    Replies: 6
    Last Post: 02-02-2012, 01:38 AM
  2. Replies: 6
    Last Post: 02-08-2011, 07:08 AM
  3. pls..transfer data in socket
    By langkhach_codon in forum Networking
    Replies: 7
    Last Post: 03-28-2010, 05:21 AM
  4. Replies: 5
    Last Post: 11-01-2009, 05:16 PM
  5. Java File Transfer
    By lks0912 in forum Advanced Java
    Replies: 0
    Last Post: 10-23-2008, 12:53 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
  •