admin管理员组

文章数量:1122855

RAW

发送接收过程


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 ERR_CLSD       -

本文标签: raw