title: Port binding on macos
body: <p>I used ServerSocket in java to check whether the mysql is running or not . If it's not running I prompted the user to please run your mysql server . But , Some of my users complained we got error message although the mysql is running . It only happens in macos . What I find is .</p>
  
  <p>Java Code :</p>
  
  <pre><code>public static boolean isPortFree(int port)
  {
      if(port &lt;= 0)
      {
          return false;
      }
      ServerSocket sock = null;
      try
      {
          String bindAddress = System.getProperty("bindaddress", "127.0.0.1");
          sock = new ServerSocket(port,0,InetAddress.getByName(bindAddress));
      }
      catch(Exception ex)
      {
          return false;
      }
      finally
      {
          if (sock != null)
          {
              try
              {
                  sock.close();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
          }
      }
      return true;
  }
  </code></pre>
  
  <p>If i set system property <code>bindaddress</code> to <code>localhost</code> it returns false  same i set to <code>127.0.0.1</code> returns true . 
  localhost , 127.0.0.1 is same why it behaves like this . It only occurs in macos . I think it's not a <code>my.cnf</code> bindaddress problem . Because I checked in command prompt with</p>
  
  <pre><code> mysql -uroot -hlocalhost
   mysql -uroot -h127.0.0.1
  </code></pre>
  
  <p>both are works .</p>
  
  <p><strong>Netstat</strong></p>
  
  <p>MacOs</p>
  
  <pre><code> tcp4       0      0  *.3306                 *.*                    LISTEN
  </code></pre>
  
  <p>Other Os</p>
  
  <pre><code>tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN 
  </code></pre>
  
  <p>Is there any difference between tcp4 and tcp?</p>
  
  <p>Any Idea why it happens ?</p>