Raw TCP/IP interface for lwIP
Authors: Adam Dunkels, Leon Woestenberg, Christiaan Simons
lwIP provides two Application Program's Interfaces (APIs) for programs
to use for communication with the TCP/IP code:
* low-level "core" / "callback" or "raw" API.
* higher-level "sequential" API.
The sequential API provides a way for ordinary, sequential, programs
to use the lwIP stack. It is quite similar to the BSD socket API. The
model of execution is based on the blocking open-read-write-close
paradigm. Since the TCP/IP stack is event based by nature, the TCP/IP
code and the application program must reside in different execution
contexts (threads).
** The remainder of this document discusses the "raw" API. **
The raw TCP/IP interface allows the application program to integrate
better with the TCP/IP code. Program execution is event based by
having callback functions being called from within the TCP/IP
code. The TCP/IP code and the application program both run in the same
thread. The sequential API has a much higher overhead and is not very
well suited for small systems since it forces a multithreaded paradigm
on the application.
The raw TCP/IP interface is not only faster in terms of code execution
time but is also less memory intensive. The drawback is that program
development is somewhat harder and application programs written for
the raw TCP/IP interface are more difficult to understand. Still, this
is the preferred way of writing applications that should be small in
code size and memory usage.
Both APIs can be used simultaneously by different application
programs. In fact, the sequential API is implemented as an application
program using the raw TCP/IP interface.
--- Callbacks
Program execution is driven by callbacks. Each callback is an ordinary
C function that is called from within the TCP/IP code. Every callback
function is passed the current TCP or UDP connection state as an
argument. Also, in order to be able to keep program specific state,
the callback functions are called with a program specified argument
that is independent of the TCP/IP state.
The function for setting the application connection state is:
- void tcp_arg(struct tcp_pcb *pcb, void *arg)
Specifies the program specific state that should be passed to all
other callback functions. The "pcb" argument is the current TCP
connection control block, and the "arg" argument is the argument
that will be passed to the callbacks.
--- TCP connection setup
The functions used for setting up connections is similar to that of
the sequential API and of the BSD socket API. A new TCP connection
identifier (i.e., a protocol control block - PCB) is created with the
tcp_new() function. This PCB can then be either set to listen for new
incoming connections or be explicitly connected to another host.
- struct tcp_pcb *tcp_new(void)
Creates a new connection identifier (PCB). If memory is not
available for creating the new pcb, NULL is returned.
- err_t tcp_bind(struct tcp_pcb *pcb, struct ip_addr *ipaddr,
u16_t port)
Binds the pcb to a local IP address and port number. The IP address
can be specified as IP_ADDR_ANY in order to bind the connection to
all local IP addresses.
If another connection is bound to the same port, the function will
return ERR_USE, otherwise ERR_OK is returned.
- struct tcp_pcb *tcp_listen(struct tcp_pcb *pcb)
Commands a pcb to start listening for incoming connections. When an
incoming connection is accepted, the function specified with the
tcp_accept() function will be called. The pcb will have to be bound
to a local port with the tcp_bind() function.
The tcp_listen() function returns a new connection identifier, and
the one passed as an argument to the function will be
deallocated. The reason for this behavior is that less memory is
needed for a connection that is listening, so tcp_listen() will
reclaim the memory needed for the original connection and allocate a
new smaller memory block for the listening connection.
tcp_listen() may return NULL if no memory was available for the
listening connection. If so, the memory associated with the pcb
passed as an argument to tcp_listen() will not be deallocated.
- struct tcp_pcb *tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
Same as tcp_listen, but limits the number of outstanding connections
in the listen queue to the value specified by the backlog argument.
To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
- void tcp_accepted(struct tcp_pcb *pcb)
Inform lwIP that an incoming connection has been accepted. This would
usually be called from the accept callback. This allows lwIP to perform
housekeeping tasks, such as allowing further incoming connections to be
queued in the listen backlog.
- void tcp_accept(struct tcp_pcb *pcb,
err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
err_t err))
Specified the callback function that should be called when a new
connection arrives on a listening connection.
- 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));
Sets up the pcb to connect to the remote host and sends the
initial SYN segment which opens the connection.
The tcp_connect() function returns immediately; it does not wait for
the connection to be properly setup. Instead, it will call the
function specified as the fourth argument (the "connected" argument)
when the connection is established. If the connection could not be
properly established, either because the other host refused the
connection or because the other host didn't answer, the "connected"
function will be called with an the "err" argument set accordingly.
The tcp_connect() function can return ERR_MEM if no memory is
available for enqueueing the SYN segment. If the SYN indeed was
enqueued successfully, the tcp_connect() function returns ERR_OK.
--- Sending TCP data
TCP data is sent by enqueueing the data with a call to
tcp_write(). When the data is successfully transmitted to the remote
host, the application will be notified with a call to a specified
callback function.
- err_t tcp_write(struct tcp_pcb *pcb, void *dataptr, u16_t len,
u8_t copy)
Enqueues the data pointed to by the argument dataptr. The length of
the data is passed as the len parameter. The copy argument is either
0 or 1 and indicates whether the new memory should be allocated for
the data to be copied into. If the argument is 0, no new memory
should be allocated and the data should only be referenced by
pointer.
The tcp_write() function will fail and return ERR_MEM if the length
of the data exceeds the current send buffer size or if the length of
the queue of outgoing segment is larger than the upper limit defined
in lwipopts.h. The number of bytes available in the output queue can
be retrieved with the tcp_sndbuf() function.
The proper way to use this function is to call the function with at
most tcp_sndbuf() bytes of data. If the function returns ERR_MEM,
the application should wait until some of the currently enqueued
data has been successfully received by the other host and try again.
- void tcp_sent(struct tcp_pcb *pcb,
err_t (* sent)(void *arg, struct tcp_pcb *tpcb,
u16_t len))
Specifies the callback function that should be called when data has
successfully been received (i.e., acknowledged) by the remote
host. The len argument passed to the callback function gives the
amount bytes that was acknowledged by the last acknowledgment.
--- Receiving TCP data
TCP data reception is callback based - an application specified
callback function is called when new data arrives. When
lwip-1.3.0.tar.gz

