梅科尔工作室-钟伊帆-小熊派学习笔记(网络应用开发)

网友投稿 678 2022-08-30

梅科尔工作室-钟伊帆-小熊派学习笔记(网络应用开发)

梅科尔工作室-钟伊帆-小熊派学习笔记(网络应用开发)

一、UDP客户端

​UDP协议相关API介绍

UDP客户端创建流程介绍

实现UDP客户端

static void UDPClientTask(void){//服务器的地址信息struct sockaddr_in send_addr;socklen_t addr_length = sizeof(send_addr);char recvBuf[512];//连接WifiWifiConnect("Hold", "0987654321");//创建socketif ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){perror("create socket failed!\r\n");exit(1);}//初始化预连接的服务端地址send_addr.sin_family = AF_INET;send_addr.sin_port = htons(_PROT_);send_addr.sin_addr.s_addr = inet_addr("192.168.0.175");addr_length = sizeof(send_addr);while (1){bzero(recvBuf, sizeof(recvBuf));//发送数据到服务远端sendto(sock_fd, send_data, strlen(send_data), 0, (struct sockaddr *)&send_addr, addr_length);//线程休眠一段时间sleep(10);//接收服务端返回的字符串recvfrom(sock_fd, recvBuf, sizeof(recvBuf), 0, (struct sockaddr *)&send_addr, &addr_length);printf("%s:%d=>%s\n", inet_ntoa(send_addr.sin_addr), ntohs(send_addr.sin_port), recvBuf);}//关闭这个 socketclose(sock_fd);}

测试UDP客户端

​使用 Sockettool 创建UDP服务端用于测试

二、TCP服务端

​socket.h接口简介:

TCP服务端创建流程介绍

实现TCP服务端

//创建socketif ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("socket is error\r\n");exit(1);}bzero(&server_sock, sizeof(server_sock));server_sock.sin_family = AF_INET;server_sock.sin_addr.s_addr = htonl(INADDR_ANY);server_sock.sin_port = htons(_PROT_);//调用bind函数绑定socket和地址if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(struct sockaddr)) == -1){perror("bind is error\r\n");exit(1);}//调用listen函数监听(指定port监听)if (listen(sock_fd, TCP_BACKLOG) == -1){perror("listen is error\r\n");exit(1);}printf("start accept\n");

测试TCP服务端

​使用 Sockettool 创建TCP客户端用于测试

三、TCP客户端

​TCP协议相关API介绍

TCP客户端创建流程介绍

实现TCP客户端

static void TCPClientTask(void){//服务器的地址信息struct sockaddr_in send_addr;socklen_t addr_length = sizeof(send_addr);char recvBuf[512];//连接WifiWifiConnect("Hold", "0987654321");//创建socketif ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1){perror("create socket failed!\r\n");exit(1);}//初始化预连接的服务端地址send_addr.sin_family = AF_INET;send_addr.sin_port = htons(_PROT_);send_addr.sin_addr.s_addr = inet_addr("192.168.0.175");addr_length = sizeof(send_addr);connect(sock_fd, (struct sockaddr *)&send_addr, addr_length);while (1){bzero(recvBuf, sizeof(recvBuf));// send(sock_fd,);if ((ret = send(sock_fd, send_data, strlen(send_data), 0)) == -1){perror("send : ");}if ((ret = recv(sock_fd, recvBuf, sizeof(recvBuf), 0)) == -1){printf("recv error \r\n");}printf("recv :%s\r\n", recvBuf);}//关闭这个 socketclose(sock_fd);}

四、UDP服务端

​UDP协议相关API介绍

UDP服务端创建流程介绍

​实现UDP服务端

static void UDPServerTask(void){//服务端地址信息struct sockaddr_in server_sock;//客户端地址信息struct sockaddr_in client_sock;socklen_t sin_size;//连接WifiWifiConnect("Hold", "0987654321");//创建socketif ((sock_fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){perror("socket is error\r\n");exit(1);}bzero(&server_sock, sizeof(server_sock));server_sock.sin_family = AF_INET;server_sock.sin_addr.s_addr = htonl(INADDR_ANY);server_sock.sin_port = htons(_PROT_);//调用bind函数绑定socket和地址if (bind(sock_fd, (struct sockaddr *)&server_sock, sizeof(server_sock)) == -1){perror("bind is error\r\n");exit(1);}//处理目标ssize_t ret;while (1){sin_size = sizeof(struct sockaddr_in);bzero(recvbuf, sizeof(recvbuf));if ((ret = recvfrom(sock_fd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&client_sock, &sin_size)) == -1){printf("recv error \r\n");}printf("recv :%s\r\n", recvbuf);if ((ret = sendto(sock_fd, buf, strlen(buf), 0, (struct sockaddr *)&client_sock, sizeof(client_sock))) == -1){perror("send : ");}}}

五、MQTT客户端

MQTT介绍

MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/s ubscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布。

Paho是IBM在2011年建立的Eclipse开源项目,该项目包含以C、Java、Python、Javascript等语言编写的可用客户 端。

Paho MQTT 文件目录介绍

​MQTTClient:封装MQTTPacket生成的高级别C++客户端程序。

MQTTClient-C:封装MQTTPacket生成的高级别C客户端程序 ○ samples目录提供FreeRTOS和linux两个例程,分别支持FreeRTOS和 Linux系统。 ○ src目录提供MQTTClient的代码实现能力,以及用于移植到对应平台的 网络驱动 MQTTPacket:提供MQTT数据包的序列化与反序列化,以及部分 辅助函数。

如何使用Paho MQTT

实现MQTT客户端

NetworkConnect(&network, "192.168.0.175", 1883);printf("MQTTClientInit ...\n");MQTTClientInit(&client, &network, 2000, sendBuf, sizeof(sendBuf), readBuf, sizeof(readBuf));MQTTString clientId = MQTTString_initializer;clientId.cstring = "bearpi";MQTTPacket_connectData data = MQTTPacket_connectData_initializer;data.clientID = clientId;data.willFlag = 0;data.MQTTVersion = 3;data.keepAliveInterval = 0;data.cleansession = 1;printf("MQTTConnect ...\n");rc = MQTTConnect(&client, &data);if (rc != 0) {printf("MQTTConnect: %d\n", rc);NetworkDisconnect(&network);MQTTDisconnect(&client);osDelay(200);goto begin;}printf("MQTTSubscribe ...\n");rc = MQTTSubscribe(&client, "test", 2, messageArrived);if (rc != 0) {printf("MQTTSubscribe: %d\n", rc);osDelay(200);goto begin;}while (++count){MQTTMessage message;char payload[30];message.qos = 2;message.retained = 0;message.payload = payload;sprintf(payload, "message number %d", count);message.payloadlen = strlen(payload);if ((rc = MQTTPublish(&client, "pubtopic", &message)) != 0){printf("Return code from MQTT publish is %d\n", rc);NetworkDisconnect(&network);MQTTDisconnect(&client);goto begin;}osDelay(50);}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:梅科尔工作室-钟伊帆-小熊派学习笔记(设备联网上云)
下一篇:map 并发崩溃一例:顺便吐槽 Go 打印堆栈不友好(map医学上)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~