LWIP raw API

Source: Internet
Author: User
Introduction

LWIP provides two APIs:

1. Underlying callback-based raw API

2. High-level ordered model APIs (similar to BSD socket)

The ordered model API provides APIs that use protocol stacks for Common Ordered programs. Similar to BSD APIs, it is also based on the blocked open-read-write-close mode. The TCP/IP protocol stack is time-based. Therefore, TCP/IP code and application code must be in different threads.

The raw API can make the application and TCP/IP protocol stack code more closely integrated. The program execution is also based on events and uses the callback function mechanism. In this way, the TCP/IP code and application code run in the same thread.

In fact, the ordered model API is also implemented based on raw API.

Callback

The execution of the program is based on the callback function. The callback function obtains the status of the current connection from the input parameters. The function that sets the connection status parameter for the callback function is as follows:

Void tcp_arg (struct tcp_pcb * PCB, void * Arg );

PCB is the control block of the current TCP connection, and Arg is the parameter that will be passed to the callback function.

Establish a TCP connection

The method for establishing a TCP connection is very similar to that of a BSD socket. You can use the tcp_new function to create a new TCP identifier (the structure of the PCB in the raw API is Protocol control block ). A PCB can be set to listen to a connection or explicitly connect to a host.

Struct tcp_pcb * tcp_new (void );

Create a new PCB. If null is returned, the creation fails.

 

Err_t tcp_bind (struct tcp_pcb * PCB, struct ip_addr * ipaddr, u16_t port );

Bind the PCB to a local IP address + port. You can set the IP address to ip_addr_any to bind it to all local IP addresses. If other connections have been bound to this port, err_use is returned; otherwise, err_ OK is returned.

 Struct tcp_pcb * tcp_listen (struct tcp_pcb * PCB );

Let the PCB enter the listening status. When a new connection is established, the callback function specified by tcp_accept is called. This function returns a new PCB, and the PCB passed as a parameter will be released. If a failure occurs, null is returned. The PCB of the parameter will not be released.

 

Struct tcp_pcb * tcp_listen_with_backlog (struct tcp_pcb * PCB, u8_t backlog );

Similar to tcp_listen, But it limits the number of connections in the queue waiting for the connection (cannot exceed the backlog). You need to set tcp_listen_backlog to 1 in lwipopts. h.

Void tcp_accepted (struct tcp_pcb * PCB );

It tells LWIP that a new connection has been accepted. Generally, it is called in the callback function to notify LWIP to allow more connection requests.

 

Void tcp_accept (struct tcp_pcb * PCB, err_t (* accept) (void * Arg, struct tcp_pcb * newpcb, err_t ERR ));

Specifies the callback function executed when a new connection is established.

 

Err_t tcp_connect (struct tcp_pcb * PCB, struct ip_addr * ipaddr, u16_t port, err_t (* connected) (void * Arg,

Struct tcp_pcb * tpcb, err_t ERR ));

Connect the PCB to a remote host and send a SYN segment to open the connection. The tcp_connect () function is returned immediately. It does not wait for the connection to be established. When the connection is actually established, it calls the fourth parameter connectted function, if the connection fails, the err parameter of the connect function is set to the corresponding error value. If there is not enough space to put SYN requests into the queue, this function will return err_mem; otherwise, err_ OK will be returned if the request is successful.

 

Send data

TCP data is sent through the tcp_write function:

 

 Err_t tcp_write (struct tcp_pcb * PCB, void * dataptr, u16_t Len, u8_t copy );

 

 

Send the data pointed to by dataptr to the sending queue. Len indicates the length of the sent data. The copy parameter indicates whether data copying is allowed. This function returns err_mem when the size of the sent data exceeds the maximum length of the current sending buffer or sending queue. The number of bytes available in the sending queue can be obtained using the tcp_sndbuf () function. This function is suitable for sending the number of bytes not greater than the length of tcp_sndbuf (). If err_mem is returned, the program continues to call the function after the sending queue has enough data space.

 

 Void tcp_sent (struct tcp_pcb * PCB, err_t (* sent) (void * Arg, struct tcp_pcb * tpcb, u16_t Len ));

Specifies the callback function to be executed after the data is successfully sent (the Response Message of the remote host is received. The LEN parameter indicates the number of bytes determined in the last ack.

 

 

Receive data

TCP data is received based on callback. The Program specifies the callback function to be executed when new data arrives.

 

 

 Void tcp_recv (struct tcp_pcb * PCB, err_t (* Recv) (void * Arg, struct tcp_pcb * tpcb, struct pbuf * P, err_t ERR ));

Set the callback function to be called when receiving data. If pbuf = NULL, the remote host is closed. If no error occurs and the function returns err_ OK, you must first release pbuf. Otherwise, do not release pbuf so that LWIP can be saved.

Void tcp_recved (struct tcp_pcb * PCB, u16_t Len );

The Len is the length of the received data.

 

Close connection

Err_t tcp_close (struct tcp_pcb * PCB );


Void tcp_abort (struct tcp_pcb * PCB );

The difference between the two functions is that the second function will never go wrong ~

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.