简体   繁体   English

如何从 C# 中的接收 CoAP 包中获取 IP 地址?

[英]How to get an IP address from a receiving CoAP package in C#?

In C#, I'm trying to get the IP address of the CoAP client.在 C# 中,我试图获取 CoAP 客户端的 IP 地址。 Is this even possible?这甚至可能吗? I've tried looking in the exchange object I receive, but I can't seem to find the IP address.我尝试查看收到的交换对象,但似乎找不到 IP 地址。

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.如您所见,我想接收发送消息的客户端的 IP 地址。 I've tried checking all the properties from the exchange object, but I can't seem to find an IP address I can use.我已经尝试检查交换对象的所有属性,但似乎找不到可以使用的 IP 地址。

Apparently, the IP Address is found under exchange.Request.Source.ToString().显然,IP 地址是在 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.如果你从 localhost 而不是 127.0.0.1 发送一个包,它只会说 localhost 并且更难找到。

"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.编辑:另外,也许对于未来需要这个的人:如果你只需要 IP 地址或端口,不要拆分 exchange.Request.Source。 Visual studio autoparses exchange.Request.Source to Endpoint. Visual Studio 将 exchange.Request.Source 自动解析为 Endpoint。 This should be IPEndpoint instead of Endpoint, because it loses the "Address" and "Port" properties if it is an Endpoint.这应该是 IPEndpoint 而不是 Endpoint,因为如果它是 Endpoint,它会丢失“Address”和“Port”属性。 You can fix it like so:你可以像这样修复它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM