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
没有合适的资源?快使用搜索试试~ 我知道了~
tcp客户端在lm3s8962上的实现
共225个文件
h:71个
c:64个
pbi:30个
4星 · 超过85%的资源 需积分: 4 37 下载量 34 浏览量
2011-05-31
13:45:48
上传
评论 1
收藏 1.27MB RAR 举报
温馨提示
tcp客户端在lm3s8962上的实现,能够实现TCP客户端的连接,在lm3s8962上能够稳定传输数据
资源推荐
资源详情
资源评论
收起资源包目录
tcp客户端在lm3s8962上的实现 (225个子文件)
LwIP_ETH.cspy.bat 2KB
LwIP_ETH.bin 25KB
mib2.c 105KB
lcp.c 57KB
dhcp.c 57KB
ppp.c 56KB
sockets.c 56KB
tcp_in.c 50KB
msg_in.c 43KB
etharp.c 43KB
tcp.c 43KB
ipcp.c 40KB
ppp_oe.c 38KB
api_msg.c 33KB
tcp_out.c 32KB
mib_structs.c 30KB
udp.c 28KB
igmp.c 27KB
pbuf.c 27KB
ip_frag.c 26KB
auth.c 26KB
dns.c 25KB
chap.c 25KB
fsm.c 24KB
msg_out.c 22KB
ip.c 22KB
lwiplib.c 21KB
stellarisif.c 20KB
mem.c 19KB
vj.c 19KB
api_lib.c 17KB
asn1_dec.c 17KB
pap.c 15KB
autoip.c 15KB
netif.c 14KB
asn1_enc.c 14KB
tcpip.c 14KB
inet_chksum.c 12KB
md5.c 12KB
chpms.c 12KB
netdb.c 11KB
ip6.c 11KB
memp.c 11KB
icmp.c 11KB
init.c 10KB
raw.c 10KB
sys.c 10KB
ethernetif.c 9KB
startup_ewarm.c 9KB
randm.c 8KB
slipif.c 8KB
inet.c 7KB
loopif.c 7KB
netbuf.c 7KB
icmp6.c 6KB
stats.c 5KB
inet6.c 4KB
Main.c 4KB
netifapi.c 4KB
Target.c 4KB
ip_addr.c 3KB
sys_arch.c 3KB
magic.c 3KB
err.c 3KB
ip6_addr.c 3KB
perf.c 2KB
CHANGELOG 83KB
COPYING 2KB
LwIP_ETH.dbgdt 4KB
LwIP_ETH.dep 33KB
LwIP_ETH.dni 413B
LwIP_ETH.ewd 33KB
LwIP_ETH.ewp 44KB
LwIP_ETH.eww 162B
FILES 1KB
FILES 507B
FILES 328B
FILES 312B
FILES 146B
opt.h 43KB
tcp.h 25KB
ppp.h 16KB
lwipopts.h 15KB
sockets.h 13KB
snmp.h 12KB
arch.h 10KB
snmp_structs.h 10KB
snmp_msg.h 9KB
sys.h 9KB
netif.h 9KB
fsm.h 8KB
api.h 8KB
dhcp.h 8KB
chap.h 7KB
lcp.h 7KB
ip_addr.h 6KB
ip.h 6KB
ppp_oe.h 6KB
vj.h 6KB
etharp.h 6KB
共 225 条
- 1
- 2
- 3
资源评论
- zhou_chr2013-06-27是TCP客户端实验代码,可是没法用keil下载在lm3s8962上运行
- niu0niu0niu2015-06-06很有参考价值
- boilingcity_12013-04-20完整的代码,很有参考价值,谢谢分享
zhihua_zhouchan
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功