Article From:

A TCP interface function:tcp_init() It must be called before calling other TCP functions, and must be configured with a hardware timer.TCP_FAST_INTERVAL (ms)Call oncetcp_fasttmr() ;eachTCP_SLOW_INTERVAL (ms)Call oncetcp_slowtmr().
Two UDP interface function:

2.1Establish:struct udp_pcb *udp_new(void),Corresponding deletionvoid udp_remove(struct udp_pcb *pcb),After the call is created, there is no need to change the network connection to communicate, delete to release the occupied resources.

2.2Bind local IP and port number:err_t udp_bind(struct udp_pcb *pcb,struct ip_addr *ipaddr,u16_t port)

2.3Connect the specified PCB to the remote host (not sending and receiving any network data).err_t udp_connect(struct udp_pcb *pcb,struct ip_addr *ipaddr,u16_t port),The corresponding disconnect connectionvoid udp_disconnect(struct udp_pcb *pcb)

2.4After the connection, the send function can be invoked to send pbuf data through the PCB (the specified IP and port number, if the function is not assigned a random port number to be sent out).err_t udp_send(struct udp_pcb *pcb, struct pbuf *p)

2.5err_t udp_sendto(struct udp_pcb *pcb,struct pbuf *p,struct ip_addr *dst_ip,u16_t dst_port)A combination of 2.3 and 2.4. If 2.3 is invoked before calling this function, 2.3 of the ports and IP will be overwritten by the function.

2.6Data reception:void udp_recv(struct udp_pcb *pcb,void (* recv)(void *arg, struct udp_pcb *upcb,struct pbuf *p,struct ip_addr *addr,u16_t port),void *recv_arg)functionrecvWhen the data is received, the callback function needs to be designed by itself.,The last one is the parameter of the callback function.

Three UDP example analysis


struct pbuf *p;
p = pbuf_alloc(PBUF_RAW,sizeof(UDPData),PBUF_RAM);
p->payload=(void *)UDPData;//Specify the send buffer
IP4_ADDR(&ipaddr,192,168,1,16);//Setting up a remote server IP
UdpPcb = udp_new();
udp_bind(UdpPcb,IP_ADDR_ANY,1025); /* Binding localIP Address and port number*/
udp_connect(UdpPcb,&ipaddr,1025); /* Connecting remote hosts and ports*/
udp_send(UdpPcb,p); //Sending data to a remote server




void http_init(void)
struct tcp_pcb *pcb;
pcb = tcp_new(); /* Establishing communicationTCP Control block(pcb) */
tcp_bind(pcb,IP_ADDR_ANY,80); /* Binding localIP Address and fixed port number 80*/
pcb = tcp_listen(pcb); /* Enter the monitoring state*/
tcp_accept(pcb,http_accept); /* Set upCallback function when there is a connection request */

static err_t http_accept(void *arg,struct tcp_pcb *pcb,err_t err)
tcp_setprio(pcb, TCP_PRIO_MIN); /* Setting the callback function priority,The existence of several connections is particularly important This function must be called*/
tcp_recv(pcb,http_recv); /* Set upTCP Segment to timeReceiving callback function */
err = ERR_OK;
return err;


void TCP_Client_Init()
struct tcp_pcb *Clipcb;
struct ip_addr ipaddr;
IP4_ADDR(&ipaddr,192,168,1,16);//Remote server IP
Clipcb = tcp_new(); /* Establishing communicationTCP Control block(Clipcb) */
tcp_bind(Clipcb,IP_ADDR_ANY,1026); /* Binding localIP Address and port number*/
tcp_connect(Clipcb,&ipaddr,1026,TcpCli_Connected);//Callback function with connection

err_t TcpCli_Connected(void *arg,struct tcp_pcb *pcb,err_t err)
tcp_write(pcb,TCP_TestData,sizeof(TCP_TestData),0); /* After the client connects to the hostSent test data*/
return ERR_OK;

Link of this Article: LWIP Application guide learning.

Leave a Reply

Your email address will not be published. Required fields are marked *