DTLS编写样例一coap协议
来源:互联网 发布:编译linux内核 编辑:程序博客网 时间:2024/06/01 09:53
参考地址:https://github.com/nikosft/CoAP-examples/tree/master/src/DTLS
代码1:DTLS.java
/* * Copyright (C) 2015 fotiou * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */import java.io.BufferedReader;import java.io.FileInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.net.InetSocketAddress;import java.security.KeyStore;import java.security.PrivateKey;import java.security.cert.Certificate;import org.eclipse.californium.core.CoapClient;import org.eclipse.californium.core.CoapServer;import org.eclipse.californium.core.network.CoAPEndpoint;import org.eclipse.californium.core.network.config.NetworkConfig;import org.eclipse.californium.scandium.DTLSConnector;import org.eclipse.californium.scandium.config.DtlsConnectorConfig;import org.eclipse.californium.scandium.dtls.cipher.CipherSuite;/** * A basic Hello World example over DTLS. The server and the client are located in the * same machine. This class contains the main method that creates the server and the * client. Use the GenKeys.sh to generate the the keystore and the truststore. In * this example only the server is authenticated, using a self-signed certificate. * @author fotiou */public class DTLS { //If you have modified GenKeys.sh modify the following variables accordingly private final static String KEY_STORE_PASSWORD = "123456"; private static final String KEY_STORE_LOCATION = "keyStore.jks"; private final static String TRUST_STORE_PASSWORD = "123456"; private static final String TRUST_STORE_LOCATION = "trustStore.jks"; public static void main(String[] args) { //prepapre server CoapServer server = new CoapServer(); //create a new resource HelloWorldResource resource = new HelloWorldResource(); //add the resource to the server server.add(resource); //prepare client CoapClient client = new CoapClient("coap://localhost:5684/helloWorld"); //Creates a new handler HelloWorldHandler handler = new HelloWorldHandler(); try { //Here starts DTLS configuration of the server // load the key store KeyStore keyStore = KeyStore.getInstance("JKS"); InputStream in = new FileInputStream(KEY_STORE_LOCATION); keyStore.load(in, KEY_STORE_PASSWORD.toCharArray()); DtlsConnectorConfig.Builder serverConfig = new DtlsConnectorConfig.Builder(new InetSocketAddress(5684)); serverConfig.setSupportedCipherSuites(new CipherSuite[]{ CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8}); serverConfig.setIdentity((PrivateKey)keyStore.getKey("server", KEY_STORE_PASSWORD.toCharArray()), keyStore.getCertificateChain("server"), true); serverConfig.setClientAuthenticationRequired(false); DTLSConnector serverConnector = new DTLSConnector(serverConfig.build(), null); server.addEndpoint(new CoAPEndpoint(serverConnector, NetworkConfig.getStandard())); server.start(); //Here starts DTLS configuration of the client //load the trust store KeyStore trustStore = KeyStore.getInstance("JKS"); InputStream inTrust = new FileInputStream(TRUST_STORE_LOCATION); trustStore.load(inTrust, TRUST_STORE_PASSWORD.toCharArray()); //load certificates Certificate[] trustedCertificates = new Certificate[1]; trustedCertificates[0] = trustStore.getCertificate("server"); DtlsConnectorConfig.Builder clientConfig = new DtlsConnectorConfig.Builder(new InetSocketAddress(0)); clientConfig.setTrustStore(trustedCertificates); clientConfig.setSupportedCipherSuites(new CipherSuite[]{ CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8}); DTLSConnector clientConnector = new DTLSConnector(clientConfig.build(), null); client.setEndpoint(new CoAPEndpoint(clientConnector, NetworkConfig.getStandard())); //start the client client.get(handler); //Just a trick to wait until the user press enter System.out.println("Press enter to close the server and end the program"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br.readLine(); //Stop the server server.destroy(); } catch (Exception e) { System.out.println("Exception " + e.toString()); e.printStackTrace(); } }}
代码2:HelloWorldHandler.java
import org.eclipse.californium.core.CoapHandler;import org.eclipse.californium.core.CoapResponse;/** * This is a simple Handler. It prints the server's response or if an error occurs * it prints the error. * @author fotiou */public class HelloWorldHandler implements CoapHandler{ /** * It is called when the CoAP server responds with some content. It prints the content of the * response. * @param response The response of the server */ @Override public void onLoad(CoapResponse response) { String content = response.getResponseText();System.out.println("Received response from Server : " + content); } /** * It is called when an error occurs. */ @Override public void onError() { System.out.println("Error"); } }
代码3:HelloWorldResource.java
import org.eclipse.californium.core.CoapResource;import org.eclipse.californium.core.server.resources.CoapExchange;/** * This is a basic resource. It can be accessed using the GET method and returns * the string ''Hello World'' * @author Nikos Fotiou */public class HelloWorldResource extends CoapResource { /** * The constructor */ public HelloWorldResource() { // set resource identifier super("helloWorld"); } /** * It is called when a GET method is received. * @param exchange The user request */ @Override public void handleGET(CoapExchange exchange) { exchange.respond("Hello World!!"); } }
0 0
- DTLS编写样例一coap协议
- CoAP协议
- CoAP协议
- CoAP协议笔记[RFC7252]
- CoAP-协议格式详解
- CoAP协议简介
- COAP协议说明
- CoAP协议调研
- COAP协议全面分析
- CoAP协议学习——CoAP基础
- CoAP协议学习——CoAP基础
- CoAP协议学习——CoAP基础
- CoAP协议学习——CoAP基础
- DTLS
- 物联网的coap协议
- DTLS编写样例二(基于DTLSConnector)
- DTLS协议 RFC4347翻译(未完成版)
- 借助mbedTLS了解DTLS握手协议
- mongodb[零]准备数据,方便测试
- 应用图标数字
- 安装zookeeper
- qt信号关联
- android 自定义view实现刮刮卡效果
- DTLS编写样例一coap协议
- 三个水杯 搜索题 bfs
- 解决ubuntu下面firefox安装银联插件的问题
- android音视频系列操作,录制,压缩
- H5万能选择器:iosselect
- 写高质量OC代码52建议总结:31.在dealloc方法中只释放引用并解除监听
- “惊群”,看看nginx是怎么解决它的
- 大数据查询网址(整理)
- Anyty C++DLL封装成NetDLL进行调用