Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello, I am trying to use connectionless async UDP listener.I need to get Remote Endpoint to use in processes and i can get remote Endpoint info with udpClient.EndReceive(ar, IP_Remote_End) command.
But as you can see I am stopping the listener for a few milisecs.
I lost a few packets in this process and I don't want to.
Is there any way to get Remote End IP without stopping the listener?

Thanks in advance
Kıvanç

VB
Public Class Form1
Dim udpClient As New Sockets.UdpClient(5070)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

udpClient.BeginReceive(AddressOf Packet_Received, udpClient)

End Sub
Private Sub Packet_Received(ByVal ar As IAsyncResult)

Dim IP_Remote_End As IPEndPoint = Nothing
 udpClient.EndReceive(ar, IP_Remote_End)
udpClient.BeginReceive(AddressOf Packet_Received, Nothing)

End Sub



When I try udpclient.client.remoteendpoint, I receive this exception

SocketErrorCode NotConnected {10057} at System.Net.Sockets.Socket.get_RemoteEndPoint()

Message "A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied"
Posted
Updated 13-May-10 13:05pm
v5

What you need is a "state" class you can pass along. This state class can hold a reference to the EndPoint passed to the UdpClient. I'm not sure how well the example holds up with BeginReceive (especially with calling it again to continue listening) as I've only really used BeginReceiveFrom - which works similar.

Something like this:
Private Class UdpState
    Public SendingSocket As UdpClient
    Public ReceivingEndpoint As EndPoint
End Class


Then you declare and start your receive like this:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim ep As New IPEndPoint(IPAddress.Any, 5070)
    Dim u As New UdpClient(ep)

    Dim s As New UdpState()
    s.SendingSocket = u
    s.ReceivingEndpoint = ep

    u.BeginReceive(New AsyncCallback(AddressOf Packet_Received), s)
End Sub


And finally in the callback to get the ip address
Private Sub Packet_Received(ByVal ar As IAsyncResult)
    Dim s As UdpState = CType(ar.AsyncState, UdpState)
    Dim IP_Remote_End As IPEndPoint = TryCast(s.ReceivingEndpoint, IPEndPoint)
End Sub


I pulled this example from the MSDN page for the BeginReceive method (http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.beginreceive.aspx) which happened to closely correspond to my own code for the BeginReceiveFrom method.

Hope that helps.
 
Share this answer
 
I haven't tried this, but;

As the BeginReceive is non-blocking, and you have declared udpClient as instance variable, you should be able to access the underlying socket and remote endpoint from;

udpClient.Client.RemoteEndpoint
 
Share this answer
 
Comments
kivanc.imer 12-May-10 13:19pm    
Hello
I tried but i doesn't work.
Do you know how udpclient.Client.ReceiveFromAsync works? i couldn't find any sample code..

Thanks
Kivanç
Adding an answer here since I found this, it did not help, but eventually I worked it out.

Use the BeginReceiveMessageFrom method, and before you Bind(), for good measure, set the following option:

s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);

This will ensure that all your calls to your async receive callback will contain the endpoint information and packet information, etc.

Also make sure you store the IAsyncResult returned from BeginReceiveMessageFrom and check in your callback that the IAsyncResult parameter == the stored one, otherwise you will get an exception under certain circumstances.
 
Share this answer
 
You could use Socket class and its BeginReceiveFrom method to get remote EP.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900