title: Read / Write streamed audio via TCP socket Java
body: <p>I'm new to java and socket programing and I'm looking for more of a starting point / point in the right direction / validation of my line of thinking.</p>
  
  <p>Overall, the idea I'm wanting to implement is:</p>
  
  <ol>
  <li>capture audio that I record as a .wav </li>
  <li>send it via TCP to my server </li>
  <li>Decide if I want to play audio recorded (process and play it in my tcp server) or send to be processed (like transcriber or something like that) to a different server via TCP.</li>
  </ol>
  
  <p>The trouble I'm encountering is server side, and it's because I don't entirely understand socket programming, to be honest.  I want to receive the audio to my server as a raw audio (passing byte/binary array as parameter).  For most documents I've found basically they say open socket, open input / output stream, read / write, close streams, close sockets.  This works for say regular text but not audio.  I'm sure this can be done with audio. What would the general idea be to do this for audio .wav?  Is there an API I'm not aware of that covers this? </p>
  
  <p><BR/>
  <BR/>
  <BR/></p>
  
  <p><b>UPDATE: 8/30/2015</b><BR/>This is TCP client / server code I have so far.  For now, the "captured audio" I'm using is just an existing .wav file I read into the client output stream.  The issue I'm facing right now is that the .wav created doesn't sound like the original.  It sounds like noise, and is much shorter time wise.</p>
  
  <p><p> TCP Client code:</p></p>
  
  <pre><code>Socket serverSocket = null;
  DataOutputStream out = null;
  BufferedReader in = null;
  
  try {
      serverSocket = new Socket(serverHostname, port);
      out = new DataOutputStream(serverSocket.getOutputStream());
      in = new BufferedReader(
                new InputStreamReader(serverSocket.getInputStream()));
  } catch (UnknownHostException e) {
      System.err.println("Don't know about host: " + serverHostname);
      System.exit(1);
  } catch (IOException e) {
      System.err.println("Couldn't get I/O for the connection to: " + serverHostname);
      System.exit(1);
  }
  
  DataInputStream stdIn = new DataInputStream( 
                                new FileInputStream(".wav location"));
  
  int readBytes;
  byte[] temp = new byte[1024];
  
  while( (readBytes = stdIn.read(temp, 0, temp.length)) != -1){
      out.write(temp, 0, readBytes);
  }
  
  out.flush();
  out.close();
  in.close();
  stdIn.close();
  serverSocket.close();
  }
  </code></pre>
  
  <p>This is my Server code so far: </p>
  
  <pre><code>public void clientConnect(int socketPort) {
          ServerSocket s_Socket = null; 
  
          try {
               s_Socket = new ServerSocket(socketPort);
  
               System.out.println ("SERVER Connection Socket Created");
               try {
  
                   System.out.println ("Waiting for CLIENT Connection");
                    while (true){
                        new TcpAudioStreaming (c_Socket = s_Socket.accept());
                        System.out.println("CLIENT: " + c_Socket.getInetAddress());
                        System.out.println("PORT: " + c_Socket.getPort());
                        System.out.println("LOCAL PORT: " + c_Socket.getLocalPort());
                    }
               }     
               catch (IOException e) { 
                    System.err.println("Accept failed."); 
                    System.exit(1); 
               }
          }
  
          catch (IOException e){
               System.err.println("Could not listen on port: " + socketPort); 
               System.exit(1); 
          }      
          finally {
  
               try {
                   s_Socket.close(); 
               }
               catch (IOException e){
                    System.err.println("Could not close port: " + socketPort); 
                    System.exit(1); 
               }
          }
      }
  
  
      public void run() {
  
          boolean end = false;
          System.out.println ("New Communication Thread Started");
  
          try {
              //DataOutputStream outStream = new DataOutputStream(c_Socket.getOutputStream()); 
              ByteArrayOutputStream outStream = new ByteArrayOutputStream(); 
              DataInputStream inStream = new DataInputStream(c_Socket.getInputStream()); 
  
              int read;
              byte[] temp = new byte[1024];
  
              while( (read = inStream.read(temp, 0, temp.length)) != -1){
                  outStream.write(temp, 0, read);
              }
  
              outStream.flush();
              byte[] audioBytes = outStream.toByteArray();
  
              writeWave(audioBytes);
  
              outStream.close(); 
              inStream.close(); 
              c_Socket.close();
  
  
          } 
          catch (IOException e) {
                System.err.println("Problem with Communication Server");
                System.exit(1); 
          } 
  
      }
  
      public void writeWave(byte[] audioArry) {
  
          String filePath = "new .wav path";
  
          AudioFormat audioFormat = new AudioFormat(AudioFormat.Encoding.ULAW, 8000, 8, 1, 1, 8000, false);
  
          try {
  
              ByteArrayInputStream inStream = new ByteArrayInputStream(audioArry);
              long length = (long)(audioArry.length / audioFormat.getFrameSize());
              AudioInputStream audioInputStreamTemp = new AudioInputStream(inStream, audioFormat, length);
  
  
              File fileOut = new File(filePath);
  
              if (AudioSystem.isFileTypeSupported(AudioFileFormat.Type.WAVE, audioInputStreamTemp)) {
                  System.out.println("Trying to write");
                  AudioSystem.write(audioInputStreamTemp, AudioFileFormat.Type.WAVE, fileOut);
                  System.out.println("Written successfully");
              }       
          }
          catch(Exception e) {
              e.printStackTrace();
          }
  
      }
  </code></pre>