If you are using TCP, or connected UDP, use the Socket.LocalEndPoint
and Socket.RemoteEndPoint
properties.
If you are using connection-less UDP, you should be using Begin/EndReceiveFrom()
instead of Begin/EndReceive()
. The callback provides an EndPoint
for the sender.
Either way, given an EndPoint
object, cast it to an IPEndPoint
and use its Address
property to access the IP address.