Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# VB.NET udp Networking
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ç
 
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 10-May-10 11:39am
Edited 13-May-10 14:05pm
Moak11.4K
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
kivanc.imer at 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ç
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You could use Socket class and its BeginReceiveFrom method to get remote EP.
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 335
1 OriginalGriff 250
2 DamithSL 170
3 Kornfeld Eliyahu Peter 130
4 Peter Leow 95
0 OriginalGriff 7,315
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,917
3 Maciej Los 4,866
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web02 | 2.8.141223.1 | Last Updated 16 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100