when I stream live audio via sockets between android devices ，I got sound with loud noise
<p>Hi friends，this problem has been driving me nuts for 2 days.I am gonna be very grateful to your suggestions or thoughts. Let me describe the problem in detail.</p>

<p>For now,I am developing a Walkie-talkie or push to talk application on android. On default,every devices are connected to the server through sockets and are listening if there is any audio stream sent from the server to play. Later,if a user presses down the talk button，the application stops listening the input audio stream and start to record what  the user talking and simultaneously transfer the recorded audio stream data to a remote server which running on a desktop computer through a socket connection(tcp socket). Once the server received the audio stream data, it then transfers this data to the rest of android devices which as well connect to it through sockets. So if we have 3 people are using this application and one of them presses down the talk button to talk, then the others will hear what he talks. Below is my android client code:</p>

<pre><code>public class MainActivity extends AppCompatActivity {
private Button startButton,stopButton;
public byte[] buffer;
public static DatagramSocket s_socket;
public static DatagramSocket r_socket;
private int port=50005;
private int r_port = 50006;
AudioRecord recorder;
AudioTrack audioTrack;
private int sampleRate = 16000 ; // 44100 for music
private int channelConfig = AudioFormat.CHANNEL_IN_MONO;
private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
int minBufSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig,                   audioFormat);
private boolean s_status = true;
private boolean r_status = true;
Thread r_Thread;
Thread s_Thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startButton = (Button) findViewById (R.id.start_button);
    startButton.setOnTouchListener (talkListener);
    startReceiving();
}


private final View.OnTouchListener talkListener = new View.OnTouchListener()   {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                startButton.setBackgroundColor(Color.parseColor("#0670c0"));
                try {
                    startStreaming();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return true; // if you want to handle the touch event

            case MotionEvent.ACTION_UP:
                startButton.setBackgroundColor(Color.parseColor("#353535"));

                try {

                    stopStreaming();
                    startReceiving();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return true; // if you want to handle the touch event
        }
        return false;
    }
};
int c=0;
Socket socket;

InetSocketAddress address;
int timeout = 6000;
public void startStreaming() throws InterruptedException, IOException {
    r_status= false;
     buffer = new byte[minBufSize];




    s_Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            if(socket==null) {
                socket = new Socket();
                address = new InetSocketAddress("192.168.0.2", port);
                try {
                    socket.setReuseAddress(true);
                    socket.connect(address, timeout);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }



            recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, minBufSize * 10);
            try {
                recorder.startRecording();
            } catch (IllegalStateException e) {
                e.printStackTrace();
                return;
            }
            s_status = true;
            OutputStream os = null;
            while (s_status == true) {
                //reading data from MIC into buffer
                recorder.read(buffer, 0, buffer.length);
                try {
                    os = socket.getOutputStream();
                    os.write(buffer, 0, minBufSize);
                }catch (Exception e){
                    e.printStackTrace();
                    s_status = false;
                }
                System.out.println("streaming out: " + buffer.length + "fff"+c++);
            }

            try {

                os.write(new byte[0]);
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
});
    s_Thread.start();
}
public void stopStreaming() throws InterruptedException, IOException {
         s_status = false;
    r_status = true;
    if(recorder!=null) {
        recorder.stop();
        recorder.release();
        recorder = null;
    }

}

public void startReceiving() {
    r_Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            InputStream is = null;
            while (r_status) {
                try {
                    if(socket==null) {
                        socket = new Socket();
                        address = new InetSocketAddress("192.168.0.2", port);
                        try {
                            socket.setReuseAddress(true);
                            socket.connect(address, timeout);
                            System.out.println("connecting-");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                    is = socket.getInputStream();

                    byte[] buffer = new byte[minBufSize];
                    is.read(buffer);
                    if (audioTrack == null) {
                        audioTrack = new AudioTrack(AudioManager.STREAM_VOICE_CALL, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, buffer.length, AudioTrack.MODE_STREAM);

                        audioTrack.play();
                    }

                    audioTrack.write(buffer, 0, buffer.length);
                    System.out.println("receiving: " + buffer.length + "fff"+c++);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            try {
                is.reset();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
    r_Thread.start();

}
</code></pre>

<p>}</p>

<p>The server code:</p>

<pre><code>public class TcpServerSocket implements Runnable {
static AudioFormat format;
static boolean status = true;
static int port = 50005;
static int outputPort = 50006;
static int sampleRate = 16000;
static int count = 0;
static Thread playSound;

public static ArrayList&lt;String&gt; addressList;
public static ArrayList&lt;Socket&gt; sockets;
Socket csocket;
public static boolean isListenning = true;
public boolean isRunning = true;
public TcpServerSocket(Socket cScoket){
    this.csocket = cScoket;
}

public static void main(String args[]) throws Exception {
 ServerSocket serverSocket = new ServerSocket(port);
 sockets = new ArrayList&lt;Socket&gt;();
    while(isListenning){
        Socket socket = serverSocket.accept();
        sockets.add(socket);
        System.out.println("new connection");
        new Thread(new TcpServerSocket(socket)).start();
    }
    serverSocket.close();



 }

@Override
public void run() {


       byte[] receiveData = new byte[1280];

        format = new AudioFormat(sampleRate, 16, 1, true, false);
        InputStream baiss = null;

        OutputStream os;
        while (isRunning){
            try {
                baiss = csocket.getInputStream();
                if(!csocket.isConnected()){
                    isRunning = false;
                    sockets.remove(csocket);
                    System.out.println("close socket");

                }
                baiss.read(receiveData);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            int socketsLen = sockets.size();
            for(int i = 0;i&lt;socketsLen;i++){
                Socket client = sockets.get(i);
                if(!client.equals(csocket)){
                    try {
                    os = client.getOutputStream();
                    os.write(receiveData,0,1280);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }

        System.out.println(csocket.getInetAddress().toString());
        }
        try {
            baiss.close();
            csocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


}
}
</code></pre>

<p>The weird problem that has been bothering me is:For example, we have to 2 phones,Phone A and Phone B. If I just simply press down the talk button on Phone A to talk, then Phone B hears me perfectly,even after I release the button and press down to talk again, the sound is perfect.But if Phone B has pressed down the talk button to talk to Phone A, then  When the next time Phone B receives audio from Phone A, the sound is broken with a lot of noise. According to what I have figured out so far the cause resulting this problem is possibly related to the socket. If I create a brand new socket every time I start to talk or start to receive,then the sound quality is always good.I really can not explain why.But this approach is not efficient，it wastes lots of hardware resources.And socket is supposed to be reusable. Can you guys give me some ideas fixing this issue.</p>