UDPClient Begin received packet source address

匆匆过客 提交于 2019-12-12 04:46:10

问题


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

来源:https://stackoverflow.com/questions/46276417/udpclient-begin-received-packet-source-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!