我正在使用Californium API ( https://github.com/eclipse/californium ) 使用 CoAP 协议发送数据。
下面是客户端和服务器的片段。
服务器 :
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");
}
}
}
客户端代码:
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();
}
我正在发送1000000 条记录,但首先发送时它发送 65535 条记录并等待几秒钟。等待几秒钟后,它再次开始发送。
系统详情:
操作系统:Win 7 64 位。内存:4 GM
为什么在 65535 条记录后等待?