This discussion is archived
11 Replies Latest reply: Apr 27, 2011 11:40 AM by captfoss RSS

Transfer Audio Input Stream with Socket

834115 Newbie
Currently Being Moderated
Hi Everyone!
I have a program where i hide in a wav file a message.
I hide it by getting the bytes of the AudioInputStream of a wav file and adding at the end a message.
Now I want to send the new wav file ,with the message hidden in it, through a socket. Im able to send the byte array with the bytes of the wav file and the hidden message but when I save it as a wav file Im unable to retrieve the message (which Im able to do with the original file that I want to send).

Any thoughts why it doesnt work?

Thanks in Advance
  • 1. Re: Transfer Audio Input Stream with Socket
    captfoss Pro
    Currently Being Moderated
    831112 wrote:
    Any thoughts why it doesnt work?
    You didn't do something correctly, presumably... but you've not even made it remotely clear what you're doing, at least not on a level that I could give you any helpful advice...

    You can extract your "hidden message" from the WAV file before you send that WAV file over the network, but not afterwards? Is that the problem?
  • 2. Re: Transfer Audio Input Stream with Socket
    834115 Newbie
    Currently Being Moderated
    Yes that was my problem and I was able to solve, but now I have a different problem.
    When I save the sent file, only one file I send is sent completely with no problems.
    With the other sound files, the sound is distorted and I hear static noise. I believe it has something to do with the AudioFormat, but I send each property of the AudioFormat through the socket to create the same AudioFormat for the sent file.

    Im not really sure what causes the problem because there is that one file that is sent completely without the sound being distorted, but all the other files have their sound distorted when played.

    Any thoughts?

    Thanks in advacne
  • 3. Re: Transfer Audio Input Stream with Socket
    EJP Guru
    Currently Being Moderated
    It is impossible to comment on this without seeing some code.
  • 4. Re: Transfer Audio Input Stream with Socket
    834115 Newbie
    Currently Being Moderated
    Alright, Sorry if I was being unclear about my problem.
    Well here is my code:
    The server sends the file to the client.
    File myFile =fcServer.getSelectedFile();                    
                        try{
                             AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(myFile);
                             AudioFormat audioFormat = audioInputStream.getFormat();
                             
                             PrintWriter out = new PrintWriter(ServerSocket.getOutputStream(), true);
                             out.println(audioInputStream.available());
                             out.println(audioFormat.getSampleRate());
                             out.println(audioFormat.getSampleSizeInBits());
                             out.println(audioFormat.getChannels());
                             out.println(audioFormat.getFrameSize());
                             out.println(audioFormat.getFrameRate());
                             out.println(audioFormat.isBigEndian());
                             RetrieveMessage rm = new RetrieveMessage();
                             String hiddenMessage = rm.Retrieve(myFile);
                             out.println(hiddenMessage);          
    
                             
                             byte[] data=new byte[audioInputStream.available()]; 
                             audioInputStream.read(data);
                             String theString = new String(data);
                             if(!hiddenMessage.equals("")){
                                  int byteSize = Integer.parseInt(theString.substring(theString.lastIndexOf("size")+4, theString.lastIndexOf("hide")).trim());
                                  String soundBytaArray = theString.substring(0, byteSize);
                           
                                  OutputStream socketOutputStream = ServerSocket.getOutputStream();
                                  socketOutputStream.write(soundBytaArray.getBytes());
                             }
                             else{
                                  OutputStream socketOutputStream = ServerSocket.getOutputStream();
                                  socketOutputStream.write(data);
                             }
                             
                        }
    The class RetrieveMessage is a class I built to retrieve a message the was hidden in the file.

    The client receives the sent file from the server:
    while (true){
                             try{
                                  InputStream is = ClientSocket.getInputStream();
                                  BufferedReader in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
                                  if(is.available()!=0){
                                       
                                       int fileSize = Integer.parseInt(in.readLine());
                                       float sampleRate = Float.parseFloat(in.readLine());
                                       int sampleSizeInBits = Integer.parseInt(in.readLine());
                                       int Channels = Integer.parseInt(in.readLine());
                                       int frameSize = Integer.parseInt(in.readLine());
                                       float frameRate = Float.parseFloat(in.readLine());
                                       boolean isBigEndian = Boolean.parseBoolean(in.readLine());
                                       String hiddenMessage = in.readLine();
                                       
                                       byte[] data = new byte[fileSize];
                                       InputStream socketInputStream = ClientSocket.getInputStream();
                                       socketInputStream.read(data);
                                       String sound = new String(data).trim();
                                       byte[] mybytearray = sound.getBytes();
                                                                     
                                       AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, sampleRate, sampleSizeInBits, Channels, frameSize, frameRate, isBigEndian);
    
                                      java.io.InputStream stream = new ByteArrayInputStream(mybytearray);
                                      AudioInputStream ais = new AudioInputStream(stream, format, fileSize);
                                      SaveFileDialog sfd = new SaveFileDialog(mainFrame);
                                      sfd.setVisible(true);
                                      String FilePath = new String(sfd.tFilePath.getText() + (char)92 + sfd.tFileName.getText()); //(char)92 is to add '\'
                                      String tempFilePath = new String(sfd.tFilePath.getText() + (char)92 + "tempFile");
                                      sfd.setVisible(false);
                                      FilePath = correctFileName(FilePath); //file path after file name correction
                                      if(hiddenMessage.equals(""))
                                           AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(FilePath));
                                      else{
                                           AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(tempFilePath));
                                           File file = new File(FilePath);
                                           File tempFile = new File(tempFilePath);
                                           HideMessage hm = new HideMessage();
                                           hm.Hide(tempFile, file, hiddenMessage);
                                           tempFile.delete();
                                      }
    Hope it helpful.
    Thank you everyone for your help.
  • 5. Re: Transfer Audio Input Stream with Socket
    EJP Guru
    Currently Being Moderated
    This code presents a very common catalogue of mistakes.
                             PrintWriter out = new PrintWriter(ServerSocket.getOutputStream(), true);
    First mistake, using Readers and Writers for binary data.
                             byte[] data=new byte[audioInputStream.available()]; 
    Second mistake, using InputStream.available() as though it gives you the entire size of the stream. It doesn't. Check the Javadoc.
                             audioInputStream.read(data);
    Third mistake, not checking the result of InputStream.read(byte[]), and assuming it filled the buffer.
    String theString = new String(data);
    OutputStream socketOutputStream = ServerSocket.getOutputStream();
    socketOutputStream.write(soundBytaArray.getBytes());
    Fourth mistake, using String as a container for binary data.
                                  OutputStream socketOutputStream = ServerSocket.getOutputStream();
                                  socketOutputStream.write(soundBytaArray.getBytes());
    Fifth mistake, mixing OutputStreams and Writers on the same underlying stream.
    while (true){
                             try{
                                  InputStream is = ClientSocket.getInputStream();
                                  BufferedReader in = new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
    Sixth mistake, same as first but for Readers/InputStreams instead of Writers/OutputStreams.
                                  if(is.available()!=0){
    Seventh mistake, same as second above.
                                       socketInputStream.read(data);
    Eighth mistake, same as third above.
                                       String sound = new String(data).trim();
                                       byte[] mybytearray = sound.getBytes();
    Ninth mistake, same as fourth above.

    Tenth mistake, using trim() unnecessarily. There is no code anywhere that can introduce leading or trailing spaces into this data.
                                      String FilePath = new String(sfd.tFilePath.getText() + (char)92 + sfd.tFileName.getText()); //(char)92 is to add '\'
    ...
                                      String tempFilePath = new String(sfd.tFilePath.getText() + (char)92 + "tempFile");
    11th mistake, assuming a character encoding. If you want \ use a double backslash. It is well documented and well known. However what you should really be using is /. There is never a need to use a backslash in a filename in Java.
                                      FilePath = correctFileName(FilePath); //file path after file name correction
    Correction how?
                                      if(hiddenMessage.equals(""))
                                           AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(FilePath));
                                      else{
                                           AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(tempFilePath));
    12th mistake, writing an if/else when the underlying processing is the same. The only difference here is the name of the file.
  • 6. Re: Transfer Audio Input Stream with Socket
    834115 Newbie
    Currently Being Moderated
    Alright I think I narrowed it down to just this one problem.
    I think the AudioInputStream isn't sent completely, only part of it is sent through the socket, and Im having problems finding a way to send the whole AudioInputStream through the socket.

    Any ideas?

    Thank you all for your help.
  • 7. Re: Transfer Audio Input Stream with Socket
    EJP Guru
    Currently Being Moderated
    Any ideas?
    +'Any ideas'?+

    You must be kidding. I've just given you one dozen ideas.

    I'll give you one more. The correct way to copy a stream in Java is as follows:
    byte[] buffer = new byte[8192]; // or more
    int count;
    while ((count = in.read(buffer)) > 0)
      out.write(buffer, 0, count);
  • 8. Re: Transfer Audio Input Stream with Socket
    captfoss Pro
    Currently Being Moderated
    831112 wrote:
    Any ideas?
    Why in the hell are you opening a WAV file, sending its audio stream along a socket, and then saving it as a WAV file on the other side???

    JUST SEND IT LIKE A NORMAL (BINARY) FILE!
  • 9. Re: Transfer Audio Input Stream with Socket
    834115 Newbie
    Currently Being Moderated
    Ive tried both ways you said and still it doesnt seem to work. It still receives only part of the InputStream.
  • 10. Re: Transfer Audio Input Stream with Socket
    EJP Guru
    Currently Being Moderated
    Show us.
  • 11. Re: Transfer Audio Input Stream with Socket
    captfoss Pro
    Currently Being Moderated
    831112 wrote:
    Ive tried both ways you said and still it doesnt seem to work. It still receives only part of the InputStream.
    Sounds like a PEBKAC error to me...

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points