当前位置: 首页 > news >正文

RAW 编程接口 TCP 简介

一、LWIP 中 中 RAW API  编程接口中与 TCP  相关的函数

二、LWIP TCP RAW API 函数

三、LwIP_Periodic_Handle函数

LwIP_Periodic_Handle 函数是一个必须被无限循环调用的 LwIP支持函数,一般在 main函数的无限循环中调用,主要功能是为 LwIP各个模块提供时间并查询链路状态,该 函数有一个形参,用于指示当前时间,单位为 ms。 对于 TCP功能,每 250ms执行一次 tcp_tmr函数;对于 ARP,每 5s 执 行一次 etharp_tmr函数;对于链路状态检测,每 1s 执行一次ETH_CheckLinkStatus 函数; 对于 DHCP功能,每 500ms执行一次 dhcp_fine_tmr函数,如果 DHCP处于DHCP_START 或 DHCP_WAIT_ADDRESS 状态就执行LwIP_DHCP_Process_Handle 函数,对于 DHCP功 能,还有每 60s 执行一次 dhcp_coarse_tmr函数。

四、TCP客户端连接代码

tcpclinet.c

#include "lwip/netif.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "lwip/udp.h"
#include "lwip/pbuf.h"
#include <stdio.h>	
#include <string.h>
#include "main.h"static void client_err(void *arg, err_t err)       //出现错误时调用这个函数,打印错误信息,并尝试重新连接
{printf("连接错误!!\n");printf("尝试重连!!\n");printf("重新初始化客户端\n");TCP_Client_Init();
}static err_t client_send(void *arg, struct tcp_pcb *tpcb)   //发送函数,调用了tcp_write函数
{uint8_t send_buf[]= "我是客户端,是你的好哥哥\n";//发送数据到服务器tcp_write(tpcb, send_buf, sizeof(send_buf), 1); return ERR_OK;
}static err_t client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{if (p != NULL) {        /* 接收数据*/tcp_recved(tpcb, p->tot_len);/* 返回接收到的数据*/  tcp_write(tpcb, p->payload, p->tot_len, 1);memset(p->payload, 0 , p->tot_len);pbuf_free(p);} else if (err == ERR_OK) {//服务器断开连接printf("服务器断开连接!\n");tcp_close(tpcb);//重新连接TCP_Client_Init();}return ERR_OK;
}static err_t client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{printf("connected ok!\n");//注册一个周期性回调函数tcp_poll(pcb,client_send,2);//注册一个接收函数tcp_recv(pcb,client_recv);return ERR_OK;
}void TCP_Client_Init(void)
{        struct tcp_pcb *client_pcb = NULL;   //这一句一定要放在里面,否则会没用ip4_addr_t server_ip;     //因为客户端要主动去连接服务器,所以要知道服务器的IP地址/* 创建一个TCP控制块  */client_pcb = tcp_new();	  IP4_ADDR(&server_ip, DEST_IP_ADDR0,DEST_IP_ADDR1,DEST_IP_ADDR2,DEST_IP_ADDR3);//合并IP地址printf("客户端开始连接!\n");//开始连接tcp_connect(client_pcb, &server_ip, TCP_CLIENT_PORT, client_connected);ip_set_option(client_pcb, SOF_KEEPALIVE);	printf("已经调用了tcp_connect函数\n");//注册异常处理tcp_err(client_pcb, client_err);printf("已经注册异常处理函数\n");	
}

tcpclinet.h 

#ifndef _TCPCLIENT_H_
#define _TCPCLIENT_H_#define TCP_CLIENT_PORT 5001void TCP_Client_Init(void);#endif

五、TCP服务器连接代码

tcpserver.c

#include "tcpserver.h"
#include "lwip/netif.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "lwip/udp.h"
#include "lwip/pbuf.h"
#include <stdio.h>	
#include <string.h>static err_t tcpecho_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{                                   //对应接收数据连接的控制块   接收到的数据   if (p != NULL) {        //int a = 666;/* 更新窗口*/tcp_recved(tpcb, p->tot_len);     //读取数据的控制块   得到所有数据的长度   /* 返回接收到的数据*/  //tcp_write(tpcb, p->payload, p->tot_len, 1);uint8_t send_buf1[]= "我收到了你的信息!是";uint8_t send_buf2[]= "吗?\n";	tcp_write(tpcb, send_buf1, sizeof(send_buf1), 1);tcp_write(tpcb, p->payload, p->tot_len, 1);	tcp_write(tpcb, send_buf2, sizeof(send_buf2), 1);	memset(p->payload, 0 , p->tot_len);pbuf_free(p);} else if (err == ERR_OK)    //检测到对方主动关闭连接时,也会调用recv函数,此时p为空{return tcp_close(tpcb);}return ERR_OK;
}static err_t tcpecho_accept(void *arg, struct tcp_pcb *newpcb, err_t err) //由于这个函数是*tcp_accept_fn类型的//形参的数量和类型必须一致
{     tcp_recv(newpcb, tcpecho_recv);    //当收到数据时,回调用户自己写的tcpecho_recvreturn ERR_OK;
}void TCP_Echo_Init(void)
{struct tcp_pcb *server_pcb = NULL;	            		/* 创建一个TCP控制块  */server_pcb = tcp_new();	printf("创建了一个控制块\n");/* 绑定TCP控制块 */tcp_bind(server_pcb, IP_ADDR_ANY, TCP_ECHO_PORT);       printf("已经绑定一个控制块\n");/* 进入监听状态 */server_pcb = tcp_listen(server_pcb);printf("进入监听状态\n");	/* 处理连接 注册函数,侦听到连接时被注册的函数被回调 */	tcp_accept(server_pcb, tcpecho_accept);  //侦听到连接后,回调用户编写的tcpecho_accept //这个函数是*tcp_accept_fn类型的
}

