当前位置:网站首页>LwIP develops | socket | TCP | keepalive heartbeat mechanism
LwIP develops | socket | TCP | keepalive heartbeat mechanism
2022-07-28 16:31:00 【Chop Hua】
** Reference resources :**https://blog.csdn.net/Chuangke_Andy/article/details/113116289
** significance :** Improve each platform lwip TCP/UDP Programming compatibility , Portability
Environmental Science :
1.freertos
2. Turn on LWIP_SOCKET & LWIP_TCP_KEEPALIVE
3. Bgi, HC32F4A0( Compatible )
Programming
#include "lwip_keepalive.h"
#include "lwip/opt.h"
#include <lwip/sockets.h>
#include "lwip/sys.h"
#include "lwip/api.h"
#include "string.h"
//https://blog.csdn.net/Chuangke_Andy/article/details/121420376
#if (LWIP_SOCKET & LWIP_TCP_KEEPALIVE)
#define DEST_PORT (6134UL)// Destination address port number
#define DEST_IP "192.168.2.195"/* Destination address IP, This is set as local */
#define MAX_DATA (1024UL)// The maximum amount of data received
#define LWIP_KEEPALIVE_DEBUG_ENABLE 1
#if LWIP_KEEPALIVE_DEBUG_ENABLE
#define LWIP_KEEPALIVE_DEBUG printf
#else
#define LWIP_KEEPALIVE_DEBUG(...)
#endif
#define BUFF_SIZE (1024UL)
static char ReadBuff[BUFF_SIZE];
void lwip_keepalive_thread(void *arg)
{
int sockfd, n, i, ret;
struct sockaddr_in dest_addr;
int so_keepalive_val = 1; // Enable the heartbeat mechanism
int tcp_keepalive_idle = 3; // Send heartbeat idle cycle S
int tcp_keepalive_intvl = 3;// Send heartbeat interval S
int tcp_keepalive_cnt = 3; // Number of retransmissions
int tcp_nodelay = 1; // Send to the merge package without delay
again:
// establish scoket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// Enable the heartbeat mechanism
setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &so_keepalive_val, sizeof(int));
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(DEST_PORT);
dest_addr.sin_addr.s_addr = inet_addr(DEST_IP);
inet_aton(DEST_IP,&dest_addr.sin_addr);/* Convert the dotted decimal system to 32 Bit integer type */
if(connect(sockfd,(struct sockaddr*)&dest_addr,sizeof(struct sockaddr)) < 0)
{
// Connection method , Incoming handle , Destination address and size
//100ms Connect to the server once
LWIP_KEEPALIVE_DEBUG("connect error\r\n");
close(sockfd);
vTaskDelay(100);
goto again;
}
// Configure heartbeat detection parameters The default parameter takes a long time
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &tcp_keepalive_idle, sizeof(int));
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &tcp_keepalive_intvl, sizeof(int));
setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, &tcp_keepalive_cnt, sizeof(int));
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &tcp_nodelay, sizeof(int));
LWIP_KEEPALIVE_DEBUG("server is connect ok\r\n");
for(;;)
{
// Wait for the server to send data
n = read(sockfd, ReadBuff, BUFF_SIZE);
if(n <= 0)
{
LWIP_KEEPALIVE_DEBUG("read socket error\r\n");
close(sockfd);
goto again;
}
// Write back to server
n = write(sockfd, ReadBuff, n);
if(n <= 0)
{
close(sockfd);
goto again;
}
}
}
#endif
void lwip_keepalive_init(void)
{
#if LWIP_SOCKET
sys_thread_new("lwip_keepalive_thread", lwip_keepalive_thread, NULL, 512, 4);
#endif
}
边栏推荐
- “蔚来杯“2022牛客暑期多校训练营3 A.Ancestor LCA+暴力计数
- ANSA二次开发 - 抽中面的两种方法
- Sdl2 concise tutorial (4): using SDL_ Image library importing pictures
- A program for judging the result of cyclic input
- “蔚来杯“2022牛客暑期多校训练营3 H.Hacker SAM+线段树/DP/分治(不带修查区间最大子段和)
- Darknet training yolov4 record
- mysql 查看事件状态语句和修改办法
- laravel
- el-input限制只能输入规定的数
- 自动打包压缩备份下载及删除 bat脚本命令
猜你喜欢

疫情红利消失,「居家健身」泡沫消散

laravel

Kubeedge releases white paper on cloud native edge computing threat model and security protection technology

Telecommuting can be easily realized in only three steps

HyperMesh运行脚本文件的几种方法

视频号找到金钥匙,抖音模仿后来人

I came across Digital Phoenix coordinate Xuhui Meiluo city in Shanghai

ANSA二次开发 - 抽中面的两种方法

优化Hypermesh脚本性能的几点建议

IT远程运维是什么意思?远程运维软件哪个好?
随机推荐
Solve the width overflow of rich text pictures such as uniapp
Roson的Qt之旅#102 ListModel
Abaqus GUI界面解决中文乱码问题(插件中文乱码也适用)
Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
Thoughts on solving the pop-up of malicious computer advertisements
Notes on October 22, 2021
A program for judging the result of cyclic input
PHP 图片上传
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
Zhengda cup hacker marathon data analysis competition
ffmpeg获取首帧
IT远程运维是什么意思?远程运维软件哪个好?
Reentrant and non reentrant
我在上海偶遇数字凤凰#坐标徐汇美罗城
头条文章_signature
Implementation of skip table
排序2-冒泡排序与快速排序(递归加非递归讲解)
Is MySQL query limit 1000,10 as fast as limit 10? If I want to page, what should I do?
Detailed explanation of QT qstring
Roson的Qt之旅#101 Qt Quick中的模型和视图