---
title: 'Retrieve Windows netstat data and observe new network connections'
url: 'https://tutego.de/blog/javainsel/2016/03/retrieve-windows-netstat-data-and-observe-new-network-connections'
markdown: 'https://tutego.de/blog/javainsel/2016/03/retrieve-windows-netstat-data-and-observe-new-network-connections.md'
date: '2016-03-10'
description: 'import java.io.IOException; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; public class Netstat { public static class Protocol { public final String protocol; public final String localAddress; public final String remoteAddress; public …'
taxonomy:
  category:
    - Snippet
---

# Retrieve Windows netstat data and observe new network connections

![Illustration einer tropischen Insel mit Palmen und einem hohen Berg](https://tutego.de/blog/javainsel/user/themes/tutego/images/insel-cover.jpg)# Java Blog für Programmierer

[Java Blog für Programmierer](https://tutego.de/blog/javainsel/)

## Retrieve Windows netstat data and observe new network connections

10.03.2016 · [Christian Ullenboom](https://tutego.de/blog/javainsel/author/admin/)

 ```java
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

public class Netstat {

  public static class Protocol {

    public final String protocol;
    public final String localAddress;
    public final String remoteAddress;
    public final String status;

    public Protocol( String protocol, String localAddress, String remoteAddress, String status ) {
      this.protocol = protocol;
      this.localAddress = localAddress;
      this.remoteAddress = remoteAddress;
      this.status = status;
    }

    @Override
    public int hashCode() {
      return Objects.hash( localAddress, protocol, remoteAddress, status );
    }

    @Override
    public boolean equals( Object obj ) {
      if ( this == obj )
        return true;
      if ( obj == null )
        return false;
      if ( getClass() != obj.getClass() )
        return false;
      Protocol other = (Protocol) obj;
      if ( localAddress == null && other.localAddress != null )
        return false;
      else if ( !localAddress.equals( other.localAddress ) )
        return false;
      if ( protocol == null && other.protocol != null )
        return false;
      else if ( !protocol.equals( other.protocol ) )
        return false;
      if ( remoteAddress == null && other.remoteAddress != null )
        return false;
      else if ( !remoteAddress.equals( other.remoteAddress ) )
        return false;
      if ( status == null && other.status != null )
        return false;
      else if ( !status.equals( other.status ) )
        return false;
      return true;
    }

    @Override
    public String toString() {
      return String.format( "%-6s %-22s %-22s %s", protocol, localAddress, remoteAddress, status );
    }
  }

  private final static Pattern pattern = Pattern.compile( "(TCP|UDP)\\s+(\\S+)\\s+(\\S+)\\s+(\\S+)" );

  public static Collection netStat() throws IOException {
    Collection result = new ArrayList();
    Process p = new ProcessBuilder( "netstat", "-n" ).start();
    try ( Scanner scanner = new Scanner( p.getInputStream() ) ) {
      while ( scanner.findWithinHorizon( pattern, 0 ) != null )
        result.add( new Protocol( scanner.match().group( 1 ), scanner.match().group( 2 ),
                                   scanner.match().group( 3 ), scanner.match().group( 4 ) ) );
    }
    return result;
  }

  public static void main( String[] args ) throws IOException, InterruptedException {
    Set oldStat = new HashSet( netStat() );

    while ( true ) {
      TimeUnit.SECONDS.sleep( 10 );

      HashSet newStat = new HashSet( netStat() );

      Set differenceSet = new HashSet( newStat );
      differenceSet.removeAll( oldStat );

      for ( Protocol p : differenceSet )
        System.out.println( p );

      oldStat = newStat;
    }
  }
}
```

Kategorien: [Snippet](https://tutego.de/blog/javainsel/category/snippet/)

[← Alle Beiträge](https://tutego.de/blog/javainsel/)

---

## Navigation

- Next: [Bouncing ball in a Swing window](https://tutego.de/blog/javainsel/2016/03/bouncing-ball-in-a-swing-window.md)
