1

我正在构建一个可以将 CoAP 消息发送到另一个对等点(不同的实现)的工具,但是我遇到了困难。我正在使用名为“Californium”的 CoAP 库,并且正在 java/eclipse 中开发该工具。这是交易:我通过 californium 的“默认端点”发送一条消息,它允许系统为 UDP“连接”构成一个源端口。我想使用 californium 的 Server 对象在同一个源端口上侦听,但出现以下错误:

SEVERE: Could not start endpoint
java.net.BindException: Address already in use

所以我的问题是:我如何首先发送 CoAP 消息并开始使用 Californium 在同一个套接字上侦听其他 CoAP 消息?

下面是客户端的java代码。它所做的是使用位于 CoAP 之上的特定协议“注册”。注册后,我希望它重新使用 UDP 套接字来监听我之前注册的实体的后续消息。

注意:当我明确告诉它监听某个端口(例如 5683)时,客户端的服务器部分工作,省略注册部分并使用 Firefox 插件“Copper”测试它(即 Copper 可以到达 /1 / 1/1 /1/1/0 资源)。

package com.example.l2mwm.client;

import java.net.InetSocketAddress;

import ch.ethz.inf.vs.californium.coap.CoAP.Code;
import ch.ethz.inf.vs.californium.coap.CoAP.ResponseCode;
import ch.ethz.inf.vs.californium.coap.CoAP;
import ch.ethz.inf.vs.californium.coap.Request;
import ch.ethz.inf.vs.californium.coap.Response;
import ch.ethz.inf.vs.californium.network.Endpoint;
import ch.ethz.inf.vs.californium.network.EndpointManager;
import ch.ethz.inf.vs.californium.server.Server;
import ch.ethz.inf.vs.californium.server.resources.CoapExchange;
import ch.ethz.inf.vs.californium.server.resources.Resource;
import ch.ethz.inf.vs.californium.server.resources.ResourceBase;

public class Main {

    public static void main(String[] args) {
        Endpoint endpoint;
        if ((endpoint = register()) != null) {
            listen(endpoint);
        } else {
            System.out.println("Couldn't register!");
        }
    }

    private static void listen(Endpoint endpoint) {
        InetSocketAddress sockAddress = endpoint.getAddress();
        int port = sockAddress.getPort();
        Server server = new Server(port);
        Resource topResource = new ResourceBase("1") {
            @Override
            public void handleGET(CoapExchange exchange) {
                exchange.respond(ResponseCode.CONTENT, "this is /1's value!");
            }

            @Override
            public String getPath() {
                return "/";
            }
        };

        Resource instanceResource = new ResourceBase("1") {
            @Override
            public void handleGET(CoapExchange exchange) {
                exchange.respond(ResponseCode.CONTENT, "this is /1/1's value!");
            }

            @Override
            public String getPath() {
                return "/1/";
            }
        };

        topResource.add(instanceResource);

        instanceResource.add(new ResourceBase("0") {
            @Override
            public void handleGET(CoapExchange exchange) {
                exchange.respond(ResponseCode.CONTENT, "this is /1/1/0's value!");
            }

            @Override
            public String getPath() {
                return "/1/1/";
            }
        });

        server.add(topResource);

        server.start();
    }

    private static Endpoint register() {
        Request request = new Request(Code.POST);
        request.setURI("localhost:5684/rd?ep=coapclient&lt=86400&b=U");
        request.setPayload("</1/1/0>");
        Endpoint endpoint = EndpointManager.getEndpointManager().getDefaultEndpoint();
        request.send(endpoint);

        Response response;
        ResponseCode responseCode = null;

        try {
            response = request.waitForResponse();
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
            return null;
        }

        responseCode = response.getCode();
        if (responseCode != CoAP.ResponseCode.CREATED) {
            return null;
        }

        return endpoint;
    }

}
4

1 回答 1

2

您需要首先绑定您的 UDP 套接字,然后启动您的 LWM2M 寄存器。

因为您所做的:创建 CoAP 端点(绑定 udp 服务器),然后在您的侦听方法中再次绑定。

  // list to the UDP post 5555
  coapServer = new Server();
  Endpoint endpoint = new CoAPEndpoint(new InetSocketAddress("localhost",5555);
  coapServer.addEndpoint(endpoint);

  // send a message to a LWM2M server:
  request request = new Request(Code.POST);
  request.setURI("iot.eclipse.org:5683/rd?ep=coapclient&lt=86400&b=U");
  request.setPayload("</1/1/0>");
  Endpoint endpoint = EndpointManager.getEndpointManager().getDefaultEndpoint();
  request.send(endpoint);

您仍然可以在 coap://localhost:5555 上使用铜线访问您的客户端

于 2014-07-10T14:51:13.737 回答