I'm using Californium API ( https://github.com/eclipse/californium ) to send data using CoAP protocol.
Below is the snippet for both client and server.
Server :
public class HelloWorldServer extends CoapServer {
/*
* Application entry point.
*/
public static void main(String[] args) {
try {
// create server
HelloWorldServer server = new HelloWorldServer();
server.start();
} catch (SocketException e) {
System.err
.println("Failed to initialize server: " + e.getMessage());
}
}
/*
* Constructor for a new Hello-World server. Here, the resources of the
* server are initialized.
*/
public HelloWorldServer() throws SocketException {
// provide an instance of a Hello-World resource
add(new HelloWorldResource());
}
/*
* Definition of the Hello-World Resource
*/
class HelloWorldResource extends CoapResource {
public HelloWorldResource() {
// set resource identifier
super("helloWorld");
// set display name
getAttributes().setTitle("Hello-World Resource");
}
@Override
public void handleGET(CoapExchange exchange) {
// respond to the request
exchange.respond("Hello World!");
}
@Override
public void handlePOST(CoapExchange exchange){
//System.out.println("Start "+System.currentTimeMillis());
exchange.accept();
//List<String> queries = exchange.getRequestOptions().getURIQueries();
// System.out.println("Text Received : "+ exchange.getRequestText().length());
// System.out.println("End "+System.currentTimeMillis());
exchange.respond("Received");
}
}
}
Client code :
try {
long startTime = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
CoapClient client = new CoapClient(new URI("coap://192.168.15.170:5683/helloWorld"));
CoapResponse response = client.post(str, 0);
}
System.out.println("done");
} catch (URISyntaxException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
I'm sending 1000000 records but while sending first it send 65535 records and it wait for few seconds.after waiting for few seconds then it again starts sending.
System Details :
OS : Win 7 64 bit. RAM : 4 GM
Why it wait after 65535 records?
I gone through the Californium.CoAP receives 250 records per second. I have added relay of 4 ms.so that it manage to send less than 250 records/second.
If you sending data from different machine to same CoAP server resource, you need to manage delay accordingly.
CoAP Message Id (MID) ranginf from 1-65565 only.
Also configure Californium.properties properly to work.