title: List Open TCP connections in Java
body: <p>I was wondering how I could list all the TCP connections on my system which are open using Java. I am using CentOS.</p>
  
  <p>I do not have any clue on where to start also. Any pointers would be helpful.</p>
  
  <p>Thanks in advance</p>
  
  <p>Thanks for the tips
  I have to do something like this </p>
  
  <p></p>
  
  <p>Q)  Identify any new established connections for all tcp ports that are currently listening<br>
      and continue to poll every 5 seconds. The script should terminate when there are no longer any established connections.</p>
  
  <pre><code>public class TCPConnections {
  
      public HashSet&lt;Integer&gt; establishedConnections = new HashSet&lt;Integer&gt;();
      public HashSet&lt;Integer&gt; listeningConnections = new HashSet&lt;Integer&gt;();
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          TCPConnections tcpConnections = new TCPConnections();
          try{        
              do{    
                  tcpConnections.getListeningConnections();
                  Thread.sleep(5000);
                  tcpConnections.getEstablishedConnections();    
              }while(!tcpConnections.establishedConnections.isEmpty());    
          }    
          catch(Exception ex){    
              ex.printStackTrace();    
          }
      }
  
      public void getEstablishedConnections(){
          String netstat = new String();    
          try {    
                  String line;
                  establishedConnections = new HashSet&lt;Integer&gt;();
                  String[] cmd = {    
                          "/bin/sh",    
                          "-c",    
                          "netstat -atn | grep -w tcp | grep ESTABLISHED"    
                          };
  
                  java.lang.Process p = Runtime.getRuntime().exec(cmd);
                  BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
  
                  while ((line = input.readLine()) != null) {    
                      String[] portNo = line.split("\\s+");    
                      if(portNo[3] != null &amp;&amp; !portNo[3].equalsIgnoreCase(" ")){    
                          String str = portNo[3].split(":")[1];    
                          if( str != null &amp;&amp; str.matches("[0-9]+")){    
                              establishedConnections.add(Integer.parseInt(str));    
                              if(listeningConnections.contains(Integer.parseInt(str))){listeningConnections.remove(Integer.parseInt(str));  
                                  System.out.println(" New connection established on port : "+Integer.parseInt(str));    
                              }    
                          }    
                      }    
                          netstat = netstat + " \n" + line;    
                  }    
                  System.out.println(netstat);    
                  input.close();
          } catch (Exception err) {    
                  err.printStackTrace();    
          }
      }
  
      public void getListeningConnections(){    
          String netstat = new String();    
          try {    
                  String line;    
                  listeningConnections = new HashSet&lt;Integer&gt;();
                  String[] cmd = {    
                          "/bin/sh",    
                          "-c",    
                          "netstat -atn | grep -w tcp | grep LISTEN"    
                          };
                  java.lang.Process p = Runtime.getRuntime().exec(cmd);    
                  BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));    
                  while ((line = input.readLine()) != null) {    
                      String[] portNo = line.split("\\s+");    
                      if(portNo[3] != null &amp;&amp; !portNo[3].equalsIgnoreCase(" ")){    
                          String str = portNo[3].split(":")[1];    
                          if( str != null &amp;&amp; str.matches("[0-9]+")){    
                              listeningConnections.add(Integer.parseInt(str));                        
                          }   
                      }       
                          netstat = netstat + " \n" + line;    
                  }    
                  System.out.println(netstat);    
                  input.close();          
          } catch (Exception err) {    
                  err.printStackTrace();    
          }
      }    
  }
  </code></pre>
  
  <p>The problem I face is that few ports are always in the established state and few are always in the Listen state so that the do-while loop runs forever. Kindly help me to solve this issue.</p>