title: java.net.SocketException: sendto failed: EPIPE (Broken pipe) When sending character to check socket connection
body: <p>I develop a system include: socket server on Android Mobile and socket client on PC. And to check the connection with client, I had sent a <code>" "</code> character every 1 second from server.</p>
  
  <p>But sometimes, I got the exception:</p>
  
  <pre><code>java.net.SocketException: sendto failed: EPIPE (Broken pipe)    
      at libcore.io.IoBridge.maybeThrowAfterSendto(IoBridge.java:506)    
      at libcore.io.IoBridge.sendto(IoBridge.java:475)    
      at java.net.PlainSocketImpl.write(PlainSocketImpl.java:507)    
      at java.net.PlainSocketImpl.access$100(PlainSocketImpl.java:46)    
      at java.net.PlainSocketImpl$PlainSocketOutputStream.write(PlainSocketImpl.java:269)    
      at java.io.OutputStream.write(OutputStream.java:82)    
      at com.foxconn.cnsbgit.mobileterminal.MainActivity$ServerThread.run(MainActivity.jaa:527)    
      at java.lang.Thread.run(Thread.java:856)    
  Caused by: libcore.io.ErrnoException: sendto failed: EPIPE (Broken pipe)    
      at libcore.io.Posix.sendtoBytes(Native Method)    
      at libcore.io.Posix.sendto(Posix.java:151)    
      at libcore.io.BlockGuardOs.sendto(BlockGuardOs.java:177)    
      at libcore.io.IoBridge.sendto(IoBridge.java:473)    
      ... 6 more
  </code></pre>
  
  <p>The sending <code>Thread</code> to check connection is following:</p>
  
  <pre><code>while (true) {
      if (client != null) {
           try {
                client.getOutputStream().write(" ".getBytes()); // Get exception when sending character
                Thread.sleep(1000);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        txtConnectionStatus.setText(R.string.smoConnectOK);
                    }
                });
           } catch (Exception e) {
                e.printStackTrace(); // Get exception at here
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        if !txtConnectionStatus.getText().toString().contains("FAIL")) {
                             txtConnectionStatus.setText(R.string.connectionFailString);
                        }
                    }
                });
                try {
                     Thread.sleep(5000);
                } catch (InterruptedException e1) {
                     e1.printStackTrace();
                }
            }
       }
  }
  </code></pre>
  
  <p><strong>Updated:</strong>
  Then I had input and sent data to client. Is connection lost when both heartbeat and data send at the same time? :</p>
  
  <pre><code>public class SendDataThread implements Runnable {
  
      @Override
      public void run() {
          try {
              if (client != null) {
                  sendDataStream = client.getOutputStream();
                  sendDataStream.write(dataSend); //dataSend is a byte array
                  sendDataStream.flush();
  
                  mHandler.post(new Runnable() {
                      @Override
                      public void run() {
                          edtCommand.selectAll();
                      }
                  });
              }
          } catch (final Exception e) {
              mHandler.post(new Runnable() {
                  @Override
                  public void run() {
                      txtRec.setText(e.toString());
                  }
              });
          }
      }
  }
  </code></pre>