【问题标题】:Coap server resources discoveryCoap服务器资源发现
【发布时间】:2021-05-22 10:18:27
【问题描述】:

我正在使用 californium 库进行 coap 通信,它正在 Android 平台上部署。我已经在一台设备上启动了 coap 服务器,而客户端在另一台设备上,两者都在同一个网络中。

服务器代码: 使用以下资源创建服务器

class HelloWorldResource extends CoapResource {

    public HelloWorldResource() {

        // set resource identifier
        super("hello");

        // set display name
        getAttributes().setTitle("Hello-World Resource");
    }

    @Override
    public void handleGET(CoapExchange exchange) {

        // respond to the request
        exchange.respond("Hello Android!");
    }
}

客户端代码:

    CoapClient coapClient = new CoapClient("coap://localhost/.well-known/core");

    try {
        Set<WebLink> webLinks = coapClient.discover();
        System.out.println(webLinks.size());
    } catch (ConnectorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

上述代码没有输出。我不知道 IP 地址,我想与服务器通信。这是正确的方法还是我错过了什么?

【问题讨论】:

    标签: android coap californium


    【解决方案1】:

    “发现”是一个特殊的 coap 请求,用于发现已知主机的资源。

    要发现主机,可以使用多播请求,但必须得到服务器的支持。

    “localhost”是设备本身的地址。这样,设备就会向自己发送发现请求。

    我不知道 IP 地址,我想与服务器通信。

    如果您不知道地址,您可以先使用“多播”请求(还需要在服务器端进行准备,请参阅MulticastServer Example。该示例比您需要的更复杂。但演示了原则)。或者您需要查找该地址(android: settings -> Connections -> WLAN -> Settings-Icon :在那里您可以看到您的 IP 地址。

    coapClient.discover()
    

    发现“已知”服务器,而不是本地网络。将本地主机替换为“californium.eclipseprojects.io”,您将得到:

    </.well-known/core>
    </large> Large resource
        rt: [block]
        sz: [1280]
    </large-create> Large resource that can be created using POST method
        rt: [block]
    </large-post> Handle POST with two-way blockwise transfer
        rt: [block]
    </large-separate> Large resource
        rt: [block]
        sz: [1280]
    </large-update> Large resource that can be updated using PUT method
        rt: [block]
        sz: [1280]
    </link1> Link test resource
        rt: [Type1, Type2]
        if: [If1]
    </link2> Link test resource
        rt: [Type2, Type3]
        if: [If2]
    </link3> Link test resource
        rt: [Type1, Type3]
        if: [foo]
    </location-query> Perform POST transaction with responses containing several Location-Query options (CON mode)
    </multi-format> Resource that exists in different content formats (text/plain utf8 and application/xml)
        ct: [0, 41, 50, 60]
    </obs> Observable resource which changes every 5 seconds
        rt: [observe]
        obs:    []
    </obs-large> Observable resource which changes every 5 seconds
        rt: [observe]
        obs:    []
    </obs-non> Observable resource which changes every 5 seconds
        rt: [observe]
        obs:    []
    </obs-pumping> Observable resource which changes every 5 seconds
        rt: [observe]
        obs:    []
    </obs-pumping-non> Observable resource which changes every 5 seconds
        rt: [observe]
        obs:    []
    </obs-reset>
    </path> Hierarchical link description entry
        ct: [40]
    </path/sub1> Hierarchical link description sub-resource
    </path/sub2> Hierarchical link description sub-resource
    </path/sub3> Hierarchical link description sub-resource
    </query> Resource accepting query parameters
    </seg1> Long path resource
    </seg1/seg2> Long path resource
    </seg1/seg2/seg3> Long path resource
    </separate> Resource which cannot be served immediately and which cannot be acknowledged in a piggy-backed way
    </shutdown>
    </test> Default test resource
    </validate> Resource which varies
        ct: [0]
        sz: [20]
    

    所有接收到的链接都与您将发现发送到的服务器相关。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多