0

Im getting corrupted data over my Socket I don't really understand what the problem is. Im sending a voice clip that the user records when they let go it activated this method below in which it sends. when I look at the file sent its always a lot smaller than the original file I recorded.

UPDATED 22:00/23/03 **SENDING END

 public void sendVoice() throws IOException{


    System.out.println("send voice");
    Socket connection2 = new Socket(InetAddress.getByName("127.0.0.1"),1200);
    System.out.println(connection2);

    ObjectOutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
    output2.flush();
    ObjectInputStream  input2 = new ObjectInputStream(connection2.getInputStream());
     System.out.println(input2);


                DataOutputStream dos = null;
                try {
                    dos = new DataOutputStream(connection2.getOutputStream());
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }
        FileInputStream fis = null;
                try {
                    fis = new FileInputStream(voiceOutput);

                } catch (FileNotFoundException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);

                }
         // new       
        int count = 0;
        byte[] buffer = new byte[4096];

                try {
while ((count = fis.read(buffer)) > 0) {
                        dos.write(buffer,0,count); 
                        dos.flush();
                        System.out.println(fis);
                    }
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }

                try {

                    fis.close();
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }
                try {
                    dos.flush();
                    dos.close();

                  ////        
                } catch (IOException ex1) {
                    Logger.getLogger(chat.class.getName()).log(Level.SEVERE, null, ex1);
                }


}

RECEIVING END IS

 public void downloadFile() throws IOException {
           server2 = new ServerSocket(1200,100);    
        System.out.println("downloading file.........");
       Socket connection2 = new Socket();
     connection2 = server2.accept();

     ObjectOutputStream output2 = new ObjectOutputStream(connection2.getOutputStream());
     output2.flush();
     ObjectInputStream   input2 = new ObjectInputStream(connection2.getInputStream());

DataInputStream dis = new DataInputStream(connection2.getInputStream());

    FileOutputStream fos = new FileOutputStream("voiceClip.wav");

    byte[] buffer = new byte[4096];
   System.out.println(buffer.length);
    //int filesize = 15123; 
    int read = 0;

   //int remaining = filesize;

   while ((read = dis.read(buffer)) > 0)
{
    fos.write(buffer, 0, read);
}


    /*
    while((read = dis.read(buffer, 0, Math.min(buffer.length, remaining))) > 0) {
        totalRead += read;
        remaining -= read;
        System.out.println("read " + totalRead + " bytes.");
        fos.write(buffer, 0, read);
    }
*/
    fos.close();
    dis.close();

    }
Sleeking
  • 29
  • 1
  • 6

1 Answers1

0

Usual problem, ignoring the count returned by read(). Your copy loops should look like this:

while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

And don't flush inside loops.

EDIT And get rid of the object streams. You don't appear to be using them, and they send headers around, which waste bandwidth. And don't create a new Socket just before accepting one into the same variable. That's a resource leak.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thank you, Are you referring to the Sending end or the Receiving end I did change the Receiving end to this and noticed the bytes match up.. But the WAV file will still not play – Sleeking Mar 23 '16 at 04:01
  • I have updated my code, This still doesn't seem to work and is behaving the same way as before? – Sleeking Mar 23 '16 at 22:56
  • Where did you get the magic number 15123 from? Are the files the same length? – user207421 Mar 23 '16 at 22:59
  • The number is arbitrary and has no purpose I simply was just trying to test and hopefully something worked in which it hasn't, The number should be eliminated then I take it. – Sleeking Mar 23 '16 at 23:15
  • The number is being used to control your read size. If the file is longer it will eventually reduce the read size to zero, which will cause a short transfer. The only use of code like this with the `Math.min()` call is if you actually know the file size ahead of the transfer, as in the duplicate question. Otherwise you should just use the code in my answer. – user207421 Mar 23 '16 at 23:48
  • Alright I will give it a try, thanks for your reply – Sleeking Mar 23 '16 at 23:58
  • Ive updated my code again... I really don't understand what Im doing wrong I created a new connection because it was closing my current connection when I would try and send a voice clip from my chat messenger to the receiving end, I'm getting no errors on sending or receiving end sending end is printing out fine and showing that is infact connected to the host and on port 1200. All I'm trying to do is send a voice clip which gets sent to the method after its been created when user presses key and then releases to activate send " sendVoice() " – Sleeking Mar 24 '16 at 02:18
  • The code you have now posted should work subject to my edit. – user207421 Mar 24 '16 at 03:57
  • OK seems to be working when I run without my GUI just in main method with file pre-loaded... I do notice however that I keep getting Socket connection refused when I do try and run my GUI chat program and initiate the file transfer... I am running both on local host 127.0.0,1, Both being Socket 1 & 2 in the same program but am directing to different ports for example: PORT 1: 1234 & PORT 2: 1245 Could this be causing the infliction?? Anyways I thank you very much for helping me, felt real stuck for a while. – Sleeking Mar 24 '16 at 06:30
  • 'Connection refused' means the server isn't running. It is logically and temporally prior to the file transfer, so it can't be causing short transfers. – user207421 Mar 24 '16 at 09:44
  • Is there anyway to use the same socket and server to pass both text typed by user on chat screen as well as transferring files? my streams keep closing and if I open a new socket & connection and use the primary connection to write text to the chat application and send it to another user the second connection will say ' refused' always. I know the code is working because I have tested it in a non-gui and it works amazing, but it would be really nice to handle everything all in one – Sleeking Mar 25 '16 at 16:27