title: [Californium/CoAP/LWM2M]: Reusing message send endpoint for server not possible?
body: <p>I am building a tool that can send CoAP messages to another peer (different implementation), but I am having difficulties. I am using the CoAP library called "Californium" and am developing the tool in java/eclipse. Here's the deal: I send a message over californium's "default endpoint", which allows the system to make up a source-port for the UDP "connection". I want to listen on this same source-port using californium's Server object, but I am getting the following error:</p>
  
  <pre><code>SEVERE: Could not start endpoint
  java.net.BindException: Address already in use
  </code></pre>
  
  <p>So my question is: how do I first send a CoAP message and start listening for other CoAP messages on the same socket using Californium?</p>
  
  <p>Below is the java code for the client. What it does is "register" using a certain protocol layered on top of CoAP. After registering I want it to re-use the UDP socket for listening for subsequent messages of the entity I registered with earlier.</p>
  
  <p><strong><em>NOTE:</em></strong> The server part of the client works when I explicitly tell it to listen to a certain port (e.g. 5683), leave out the register part and test it with the Firefox Addon "Copper" (i.e. Copper can get to the /1 /1/1 /1/1/0 resources).</p>
  
  <pre><code>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&amp;lt=86400&amp;b=U");
          request.setPayload("&lt;/1/1/0&gt;");
          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;
      }
  
  }
  </code></pre>