我正在构建一个可以将CoAP消息发送给另一个对等方(不同的实现)的工具,但我遇到了困难。我正在使用名为" Californium"的CoAP库。我正在开发java / eclipse中的工具。这是交易:我通过cal"默认端点"发送消息,允许系统组成UDP"连接的源端口" 。我想使用caligoium的Server对象在同一个源端口上侦听,但是我收到以下错误:
SEVERE: Could not start endpoint
java.net.BindException: Address already in use
所以我的问题是:我如何首先发送CoAP消息并使用Cal来开始在同一套接字上侦听其他CoAP消息?
以下是客户端的java代码。它的作用是"注册"使用在CoAP之上分层的特定协议。注册后,我希望它重新使用UDP套接字来监听我之前注册的实体的后续消息。
注意: 当我明确告诉它听某个端口(例如5683)时,客户端的服务器部分工作,遗漏寄存器部分并测试它使用Firefox Addon" 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<=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;
}
}
答案 0 :(得分:2)
您需要首先绑定UDP套接字,然后启动LWM2M寄存器。
因为你做了什么:创建CoAP端点(绑定一个udp服务器)而不是你在listen方法中再次绑定。
// 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<=86400&b=U");
request.setPayload("</1/1/0>");
Endpoint endpoint = EndpointManager.getEndpointManager().getDefaultEndpoint();
request.send(endpoint);
您仍然可以使用coap:// localhost:5555
上的铜访问您的客户端