1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| Public WithEvents Ecoute As System.ComponentModel.BackgroundWorker
Dim ecouter_local_port As Integer
Dim ecouter_hote_ip_distant As IPAddress
Dim ecouter_hote_port_distant As Integer
Dim parametre_ecoute As String
Dim boucle As Boolean
Dim preparation_ecoute As String
Function GetMAC(ByVal IPAddress As String) As String
Dim addr As Net.IPAddress = Net.IPAddress.Parse(IPAddress)
Dim mac() As Byte = New Byte(6) {}
Dim len As Integer = mac.Length
SendARP(CUInt(addr.Address), 0, mac, len)
Dim macAddress As String = BitConverter.ToString(mac, 0, len)
Return macAddress
End Function
[...une fonction lister interface qui récupère les couples IPv4/MAC des interface dispo et une fonction envoyer paquet udp...]
Public Sub initialiser_ecoute()
Ecoute = New System.ComponentModel.BackgroundWorker
' on configure le thread
Ecoute.WorkerReportsProgress = True
Ecoute.WorkerSupportsCancellation = True
End Sub
Public Sub recevoir_message(ByVal local_p As Integer, ByVal ip_src As String, ByVal port_src As Integer, ByVal boucle_infini As Boolean)
boucle = boucle_infini
parametre_ecoute = local_p & "#" & ip_src & "#" & port_src
If Ecoute.IsBusy = False Then
Ecoute.RunWorkerAsync(parametre_ecoute)
End If
End Sub
Private Sub Ecoute_démaré(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Ecoute.DoWork
'declaration
Dim lport As Integer
Dim dip_string As String
Dim dip As IPAddress
Dim dport As String
Dim parametre As String
Dim car_scanne As String
Dim pos_1 As Integer = Nothing
Dim pos_2 As Integer = Nothing
Dim flag_nb As Boolean = False
parametre = e.Argument
For pos_scan_car As Integer = 0 To parametre.Count() - 1 Step 1
car_scanne = parametre.Chars(pos_scan_car)
If car_scanne = "#" Then
If flag_nb = False Then
pos_1 = pos_scan_car
flag_nb = True
Else
pos_2 = pos_scan_car
End If
End If
Next
lport = CInt(parametre.Substring(0, pos_1))
dip_string = parametre.Substring(pos_1 + 1, pos_2 - pos_1 - 1)
dport = parametre.Substring(pos_2 + 1, parametre.Count() - pos_2 - 1)
If dip_string = "any" Then
dip = IPAddress.Any
Else
dip = IPAddress.Parse(dip_string)
End If
'On déclare une connexion UDP
Dim receivingUdpClient As New UdpClient(lport)
'On désigne un hote distant : tout le monde
Dim RemoteIP As New System.Net.IPEndPoint(dip, dport)
'On lance la réceptions des donnée
Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIP)
'on convertis les données recu
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
'on prépare l'expédition du résultat au process principal
e.Result = returnData
'on ferme la connexion UDP
receivingUdpClient.Close()
'on stop le thread secondaire tout en renvoyant le résultat
System.Threading.Thread.Sleep(1)
End Sub
Private Sub Ecoute_terminé(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Ecoute.RunWorkerCompleted
Dim recup_data As String = e.Result
traiter_message_recu(recup_data)
End Sub
Private Sub traiter_message_recu(ByVal data As String)
Form1.Label1.Text = data
If boucle = True Then
Ecoute.RunWorkerAsync(parametre_ecoute)
End If
End Sub |
Partager