当前位置:网站首页>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
}
边栏推荐
猜你喜欢

使用js直传oss阿里云存储文件,解决大文件上传服务器限制

ANSYS二次开发 - MFC界面调用ADPL文件

软件问题修复跟踪系统实战开发教程(上篇)

排序1-插入排序与希尔排序

IT远程运维是什么意思?远程运维软件哪个好?

The little red book of accelerating investment, "rush to medical treatment"?

MySQL view event status statements and modification methods

资本「断供」两年,我只能把公司卖了

Connection and application of portable borehole inclinometer data acquisition instrument and inclinometer probe

mysql 查看事件状态语句和修改办法
随机推荐
Numpy ndarray learning < I > Foundation
[Multisim Simulation] LM339 zero crossing circuit simulation
Mlx90640 infrared thermal imager temperature sensor module development notes (VIII)
Implementation of skip table
Geodetic coordinate system to Martian coordinate system
MySQL view event status statements and modification methods
动态规划 --- 数位统计DP
Notes on October 22, 2021
五舅的思考
Food safety | these two kinds of melons and fruits should be improved, especially for pregnant women with constipation
el-input限制只能输入规定的数
HM二次开发 - Data Names及其使用
排序2-冒泡排序与快速排序(递归加非递归讲解)
Numpy ndarray learning < II > miscellaneous records
I can only sell the company after the capital has been "cut off" for two years
flashfxp 530 User cannot log in. ftp
解决uniapp等富文本图片宽度溢出
LwIP development | realize TCP server through socket
Headline article_ signature
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction