0

I have a service in VB.NET (2010) that is receiving a UDP packets with UDPClient.BeginReceive asynchronously. Everything is working fine, I'm able to receive a packet data - but I'm not able to get sender (or remote) endpoint.

There is a CallBack routine that is receiving an IAsyncResult object, that is represented by System.net.sockets.OverlappedAsyncResult where in property SocketAddress is a byte array where is a sender port and sender IP address. But it is accessible only in debug time - I'm using a breakpoint in async callback routine.

Im unable to cast IAsyncResult as OverlappedAsyncResult and I'm not able to access the socket address in design time. It is giving me an error that means that object is not found and it can't be declared.

I'm attaching a class that I use for it for reference. Is there any better way to do it, or can I get the sender IP?

Imports System.Net
Imports System.Net.Sockets
Imports System.Text

Public Class UDPInterface
Implements IDisposable

#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls

' IDisposable
Protected Overridable Sub Dispose(disposing As Boolean)
    If Not Me.disposedValue Then
        If disposing Then
            ' TODO: dispose managed state (managed objects).
        End If

        ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below.
        ' TODO: set large fields to null.
    End If
    Me.disposedValue = True
End Sub

' TODO: override Finalize() only if Dispose(ByVal disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
'    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
'    Dispose(False)
'    MyBase.Finalize()
'End Sub

' This code added by Visual Basic to correctly implement the disposable pattern.
Public Sub Dispose() Implements IDisposable.Dispose
    ' Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.
    Dispose(True)
    GC.SuppressFinalize(Me)
End Sub
#End Region

Private _EP As UdpClient
Private _RemIPEP As System.Net.IPEndPoint
Private _Port As Integer

Public Sub New(Port As Integer)
    _Port = Port
    _RemIPEP = New IPEndPoint(System.Net.IPAddress.Any, Port)
    _EP = New UdpClient
    _EP.Client.Bind(_RemIPEP)
    _EP.BeginReceive(AddressOf Receive, _EP)

End Sub

Public Sub SendPacket(Bytes As Byte())
    _EP.Send(Bytes, Bytes.Length, New IPEndPoint(System.Net.IPAddress.Broadcast, _Port))
End Sub

Private Sub Receive(rec As IAsyncResult)
    Dim RX As Byte() = _EP.EndReceive(rec, _RemIPEP)

    Debug.Print(Encoding.ASCII.GetString(RX))

    _EP.BeginReceive(AddressOf Receive, _EP)
    RaiseEvent PacketReceived(RX)

End Sub
Public Event PacketReceived(Bytes As Byte())

End Class
  • 1
    Not fluent in VB but I´d expect that the _RemIPEP variable (shouldn´t it be passed as ByRef?) would contain the remote host after EndReceive is called. – C. Gonzalez Sep 18 '17 at 11:48
  • _RemIPEP is endpoint on which the UDPClient is bound, but as I'm looking on it now, it is not true may be. I will give it a try and let you know, thank you! – Jan Vojtěch Vaníček Sep 18 '17 at 12:19
  • Mr. Gonzalez, you are completely right. I created new IPEndpoint inside a callback procedure. `Dim REP As New IPEndPoint(IPAddress.Any, 1)` and sender IP address was assigned to this object on .EndReceive call. Thank you so much! – Jan Vojtěch Vaníček Sep 18 '17 at 12:40

0 Answers0