In C#, I'm trying to get the IP address of the CoAP client. Is this even possible? I've tried looking in the exchange object I receive, but I can't seem to find the IP address.
Client
class Program
{
private static string _port = "5683";
static void Main(string[] args)
{
Request request = new Request(Method.GET);
Uri uri = new Uri("coap://127.0.0.1:" + _port + "/" + "Test");
request.URI = uri;
byte[] payload = Encoding.ASCII.GetBytes("test");
request.Payload = payload;
request.Send();
// wait for one response
Response response = request.WaitForResponse();
Debug.WriteLine(response);
}
}
Server
public Task<string> OpenAsync(CancellationToken cancellationToken)
{
try
{
_server = new CoapServer(_port);
_server.Add(new MessageResource(_path);
_server.Start();
} catch (Exception ex)
{
throw;
}
}
Message Resource (used for the server)
public class MessageResource : CoAP.Server.Resources.Resource
{
public MessageResource(string path) : base(path)
{
}
protected async override void DoGet(CoapExchange exchange)
{
try
{
var payload = exchange.Request.Payload;
if (payload != null)
{
exchange.Respond(payloadString);
} else
{
throw new Exception("Payload is null. No actor has been made.");
}
}
catch (Exception ex)
{
throw;
}
}
}
As you can see, I would like to receive the IP address of the client who sent the message. I've tried checking all the properties from the exchange object, but I can't seem to find an IP address I can use.