RAW_TCP

本文详细介绍了LWIP中RAW_TCP的使用,包括TCP控制块的创建、连接建立、数据发送与接收的过程,以及相关API的使用,如tcp_new、tcp_connect、tcp_write等。此外,还提到了Cubemx的配置和PHY设置,以及 lwip 配置和回调函数的注册。实验部分展示了如何实现TCP连接和数据传输。
摘要由CSDN通过智能技术生成

发送接收过程

在这里插入图片描述
1、接收:通过ip_input接收数据,然后层层递交,到tcp_process时会对数据进行处理(不用用户操心)会将用于应用程序的数据层层递交到应用层;而在处理类似握手挥手这样时,会走tcp_output这条道路(这时应用程序是不需要进行处理的)
2、发送过程:应用程序调用tcp_write会将数据放到发送队列中(enqueue),tcp_output会将发送的数据发送出去

TCP控制块

LWIP中将TCP控制块组合成链表的形式:
在这里插入图片描述

注册回调函数

RAW编程接口的TCP实验需要我们自行实现对应的回调 函数,然后将这些回调函数注册给指定的TCP控制块,这些注册函数如下:

函数 描述
tcp_arg(struct tcp_pcb *pcb, void *arg) 注册回调函数使用的参数 这样下面这几个回调函数就是用的这个参数了,所以这个参数要给他malloc个内存,不然会没有掉的
tcp_recv(struct tcp_pcb *pcb, tcp_recv_fn recv) 注册接收的回调函数
tcp_err(struct tcp_pcb *pcb, tcp_err_fn err) 注册出错处理的回调函数
tcp_sent(struct tcp_pcb *pcb, tcp_sent_fn sent) 注册发送成功的回调函数
tcp_poll(struct tcp_pcb *pcb, tcp_poll_fn poll, u8_t interval) 注册轮询函数,这个函数会被周期性调用,因此这这个函数中我们可以将要发送的数据发送出去
tcp_accept() 当侦听到有客户端连接的话就会调用注册函数

使用:就是直接放函数名就行了

		tcp_arg(tpcb,&es);
		tcp_recv(tpcb,tcp_client_recv);  	//初始化LwIP的tcp_recv回调功能   
		tcp_err(tpcb,tcp_client_error); 	//初始化tcp_err()回调函数
		tcp_sent(tpcb,tcp_client_sent);		//初始化LwIP的tcp_sent回调功能
		tcp_poll(tpcb,tcp_client_poll,1); 	//初始化LwIP的tcp_poll回调功能  这个函数会被周期性调用,因此这这个函数中我们可以将要发送的数据发送出去

回调函数结构,在tcp.h中

/** Function prototype for tcp accept callback functions. Called when a new
 * connection can be accepted on a listening pcb.
 *
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 * @param newpcb The new connection pcb
 * @param err An error code if there has been an error accepting.
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 *            callback function!
 */
typedef err_t (*tcp_accept_fn)(void *arg, struct tcp_pcb *newpcb, err_t err);

/** Function prototype for tcp receive callback functions. Called when data has
 * been received.
 *
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 * @param tpcb The connection pcb which received data
 * @param p The received data (or NULL when the connection has been closed!)
 * @param err An error code if there has been an error receiving
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 *            callback function!
 */
typedef err_t (*tcp_recv_fn)(void *arg, struct tcp_pcb *tpcb,
                             struct pbuf *p, err_t err);

/** Function prototype for tcp sent callback functions. Called when sent data has
 * been acknowledged by the remote side. Use it to free corresponding resources.
 * This also means that the pcb has now space available to send new data.
 *
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 * @param tpcb The connection pcb for which data has been acknowledged
 * @param len The amount of bytes acknowledged
 * @return ERR_OK: try to send some data by calling tcp_output
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 *            callback function!
 */
typedef err_t (*tcp_sent_fn)(void *arg, struct tcp_pcb *tpcb,
                              u16_t len);

/** Function prototype for tcp poll callback functions. Called periodically as
 * specified by @see tcp_poll.
 *
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 * @param tpcb tcp pcb
 * @return ERR_OK: try to send some data by calling tcp_output
 *            Only return ERR_ABRT if you have called tcp_abort from within the
 *            callback function!
 */
typedef err_t (*tcp_poll_fn)(void *arg, struct tcp_pcb *tpcb);

/** Function prototype for tcp error callback functions. Called when the pcb
 * receives a RST or is unexpectedly closed for any other reason.
 *
 * @note The corresponding pcb is already freed when this callback is called!
 *
 * @param arg Additional argument to pass to the callback function (@see tcp_arg())
 * @param err Error code to indicate why the pcb has been closed
 *            ERR_ABRT: aborted through tcp_abort or by a TCP timer
 *            ERR_RST: the connection was reset by the remote host
 */
typedef void  (*tcp_err_fn)(void *arg, err_t err);

err 错误码

在err.h中

/* Definitions for error constants. */

#define ERR_OK          0    /* No error, everything OK. */
#define ERR_MEM        -1    /* Out of memory error.     */
#define ERR_BUF        -2    /* Buffer error.            */
#define ERR_TIMEOUT    -3    /* Timeout.                 */
#define ERR_RTE        -4    /* Routing problem.         */
#define ERR_INPROGRESS -5    /* Operation in progress    */
#define ERR_VAL        -6    /* Illegal value.           */
#define ERR_WOULDBLOCK -7    /* Operation would block.   */
#define ERR_USE        -8    /* Address in use.          */
#define ERR_ISCONN     -9    /* Already connected.       */

#define ERR_IS_FATAL(e) ((e) < ERR_ISCONN)

#define ERR_ABRT       -10   /* Connection aborted.      */
#define ERR_RST        -11   /* Connection reset.        */
#define E
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

成草

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

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

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

打赏作者

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

抵扣说明:

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

余额充值