LWIP,全称Lightweight TCP/IP stack,是一款开源、轻量级的TCP/IP协议栈。这个"lwip-1.3.0.tar.gz"文件是LWIP的1.3.0版本,是一个经过更新和优化的版本,旨在修复之前版本中发现的多个bug,以提高软件的稳定性和性能。
LWIP设计的目标是为嵌入式系统提供一个小型但功能完整的网络协议栈。它实现了TCP、UDP、ICMP、IPv4、ARP以及DNS等网络协议,支持多线程和事件驱动两种工作模式,适用于资源有限的微控制器环境。
在LWIP 1.3.0版本中,开发者可能针对以下几个方面进行了优化:
1. **错误修复**:这个版本修复了各种可能导致程序崩溃或行为异常的bug,这些bug可能涉及到协议处理、内存管理、并发处理等方面,确保了网络通信的可靠性。
2. **性能提升**:可能对内存分配、数据包处理速度等关键性能指标进行了优化,以减少处理器负担,降低功耗。
3. **稳定性增强**:通过对协议栈的深度调试和测试,增强了系统在复杂网络环境下的稳定性,降低了网络连接中断的可能性。
4. **API改进**:可能对用户接口API进行了调整,使得开发者更容易集成和使用LWIP,同时也可能增加了新的API来支持更多的网络功能。
5. **兼容性**:LWIP 1.3.0可能增强了与不同硬件平台和操作系统的兼容性,使得更多类型的设备可以利用LWIP实现网络通信。
在解压"lwip-1.3.0.tar.gz"后,你将得到"lwip"目录,其中包含以下内容:
- **源代码**:LWIP的核心代码,包括协议处理模块、内存管理模块、事件处理模块等。
- **文档**:可能包括开发者指南、API参考手册、变更日志等,帮助你理解和使用LWIP 1.3.0。
- **示例**:示例应用程序展示了如何在实际项目中集成和使用LWIP,这对于初学者非常有帮助。
- **配置脚本**:用于定制LWIP以适应你的特定硬件平台和需求的配置文件。
- **编译脚本**:构建和编译LWIP所需的Makefile或其他构建工具。
在使用LWIP时,你需要根据你的开发环境进行适当的配置,如选择合适的内存模型、设置网络接口参数等。然后,你可以通过编译脚本将LWIP编译为适合你的嵌入式系统的库,并将其集成到你的项目中,实现网络功能。
LWIP 1.3.0是一个经过精心优化和修复的网络协议栈版本,对于需要在网络通信上进行嵌入式开发的工程师来说,是一个可靠的选择。通过理解和应用这个版本,你可以实现高效、稳定的网络连接,同时享受到开源社区持续的维护和支持。

ssh_33
- 粉丝: 0
最新资源
- 永磁同步电机(PMSM)直接转矩控制(DTC)技术的研究与实现 v1.5
- 基于改进粒子群算法的六自由度机械臂轨迹规划Matlab源码:加入动态学习因子、动态惯性权重、混沌映射与黄金正弦的优化算法
- 基于GPU并行计算的LBM三维两相流模拟:实时相饱和度曲线与精准控制参数
- LabVIEW调用VisionPro DLL实现多工位多相机二维码读取及MES上传与Modbus TCP通讯
- 基于SUMO的智能小车实时动态道路信息获取与备选路径推荐系统
- 电力系统三段式电流保护机制的Simulink建模与仿真分析 · 仿真分析
- Proteus仿真中可控增益放大电路
- 三菱PLC控制的中达一体机程序:实现单轴往复运动,双段位置循环,手动前进后退及回原点功能,便捷设置循环次数
- Cruise双电机四驱与单电机前驱分层建模研究:控制策略模型与多模式联合仿真实现及SP资料详解
- Algotirhm.cs
- MMC储能技术详解:模块化多电平变换器与电池储能系统的SOC均衡控制策略
- 相场锂枝晶:进阶案例教学——融合相场、浓度与电势的耦合分析在Comsol中的应用
- 基于TC397的Autosar BSW与MCAL工程配置:支持Xcp功能与Can通信的多核嵌入式系统开发 - CAN通信 v1.0
- 混合A星路径规划详解:基于MATLAB的逐行源码解析与实践应用
- 基于无差拍控制的有源电力滤波器仿真研究及谐波补偿文献探讨 参考文献:可依据研究内容选取相关电力电子、电力系统和控制理论等领域的文献进行参考。
- 马尔科夫时间序列预测方法:简单、易用且效果显著的具体流程步骤及相关文档 (2025年)