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
| Private Sub StartMulticast()
//avec adipp = 10.172.1.51
Try
mcastSocket = New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Dim localIPAddr As IPAddress = IPAddress.Parse(adipp)
//IPAddress localIP = IPAddress.Any;
Dim localEP As EndPoint = CType(New IPEndPoint(localIPAddr, mcastPort), EndPoint)
mcastSocket.Bind(localEP)
mcastOption = New MulticastOption(mcastAddress, localIPAddr)
mcastSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOption)
Catch e As Exception
MsgBox(e.ToString())
End Try
End Sub
Public Sub ReceiveBroadcastMessages()
Dim done As Boolean = False
Dim bytes() As Byte = New [Byte](99) {}
Dim groupEP As New IPEndPoint(mcastAddress, mcastPort)
Dim remoteEP As EndPoint = CType(New IPEndPoint(IPAddress.Any, 0), EndPoint)
Try
mcastSocket.ReceiveTimeout = 1000
mcastSocket.ReceiveFrom(bytes, remoteEP)
toto = Encoding.ASCII.GetString(bytes, 0, bytes.Length)
//Traitement de toto
mcastSocket.Close()
Catch e As Exception
//Ne pas oublier le ".close" qui permet de pouvoir relançer l'acquisition
mcastSocket.Close()
End Try
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
mcastAddress = IPAddress.Parse("225.0.1.30")
mcastPort = 8000
//Start a multicast group.
StartMulticast()
// Receive broadcast messages.
ReceiveBroadcastMessages()
End Sub |
Partager