tcpserver.h

#ifndef _TCPECHO_H_
#define _TCPECHO_H_#define TCP_ECHO_PORT 5001void TCP_Echo_Init(void);#endif
http://www.lryc.cn/news/305157.html

相关文章:

  • Oracle EBS FA折旧回滚的分录追溯
  • sql注入 [极客大挑战 2019]FinalSQL1
  • 持续集成,持续交付和持续部署的概念,以及GitLab CI / CD的介绍
  • [Java 项目亮点] 三层限流设计
  • GPT-SoVITS 快速声音克隆使用案例:webui、api接口
  • 高速自动驾驶智慧匝道(HIC)系统功能规范
  • SQL Server——建表时为字段添加注释
  • 【明道云】导入Excel数据时的默认顺序
  • 几种后端开发中常用的语言。
  • Sora——探索AI视频模型的无限可能
  • [NCTF2019]True XML cookbook --不会编程的崽
  • Qt 应用程序中指定使用桌面版本的 OpenGL或嵌入式系统OpenGL ES的 API 进行渲染
  • 大数据软件,待补充
  • 深入探索pdfplumber:从PDF中提取信息到实际项目应用【第94篇—pdfplumbe】
  • 实现linux platform tree框架下ICM20608驱动开发(SPI)
  • 在前端开发中需要考虑的常见web安全问题和攻击原理以及防范措施
  • 年关将至送大礼 社区适时献爱心
  • singularity容器的技术基础
  • jax可微分编程的笔记(2)
  • 在Linux服务器上部署一个单机项目
  • HTTP概要
  • 128 Linux 系统编程6 ,C++程序在linux 上的调试,GDB调试
  • vue2的ElementUI的form表单报错“Error: [ElementForm]unpected width”修复
  • Linux 网络命令指南
  • vue3组件间的通信,通过props,emit,provide和inject把数据传递N个层级,expose和ref实现父组件调用子组件方法
  • 开源免费的NTFS for mac工具mounty
  • Sora-OpenAI 的 Text-to-Video 模型:制作逼真的 60s 视频片段
  • 4 buuctf解题
  • Jmeter基础(3) 发起一次请求
  • 视频怎么变成gif动图?一招教你在线转换