Ausnahme des Typ "System.Net.Sockets.SocketException"

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 9 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Ausnahme des Typ "System.Net.Sockets.SocketException"

    Guten Abend,
    Ich habe eine Ausnahme,
    weis aber nicht was diese Bedeutet ?(

    [Das Programm soll alle Pakete aufliste]

    Ausnahme;
    Spoiler anzeigen
    Eine Ausnahme vom Typ "System.Net.Sockets.SocketException" ist in System.dll aufgetreten, doch wurde diese im Benutzercode nicht verarbeitet.

    Zusätzliche Informationen: Der Zugriff auf einen Socket war aufgrund der Zugriffsrechte des Sockets unzulässig

    Falls ein Handler für diese Ausnahme vorhanden ist, kann das Programm möglicherweise weiterhin sicher ausgeführt werden.


    Code;

    VB.NET-Quellcode

    1. Dim socketz As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)


    Danke im Voraus ^^
    @DerTuner Starte Dein Programm als Administrator:
    Autostart Programm als Admin starten
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Naja die Fehlermeldung sagt doch schon alles, nicht ausreichende Berechtigungen.
    Wahrscheinlich versuchst du auf einem "privilegierten Port" zu arbeiten (alles unter 1024?), dafür brauchst du bzw. deine Anwendung "admin" Rechte.
    bisher hast du nix gezeigt, was du ausführst.
    Wie soll man nu sinnvoll auf deine Frage antworten?
    oder ist das wirklich diese Zeile:

    VB.NET-Quellcode

    1. Dim socketz As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
    die als normal-user failt und als admin funzt?
    weil ein bestimmter port ( < 1024) spielt da ja keine Rolle.

    Hier ist der Code;
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.Net
    2. Imports System.Net.Sockets
    3. Public Class Form1
    4. Dim socketz As New Socket(AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP)
    5. Dim bytedata(4096) As Byte
    6. Dim myip As IPAddress
    7. Dim started As Boolean = True
    8. Dim sizediff As Size
    9. Dim formloaded As Boolean = False
    10. Dim FilterIPAddress As New IPAddress(0)
    11. Dim FilterIP As Boolean
    12. Dim mycomputerconnections() As Net.NetworkInformation.NetworkInterface
    13. 'DGV Update stuff
    14. Dim stringz As String = ""
    15. Dim Typez As String = ""
    16. Dim ipfrom As IPAddress
    17. Dim ipto As IPAddress
    18. Dim destinationport As UInteger
    19. Dim sourceport As UInteger
    20. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    21. sizediff.Height = Me.Height - DGV.Height
    22. sizediff.Width = Me.Width - DGV.Width
    23. formloaded = True
    24. mycomputerconnections = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
    25. For i = 0 To mycomputerconnections.Length - 1
    26. ComboBox1.Items.Add(mycomputerconnections(i).Name)
    27. Next
    28. End Sub
    29. Private Sub OnReceive(ByVal asyncresult As IAsyncResult)
    30. If started = True Then
    31. 'Get Length of packet (including header)
    32. Dim readlength As UInteger = BitConverter.ToUInt16(Byteswap(bytedata, 2), 0)
    33. sourceport = BitConverter.ToUInt16(Byteswap(bytedata, 22), 0)
    34. destinationport = BitConverter.ToUInt16(Byteswap(bytedata, 24), 0)
    35. 'Get Protocol Type
    36. If bytedata(9) = 6 Then
    37. Typez = "TCP"
    38. ElseIf bytedata(9) = 17 Then
    39. Typez = "UDP"
    40. Else
    41. Typez = "???"
    42. End If
    43. 'Get IP from and to
    44. ipfrom = New IPAddress(BitConverter.ToUInt32(bytedata, 12))
    45. ipto = New IPAddress(BitConverter.ToUInt32(bytedata, 16))
    46. 'If this is a packet to/from me and not from myself then...
    47. If (ipfrom.Equals(myip) = True Or ipto.Equals(myip) = True) And ipto.Equals(ipfrom) = False Then
    48. If FilterIP = False Or (FilterIP = True And (FilterIPAddress.Equals(ipfrom) Or FilterIPAddress.Equals(ipto))) Then
    49. 'Fix data
    50. stringz = ""
    51. For i = 26 To readlength - 1
    52. If Char.IsLetterOrDigit(Chr(bytedata(i))) = True Then
    53. stringz = stringz & Chr(bytedata(i))
    54. Else
    55. stringz = stringz & "."
    56. End If
    57. Next
    58. 'Put data to DataGridView, since it's on a different thread now, invoke it
    59. DGV.Invoke(New MethodInvoker(AddressOf DGVUpdate))
    60. End If
    61. End If
    62. End If
    63. 'Restart the Receiving
    64. socketz.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
    65. End Sub
    66. Private Sub DGVUpdate()
    67. 'Remove rows if there are too many
    68. If DGV.Rows.Count > 50 Then
    69. DGV.Rows.RemoveAt(0)
    70. End If
    71. DGV.Rows.Add()
    72. DGV.Rows(DGV.Rows.Count - 1).Cells(0).Value = ipfrom.ToString & ":" & sourceport 'From Column, size at 125
    73. DGV.Rows(DGV.Rows.Count - 1).Cells(1).Value = ipto.ToString & ":" & destinationport 'To Column, size at 125
    74. DGV.Rows(DGV.Rows.Count - 1).Cells(2).Value = Typez 'Type Column, size at 50
    75. DGV.Rows(DGV.Rows.Count - 1).Cells(3).Value = stringz 'Data column, size mode set to fill
    76. End Sub
    77. Private Function Byteswap(ByVal bytez() As Byte, ByVal index As UInteger)
    78. Dim result(1) As Byte
    79. result(0) = bytez(index + 1)
    80. result(1) = bytez(index)
    81. Return result
    82. End Function
    83. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    84. If started = True Then
    85. Button1.Text = "Start"
    86. started = False
    87. Else
    88. Button1.Text = "Stop"
    89. started = True
    90. End If
    91. End Sub
    92. Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    93. If formloaded = True Then
    94. DGV.Size = Me.Size - sizediff
    95. End If
    96. End Sub
    97. Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    98. Try
    99. If TextBox1.Text <> "" And TextBox1.Text IsNot Nothing Then
    100. FilterIPAddress = IPAddress.Parse(TextBox1.Text)
    101. FilterIP = True
    102. TextBox1.BackColor = Color.LimeGreen
    103. Else
    104. FilterIP = False
    105. TextBox1.BackColor = Color.White
    106. End If
    107. Catch ex As Exception
    108. FilterIP = False
    109. TextBox1.BackColor = Color.White
    110. End Try
    111. End Sub
    112. Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    113. For i = 0 To mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses.Count - 1
    114. If mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address.AddressFamily = Net.Sockets.AddressFamily.InterNetwork Then
    115. myip = mycomputerconnections(ComboBox1.SelectedIndex).GetIPProperties.UnicastAddresses(i).Address
    116. BindSocket()
    117. End If
    118. Next
    119. End Sub
    120. Private Sub BindSocket()
    121. Try
    122. socketz.Bind(New IPEndPoint(myip, 0))
    123. socketz.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.HeaderIncluded, True)
    124. Dim bytrue() As Byte = {1, 0, 0, 0}
    125. Dim byout() As Byte = {1, 0, 0, 0}
    126. socketz.IOControl(IOControlCode.ReceiveAll, bytrue, byout)
    127. socketz.Blocking = False
    128. ReDim bytedata(socketz.ReceiveBufferSize)
    129. socketz.BeginReceive(bytedata, 0, bytedata.Length, SocketFlags.None, New AsyncCallback(AddressOf OnReceive), Nothing)
    130. ComboBox1.Enabled = False
    131. Catch ex As Exception
    132. ComboBox1.BackColor = Color.Red
    133. End Try
    134. End Sub
    135. End Class
    Interessant. Du nutzt also den kompletten Code von hier: SocketType.Raw zugriff verweigert , alternative ?
    Dort war ja schon eine Lösung. Desweiteren ist bitte bitte bitte schmeiß die ganzen komischen Try-Catch sachen raus oder behandle es richtig. So wie du es machst
    (wurde dir schon öfter gesagt) ist es halt "scheiße" wenn man das so sagen darf.

    SocketType.Raw braucht halt Admin-Rechte mit Windows 2000 und höher: docs.microsoft.com/de-de/windo…Sock/tcp-ip-raw-sockets-2
    Und dafür hat dir der RFG ja schon in Post #2 die Lösung gepostet.
    Grüße , xChRoNiKx

    Nützliche Links:
    Visual Studio Empfohlene Einstellungen | Try-Catch heißes Eisen

    xChRoNiKx schrieb:

    Desweiteren ist bitte bitte bitte schmeiß die ganzen komischen Try-Catch sachen raus oder behandle es richtig
    Jou.
    Bzw. lass das mit dem Behandeln - bring überhaupt erstmal was ans laufen.
    Fehler behandeln kann man erst, wenn der eigentliche Code ordentlich funktioniert. Mit verfrühten TryCatches schiesst man sich nur pausenlos selbst ins Knie.