简体   繁体   English

Coap 服务器资源发现

[英]Coap server resources discovery

I am using the californium library for coap communication and it is being deployed on the Android platform.我正在使用 californium 库进行 coap 通信,它正在部署在 Android 平台上。 I have started coap server in one device and the client is on another device, both are in the same network.我已经在一台设备上启动了 coap 服务器,而客户端在另一台设备上,两者都在同一个网络中。

Server code: Creating a server with below resource服务器代码:使用以下资源创建服务器

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!");
    }
}

Client code:客户端代码:

    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();
    }

no output from the above code.上述代码中没有 output。 I don't know the IP address and I want to communicate with the server.不知道IP地址,想和服务器通信。 is this the right way or I am missing something?这是正确的方法还是我错过了什么?

A "discover" is a special coap request, to discover the resources of a known host. “发现”是一个特殊的 coap 请求,用于发现已知主机的资源。

To discover the host, a mutlicast request may be used, but must be supported by the server.要发现主机,可以使用多播请求,但必须得到服务器的支持。

"localhost" is the address of the device itself. “localhost”是设备本身的地址。 With that, the device will send the discover-request to itself.这样,设备将向自身发送发现请求。

I don't know the IP address and I want to communicate with the server.不知道IP地址,想和服务器通信。

If you dont't know the address, you might either use a "multicast" request first (requires also preparation on the server side, see MulticastServer Example . That example is more complex, than you will need. But demonstrates the principles as well).如果您不知道地址,您可以先使用“多播”请求(还需要在服务器端进行准备,请参阅MulticastServer 示例。该示例比您需要的更复杂。但也演示了原理) . Or you need to lookup that address (android: settings -> Connections -> WLAN -> Settings-Icon: there you see your IP-address.或者您需要查找该地址(android:设置-> 连接-> WLAN-> 设置-图标:在那里您可以看到您的 IP 地址。

coapClient.discover()

Discovers a "known" server, not the local-network.发现“已知”服务器,而不是本地网络。 Replace the localhost with "californium.eclipseprojects.io" and you will get:将 localhost 替换为“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]

All the received links are relative to the server you send the discover to.所有收到的链接都与您将发现发送到的服务器相关。

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

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