CoAP协议

目录

 

CoAP协议

CoAP架构

CoAP消息头格式

CoAP消息交换

HTTP与CoAP协议对比


CoAP协议

CoAP是约束应用协议的缩写形式。

CoAP协议在RFC 7252中规定。它是一种Web传输协议,用于受约束的节点或网络,如WSN,IoT,M2M等。因此,名称为Constrained Application Protocol。该协议针对具有较少内存和较少功率规范的物联网(IoT)设备。

由于它是为Web应用程序设计的,因此它也被称为“Web of Things Protocol”。它可用于通过Web应用程序将数据从几个字节传输到1000个字节。它存在于UDP层和应用层之间。

以下是CoAP协议的功能:
•它是非常有效的RESTful协议。 
•易于代理到HTTP /从HTTP代理。 
•它是开放的IETF标准 
•它是嵌入式Web传输协议(coap://) 
•它使用异步事务模型。 
•UDP与可靠性和多播支持绑定。
•使用GET,POST,PUT和DELETE方法。
•支持URI。 
•它使用小而简单的4字节标头。
•支持绑定到UDP,SMS和TCP。
•使用基于DTLS的PSK,RPK和证书安全性。
•使用MIME类型和HTTP响应代码的子集。
•使用内置的发现机制。

CoAP架构

CoAP架构

图1描绘了CoAP架构。如图所示,它将普通HTTP客户端扩展到具有资源约束的客户端 这些客户端称为CoAP客户端。代理设备基于HTTP协议弥合了传统环境与典型互联网环境之间的差距。同一服务器负责HTTP和CoAP协议消息。

CoAP消息头格式

CoAP消息格式

图2描绘了CoAP消息格式包括4个字节的头部,后跟令牌值(从0到8个字节)。下表提到了由4个字节组成的标题,即32位。

CoAP邮件头

描述

版本它是2位无符号整数。它提到了CoAP版本号。设置为一个。
Ť它是2位无符号整数。表示消息类型即。确认(0),不可确认(1),ACK(2)或重置(3)。
漳州灿坤它是4位无符号整数,表示令牌的长度(0到8个字节)。
它是8位无符号整数,它分为两部分即。3位类(MSB)和5位细节(LSB)。
消息ID16位无符号整数。用于匹配响应。用于检测邮件重复。

CoAP消息交换

CoAP消息交换

有两种模式可以在CoAP客户端和CoAP服务器之间交换CoAP协议消息。没有单独的回应和单独的回应。

通过单独响应,服务器通知客户端收到请求消息。这将增加处理时间,但有助于避免不必要的重传。

由于使用UDP,CoAP是不可靠的协议。因此,CoAP消息在到达目的地时会无序或丢失。

为了使CoAP成为可靠的协议,停止并等待指数退避重传功能。还引入了重复检测。

CoAP约束应用协议协议是HTTP的升级版本。

它专为资源构成应用而设计,如IoT / WSN / M2M等。它基于UDP。它使用ACK消息,使其像TCP一样可靠。与HTTP相比,它具有低延迟并且消耗更少的功率。 

HTTP与CoAP协议对比

HTTP连接

图1描述了客户端和服务器之间使用的HTTP协议作为不安全连接。HTTP的HTTPS版本用于提供安全连接。HTTP主要用于查看网页或网站。该协议是为基于互联网的应用程序和设备而设计的,其中不存在电源和任何其他组件。它是基于TCP的协议。 

以下是CoAP和HTTP协议之间的表格差异。

特征

CoAP协议

HTTP

协议它使用UDP。它使用TCP。
网络层它使用IPv6和6LoWPAN。它使用IP层。
多播支持它支持。它不支持。
架构模型CoAP使用客户端 - 服务器和发布 - 订阅模型。HTTP使用客户端和服务器架构。
同步通信CoAP不需要这个。HTTP需要这个。
开销开销更少,而且很简单。与CoAP相比,开销更大,而且很复杂。
应用专为资源受限的网络设备而设计,例如WSN / IoT / M2M。专为没有任何资源问题的互联网设备而设计。

 

posted on 2018-08-29 17:07 sundaygeek 阅读(...) 评论(...) 编辑 收藏

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
CoAP(Constrained Application Protocol)是一种轻量级的应用层协议,用于在受限的网络环境中传输数据。以下是一个简单的CoAP协议C语言实现的示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <arpa/inet.h> #define COAP_VERSION 1 #define COAP_HEADER_LENGTH 4 typedef struct { uint8_t version; uint8_t type; uint8_t token_length; uint8_t code; uint16_t message_id; } coap_header_t; typedef struct { uint8_t *token; uint16_t token_length; uint8_t *payload; uint16_t payload_length; } coap_message_t; typedef struct { int socket_fd; struct sockaddr_in address; } coap_endpoint_t; typedef struct { uint8_t *data; uint16_t length; } coap_buffer_t; coap_header_t *coap_create_header(uint8_t type, uint8_t code, uint16_t message_id) { coap_header_t *header = calloc(1, sizeof(coap_header_t)); header->version = COAP_VERSION; header->type = type; header->token_length = 0; header->code = code; header->message_id = htons(message_id); return header; } void coap_free_header(coap_header_t *header) { free(header); } coap_message_t *coap_create_message(uint8_t type, uint8_t code, uint16_t message_id) { coap_message_t *message = calloc(1, sizeof(coap_message_t)); message->token = NULL; message->token_length = 0; message->payload = NULL; message->payload_length = 0; coap_header_t *header = coap_create_header(type, code, message_id); message->payload_length += COAP_HEADER_LENGTH; message->payload = calloc(1, message->payload_length); memcpy(message->payload, header, COAP_HEADER_LENGTH); coap_free_header(header); return message; } void coap_free_message(coap_message_t *message) { free(message->token); free(message->payload); free(message); } coap_endpoint_t *coap_create_endpoint(char *ip, int port) { coap_endpoint_t *endpoint = calloc(1, sizeof(coap_endpoint_t)); endpoint->socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); memset(&endpoint->address, 0, sizeof(endpoint->address)); endpoint->address.sin_family = AF_INET; endpoint->address.sin_addr.s_addr = inet_addr(ip); endpoint->address.sin_port = htons(port); return endpoint; } void coap_free_endpoint(coap_endpoint_t *endpoint) { close(endpoint->socket_fd); free(endpoint); } coap_buffer_t *coap_receive(coap_endpoint_t *endpoint) { coap_buffer_t *buffer = calloc(1, sizeof(coap_buffer_t)); socklen_t address_length = sizeof(endpoint->address); int length = recvfrom(endpoint->socket_fd, buffer->data, buffer->length, 0, (struct sockaddr *) &endpoint->address, &address_length); buffer->length = length; return buffer; } void coap_send(coap_endpoint_t *endpoint, coap_message_t *message) { sendto(endpoint->socket_fd, message->payload, message->payload_length, 0, (struct sockaddr *) &endpoint->address, sizeof(endpoint->address)); } int main() { coap_endpoint_t *endpoint = coap_create_endpoint("127.0.0.1", 5683); coap_message_t *message = coap_create_message(0, 1, 1); coap_send(endpoint, message); coap_buffer_t *buffer = coap_receive(endpoint); printf("Received %d bytes: %.*s\n", buffer->length, buffer->length, buffer->data); coap_free_message(message); coap_free_endpoint(endpoint); free(buffer); return 0; } ``` 这个示例实现了CoAP协议的基本功能,包括创建和发送CoAP消息,以及接收和处理CoAP消息。在这个示例中,我们使用了标准的C语言库函数和一些网络相关的函数来实现CoAP协议的功能。如果您想要了解更多关于CoAP协议的信息,请参考CoAP协议的官方网站。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sundaygeek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值