Problem gettin a RemoteEndPoint from a UDP packet

P

PH

Hi guys!



I need to get the remote EndPoint from when I receive packets when
listening in a local port in my computer.

I'm using UDP (connection-less) so I only bind the socket to my
localEndPoint, I do not Bind(), Connect() to a RemoteEndPoint.

I'm using BeginReceive() to listen and receive from connection
Asynchronously.



In this way how do I get the RemoteEndPoint?



Thanks guys!
 
N

Nicholas Paldino [.NET/C# MVP]

PH,

Don't you make a call to Accept somewhere? The socket that is returned from that call should have the RemoteEndPoint populated and you should be able to get the information about the client from there.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Hi guys!



I need to get the remote EndPoint from when I receive packets when listening in a local port in my computer.

I'm using UDP (connection-less) so I only bind the socket to my localEndPoint, I do not Bind(), Connect() to a RemoteEndPoint.

I'm using BeginReceive() to listen and receive from connection Asynchronously.



In this way how do I get the RemoteEndPoint?



Thanks guys!
 
P

PH

Hi Nicholas, thanks for your answer;



Remember that this is connection-less.

I do not make any kind of connection.

I listen for incoming connections Binding to a localEndPoint. And then
using BeginReceive()



So you think that I can get the RemoteEndPoint in this way?





Thanks again





_____

From: Nicholas Paldino [.NET/C# MVP]
[mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 12:41 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet


PH,



Don't you make a call to Accept somewhere? The socket that is
returned from that call should have the RemoteEndPoint populated and you
should be able to get the information about the client from there.



Hope this helps.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi guys!



I need to get the remote EndPoint from when I receive packets when
listening in a local port in my computer.

I'm using UDP (connection-less) so I only bind the socket to my
localEndPoint, I do not Bind(), Connect() to a RemoteEndPoint.

I'm using BeginReceive() to listen and receive from connection
Asynchronously.



In this way how do I get the RemoteEndPoint?



Thanks guys!
 
N

Nicholas Paldino [.NET/C# MVP]

Ph,

If that is the case, then I would have your clients send the IP information as part of the information they are sending, if you need to identify them.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi Nicholas, thanks for your answer;



Remember that this is connection-less.

I do not make any kind of connection.

I listen for incoming connections Binding to a localEndPoint. And then using BeginReceive()



So you think that I can get the RemoteEndPoint in this way?





Thanks again






------------------------------------------------------------------------------

From: Nicholas Paldino [.NET/C# MVP] [mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 12:41 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet


PH,



Don't you make a call to Accept somewhere? The socket that is returned from that call should have the RemoteEndPoint populated and you should be able to get the information about the client from there.



Hope this helps.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi guys!



I need to get the remote EndPoint from when I receive packets when listening in a local port in my computer.

I'm using UDP (connection-less) so I only bind the socket to my localEndPoint, I do not Bind(), Connect() to a RemoteEndPoint.

I'm using BeginReceive() to listen and receive from connection Asynchronously.



In this way how do I get the RemoteEndPoint?



Thanks guys!
 
P

PH

This is the thing;



I receive a packet, I have to process it, and then I have to send the
response packet back to the same IP and port where the original packet
was coming from.

Having said that, and considering that there is no connection of any
kind between the two machines (needles because is UDP) there has to be a
way out there to do just that.



What do you think?



Thanks Nicholas



_____

From: Nicholas Paldino [.NET/C# MVP]
[mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 1:51 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet


Ph,



If that is the case, then I would have your clients send the IP
information as part of the information they are sending, if you need to
identify them.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Hi Nicholas, thanks for your answer;



Remember that this is connection-less.

I do not make any kind of connection.

I listen for incoming connections Binding to a localEndPoint. And then
using BeginReceive()



So you think that I can get the RemoteEndPoint in this way?





Thanks again






_____


From: Nicholas Paldino [.NET/C# MVP]
[mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 12:41 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet


PH,



Don't you make a call to Accept somewhere? The socket that is
returned from that call should have the RemoteEndPoint populated and you
should be able to get the information about the client from there.



Hope this helps.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi guys!



I need to get the remote EndPoint from when I receive packets when
listening in a local port in my computer.

I'm using UDP (connection-less) so I only bind the socket to my
localEndPoint, I do not Bind(), Connect() to a RemoteEndPoint.

I'm using BeginReceive() to listen and receive from connection
Asynchronously.



In this way how do I get the RemoteEndPoint?



Thanks guys!
 
N

Nicholas Paldino [.NET/C# MVP]

PH,

You will have to make it part of the protocol that the client sends the IP address to get a response from.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
This is the thing;



I receive a packet, I have to process it, and then I have to send the response packet back to the same IP and port where the original packet was coming from.

Having said that, and considering that there is no connection of any kind between the two machines (needles because is UDP) there has to be a way out there to do just that.



What do you think?



Thanks Nicholas




------------------------------------------------------------------------------

From: Nicholas Paldino [.NET/C# MVP] [mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 1:51 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet


Ph,



If that is the case, then I would have your clients send the IP information as part of the information they are sending, if you need to identify them.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)




Hi Nicholas, thanks for your answer;



Remember that this is connection-less.

I do not make any kind of connection.

I listen for incoming connections Binding to a localEndPoint. And then using BeginReceive()



So you think that I can get the RemoteEndPoint in this way?





Thanks again






----------------------------------------------------------------------------

From: Nicholas Paldino [.NET/C# MVP] [mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 12:41 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet


PH,



Don't you make a call to Accept somewhere? The socket that is returned from that call should have the RemoteEndPoint populated and you should be able to get the information about the client from there.



Hope this helps.




--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi guys!



I need to get the remote EndPoint from when I receive packets when listening in a local port in my computer.

I'm using UDP (connection-less) so I only bind the socket to my localEndPoint, I do not Bind(), Connect() to a RemoteEndPoint.

I'm using BeginReceive() to listen and receive from connection Asynchronously.



In this way how do I get the RemoteEndPoint?



Thanks guys!
 
J

JustinC

Hello PH, I have had a similar problem myself, and I used the same
solution that Nicholas is telling you. If you can have the clients send
their IP address with the packet, that is the best solution. The IP
address can be a simple 4 bytes at the beginning or end of your
packets.

If you need to be able to get RemoteEndPoint information from the
packet, I think you would have to switch to TCP. As far as I know UDP,
since it is connectionless, does not have this information. It could
also be that .NET does not share this information for UDP. All I know
is I ran into the same problem, and now I pre-pend IP address and port
information to the beginning of each of my packages.

Best of luck ~ Justin
 
G

Guest

PH,
You don't specify exactly how you are using BeginReceive. However if you
pass the Socket in your State object, you should be able to do something like
the following (this is from some Syslog UDP message listening code I have):

EndPoint epSource = new
IPEndPoint(((IPEndPoint)state.remoteEndPoint).Address,((IPEndPoint)state.remoteEndPoint).Port);
string sFromIPAndPort = epSource.ToString(); //ep.ToString();

Hope that helps.
Peter

--
Co-founder,
Eggheadcafe.comhttp://msdn.microsoft.com/wn3/aspx/postui.aspx?mid=34b2004f-e2d3-49af-8848-5969b3cf1a5b&cat=&lang=en&cr=US&guid=&sloc=en-us&dg=microsoft.public.dotnet.languages.csharp&base=http://msdn.microsoft.com/newsgroup...soft.com/newsgroups/settings.xml&sdgloc=en-US
developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
 
P

PH

Hi Justin, thanks for your reply.


I can't to anything with the incoming packets, because I don't control,
the client. Another thing is that the protocol used works over UDP, so
TCP is not an option.

What's killing me is that obviously there should be a way to accomplish
this.
Think about this, how other people are using UDP to receive packets, and
then send a response back? They can only do this if they know where the
packet is coming from.

Thanks again.




-----Original Message-----
From: JustinC [mailto:[email protected]]
Posted At: Wednesday, March 08, 2006 2:58 PM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Problem gettin a RemoteEndPoint from a UDP packet
Subject: Re: Problem gettin a RemoteEndPoint from a UDP packet

Hello PH, I have had a similar problem myself, and I used the same
solution that Nicholas is telling you. If you can have the clients send
their IP address with the packet, that is the best solution. The IP
address can be a simple 4 bytes at the beginning or end of your
packets.

If you need to be able to get RemoteEndPoint information from the
packet, I think you would have to switch to TCP. As far as I know UDP,
since it is connectionless, does not have this information. It could
also be that .NET does not share this information for UDP. All I know
is I ran into the same problem, and now I pre-pend IP address and port
information to the beginning of each of my packages.

Best of luck ~ Justin
 
N

Nicholas Paldino [.NET/C# MVP]

PH,

They probably know because they have the client tell them (as I have
suggested) in the first place.
 
P

PH

When I run this code, senderRemote returns 0.0.0.0
It should return the remote endpoint, should it?

Thanks



Public Shared Sub ReceiveFrom1()
Dim hostEntry As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim endPoint As New IPEndPoint(hostEntry.AddressList(0), 11000)

Dim s As New Socket(endPoint.Address.AddressFamily,
SocketType.Dgram, ProtocolType.Udp)

' Creates an IPEndPoint to capture the identity of the sending host.
Dim sender As New IPEndPoint(IPAddress.Any, 0)
Dim senderRemote As EndPoint = CType(sender, EndPoint)

' Binding is required with ReceiveFrom calls.
s.Bind(endPoint)

Dim msg() As Byte = New [Byte](255) {}
Console.WriteLine("Waiting to receive datagrams from client...")

' This call blocks.
s.ReceiveFrom(msg, senderRemote)
s.Close()

End Sub 'ReceiveFrom1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top