Tipp 0044 IP-Adresse berechnen
Autor/Einsender:
Datum:
  Peter Grischott
04.06.2004
Entwicklungsumgebung:   VB.Net 2003
Framework:   1.1
Mit diesem Tool kann man anhand einer Host-IP oder Subnetmask die Netz- und Broadcast-Adresse berechnen. Diese können in Hexadezimal-, Dezimal- oder Binär-Format dargestellt werden.
 
Public Class Form1
  Inherits System.Windows.Forms.Form
Vom Windows Form Designer generierter Code
  Private Sub RadioButton_CheckedChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) _
          Handles optEingabeSubnetzBin.Click, _
                  optEingabeSubnetzDez.Click, _
                  optEingabeSubnetzHex.Click, _
                  optEingabeIPBin.Click, _
                  optEingabeIPDez.Click, _
                  optEingabeIPHex.Click, _
                  optAusgabeBroadBin.Click, _
                  optAusgabeBroadDez.Click, _
                  optAusgabeBroadHex.Click, _
                  optAusgabeNetzBin.Click, _
                  optAusgabeNetzDez.Click, _
                  optAusgabeNetzHex.Click
    Einheitwechseln(sender.text, sender.tabIndex)
  End Sub

  Private Sub Einheitwechseln(ByVal Einheit As String, _
          ByVal Tabindex As Integer)
    Dim fraSuchRahmen As Control
    Dim EinheitQuelle As String
    Dim EinheitZiel As String

    Select Case Tabindex
      Case Is < 99
        fraSuchRahmen = fraEingabeIP
        Exit Select
      Case Is < 199
        fraSuchRahmen = fraEingabeSubnet
        Exit Select
      Case Is < 299
        fraSuchRahmen = fraAusgabeNetz
        Exit Select
      Case Is < 399
        fraSuchRahmen = fraAusgabeBroadCast
        Exit Select
    End Select

    Dim lblSuchlabel As Control
    For Each lblSuchlabel In fraSuchRahmen.Controls
      If lblSuchlabel.GetType.Name.ToString = "Label" Then
        If lblSuchlabel.BackColor.Name = Color.DarkGray.Name Then
          lblSuchlabel.BackColor = Color.Gainsboro
          EinheitQuelle = lblSuchlabel.Text
        End If
        If lblSuchlabel.Text.ToString = Einheit.ToString Then
          lblSuchlabel.BackColor = Color.DarkGray
          EinheitZiel = lblSuchlabel.Text
        End If
      End If
    Next

    For Each lblSuchlabel In fraSuchRahmen.Controls
      If lblSuchlabel.GetType.Name.ToString = "TextBox" Then
        Dim StartWert As String = lblSuchlabel.Text
        Dim ZielWert As String = ""
        ZielWert = Rechnen(EinheitQuelle, EinheitZiel, StartWert)
        If EinheitZiel = "Binär" Then
          Dim i As Integer
          For i = 0 To (8 - ZielWert.Length) - 1
            ZielWert = "0" + ZielWert
          Next i
        End If

        lblSuchlabel.Text = ZielWert
        lblSuchlabel.Refresh()
      End If
    Next lblSuchlabel
  End Sub

  Function Rechnen(ByVal Quelleinheit As String, ByVal _
       Zieleinheit As String, ByVal Quelle As String) As String
    Select Case Quelleinheit
      Case "Dezimal"
        If Zieleinheit = "Hexadezimal" Then
          Return Hex(Quelle)
        End If
        If Zieleinheit = "Binär" Then
          Return Convert.ToString(CByte(Quelle.ToString), 2)
        Else
          Return Quelle.ToString
        End If

      Case "Hexadezimal"
        If Zieleinheit = "Dezimal" Then
          Return ((Convert.ToInt32( _
               (Quelle.ToString), 16)).ToString)
        End If
        If Zieleinheit = "Binär" Then
          Return Convert.ToString(CByte(((Convert.ToInt32( _
                    (Quelle.ToString), 16)).ToString)), 2)
        Else
          Return Quelle.ToString
        End If

      Case "Binär"
        If Zieleinheit = "Dezimal" Then
          Return Convert.ToByte(Quelle.ToString, 2)
        End If
        If Zieleinheit = "Hexadezimal" Then
          Return Hex(Convert.ToByte(Quelle.ToString, 2))
        Else
          Return Quelle.ToString
        End If
    End Select
  End Function

  Private Sub txtEingabe_TextChanged(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) _
          Handles txtEingabeIP1.Leave, _
                  txtEingabeIP2.Leave, _
                  txtEingabeIP3.Leave, _
                  txtEingabeIP4.Leave, _
                  txtEingabeSubnet1.Leave, _
                  txtEingabeSubnet2.Leave, _
                  txtEingabeSubnet3.Leave, _
                  txtEingabeSubnet4.Leave
    Dim Prüfwert As String = sender.text
    Dim strEinheit As String
    Dim crtEinheit As Control

    For Each crtEinheit In sender.parent.controls
      If crtEinheit.BackColor.Name = Color.DarkGray.Name Then
        strEinheit = crtEinheit.Text
        Exit For
      End If
    Next

    If Prüfwert = "" Then
      sender.focus()
      sender.backcolor = Color.Red
      MessageBox.Show("Keine Eingabe ist nicht erlaubt!")
      sender.backcolor = Color.White
      Exit Sub
    End If

    Dim i As Integer
    Dim BolKorektEingabe As Boolean = False

    If strEinheit = "Hexadezimal" Then
      For i = 0 To Prüfwert.Length - 1
        BolKorektEingabe = False
        Select Case Prüfwert.Chars(i)
          Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", _
               "a", "b", "c", "d", "e", "f", "A", "B", "C", "D", _
               "E", "F"
            BolKorektEingabe = True
        End Select
        If BolKorektEingabe = False Then
          sender.focus()
          sender.backcolor = Color.Red
          MessageBox.Show( _
                "Falsche Eingabe! Es sind nur Zeichen von " & _
                "0 bis 9 und a bis f möglich!")
          sender.backcolor = Color.White
          Exit Sub
        End If
      Next i
    End If

    If strEinheit = "Dezimal" Then
      For i = 0 To Prüfwert.Length - 1
        BolKorektEingabe = False
        Select Case Prüfwert.Chars(i)
          Case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
            BolKorektEingabe = True
        End Select
        If BolKorektEingabe = False Then
          sender.focus()
          sender.backcolor = Color.Red
          MessageBox.Show( _
                "Falsche Eingabe! Es sind nur Zahlen von " & _
                "0 bis 9 erlaubt!")
          sender.backcolor = Color.White
          Exit Sub
        End If
      Next i
    End If

    If strEinheit = "Binär" Then
      For i = 0 To Prüfwert.Length - 1
        BolKorektEingabe = False
        Select Case Prüfwert.Chars(i)
          Case "0", "1"
            BolKorektEingabe = True
        End Select
        If BolKorektEingabe = False Then
          sender.focus()
          sender.backcolor = Color.Red
          MessageBox.Show( _
                "Falsche Eingabe! Es sind nur die Zahlen " & _
                "0 und 1 erlaubt!")
          sender.backcolor = Color.White
          Exit Sub
        End If
      Next i
    End If

    Select Case strEinheit
      Case "Dezimal"
      Case "Hexadezimal"
        Prüfwert = ((Convert.ToInt32((Prüfwert), 16)).ToString)
      Case "Binär"
        Prüfwert = Convert.ToByte(Prüfwert, 2)
    End Select
    If CInt(Prüfwert) < 0 Or CInt(Prüfwert) > 255 Then
      MessageBox.Show( _
            "Wert muss in Dezimal zwischen 0 und 255 liegen!")
    End If
  End Sub

  Private Sub cmdBerechnen_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles cmdBerechnen.Click
    NetzadresseBerechnen()
  End Sub

  Private Sub NetzadresseBerechnen()
    optEingabeIPDez.PerformClick() : _
        optEingabeSubnetzDez.PerformClick()
    optAusgabeNetzDez.PerformClick() : _
        optAusgabeBroadDez.PerformClick()

    Dim strIP(4), strSubnet(4), strNetz(4) As String
    Dim strBroadcast(4) As String
    strIP(0) = txtEingabeIP1.Text : _
         strIP(1) = txtEingabeIP2.Text
    strIP(2) = txtEingabeIP3.Text : _
         strIP(3) = txtEingabeIP4.Text
    strSubnet(0) = txtEingabeSubnet1.Text : _
         strSubnet(1) = txtEingabeSubnet2.Text
    strSubnet(2) = txtEingabeSubnet3.Text : _
         strSubnet(3) = txtEingabeSubnet4.Text

    Dim i As Integer
    For i = 0 To 3
      strIP(i) = _
          Convert.ToString(CByte(strIP(i).ToString), 2)
      strSubnet(i) = _
          Convert.ToString(CByte(strSubnet(i).ToString), 2)
      If strIP(i).Length < 8 Then
        Dim intFüllmenge As Integer = 7 - strIP(i).Length
        Dim s As Integer
        For s = 0 To intFüllmenge
          strIP(i) = "0" + strIP(i)
        Next
      End If
      If strSubnet(i).Length < 8 Then
        Dim intFüllmenge As Integer = 7 - strSubnet(i).Length
        Dim s As Integer
        For s = 0 To intFüllmenge
          strSubnet(i) = "0" + strSubnet(i)
        Next
      End If
    Next i

    Dim h As Integer
    For i = 0 To 3
      For h = 0 To 7
        If strSubnet(i).Chars(h) = "1" And _
              strIP(i).Chars(h) = "1" Then
          strNetz(i) += "1"
        Else
          strNetz(i) += "0"
        End If
        If strSubnet(i).Chars(h) = strIP(i).Chars(h) Then
          strBroadcast(i) += "1"
        Else
          strBroadcast(i) += "0"
        End If
      Next h
      strNetz(i) = _
         (Convert.ToByte(strNetz(i).ToString, 2))
      strBroadcast(i) = _
         (Convert.ToByte(strBroadcast(i).ToString, 2))
    Next i

    txtAusgabeNetz1.Text = strNetz(0) : _
        txtAusgabeNetz2.Text = strNetz(1)
    txtAusgabeNetz3.Text = strNetz(2) : _
        txtAusgabeNetz4.Text = strNetz(3)
    txtAusgabeBroadcast1.Text = strBroadcast(0) : _
        txtAusgabeBroadcast2.Text = strBroadcast(1)
    txtAusgabeBroadcast3.Text = strBroadcast(2) : _
        txtAusgabeBroadcast4.Text = strBroadcast(3)

    Dim intAnzahlAdressen(2)
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show( _
        "Geben Sie zuerst eine Netz oder Host IP-Nummer ein! " & _
        "Mit den RadioButtons ....", "Hilfe", _
        MessageBoxButtons.OK, MessageBoxIcon.Information, _
        MessageBoxDefaultButton.Button3)
  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, _
          ByVal e As System.EventArgs) Handles Button2.Click
    Me.Close()
  End Sub
End Class
 
Weitere Links zum Thema
E-Mail-Adresse prüfen und E-Mail anzeigen
URI erzeugen und zerlegen

Windows-Version
98/SE
ME
NT
2000
XP
Vista
Win 7


Download  (12 kB) Downloads bisher: [ 2020 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Tipps | Projekte | Tutorials | Bücherecke | VB-/VBA-Tipps | API-Referenz | Komponenten | VB.Net-Forum | VB/VBA-Forum | DirectX-Forum | Foren-Archiv | DirectX | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Sonntag, 22. Januar 2012