2

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.

Sfynx
  • 123
  • 6
  • Did you check the properties of `exchange.Request` ? – Chetan Oct 08 '18 at 08:24
  • Yep. I haven't seen any property that contains an IP address from the sender. – Sfynx Oct 08 '18 at 08:24
  • How about `Host` property of `Request`? Did you check that? – Chetan Oct 08 '18 at 08:48
  • I checked that, but I found it now. Apparently, the IP address is under Exchange.Request.Source.ToString();. I'll answer the question and solve it myself. Thanks anyways! I first did not found it because i sent a package from localhost instead of 127.0.0.1. – Sfynx Oct 08 '18 at 08:52

1 Answers1

2

Apparently, the IP Address is found under exchange.Request.Source.ToString(). If you send a package from localhost instead of 127.0.0.1, it will just say localhost and makes it harder to find.

"Uri uri = new Uri("coap://localhost:" + _port + "/" + "Test");"

EDIT: Also, maybe for future people who need this: if you need the IP address or port alone, don't split exchange.Request.Source. Visual studio autoparses exchange.Request.Source to Endpoint. This should be IPEndpoint instead of Endpoint, because it loses the "Address" and "Port" properties if it is an Endpoint. You can fix it like so:

        if (exchange.Request.Source is IPEndPoint p)
        {
            //p.Address
            //p.Port
        }
        else
        {
            //Handle errors here
        }
Sfynx
  • 123
  • 6