lwip获取socket的状态
时间: 2023-08-31 14:10:18 浏览: 331
在lwIP中,可以使用`netconn_get_info()`函数来获取一个连接(socket)的状态信息。该函数的定义如下:
```
err_t netconn_get_info(struct netconn *conn, struct netconn_info *info);
```
其中,`conn`参数是一个指向`struct netconn`类型的指针,表示要获取信息的连接;`info`参数是一个指向`struct netconn_info`类型的指针,用于存储获取到的连接信息。
`struct netconn_info`类型定义如下:
```
struct netconn_info {
u16_t pcb_type; /* protocol control block type (TCP/UDP) */
u16_t local_port; /* local port */
u16_t remote_port; /* remote port */
ip_addr_t local_ip; /* local IP address */
ip_addr_t remote_ip; /* remote IP address */
u8_t used; /* whether the pcb is used or not */
u8_t state; /* TCP state or UDP PCB state */
u32_t rcv_wnd; /* receive window */
u32_t rcv_queued; /* receive queued */
u32_t snd_queued; /* send queued */
u32_t acked; /* ACKed */
u32_t unacked; /* UnACKed */
u32_t last_ack; /* Last ACKed */
u32_t last_rcv_ack; /* Last ACK sent */
u32_t last_rcv_wnd; /* Last receive window */
u32_t total_rx; /* Total received */
u32_t total_tx; /* Total transmitted */
u32_t err_tx; /* Transmit errors */
u32_t err_rx; /* Receive errors */
u32_t err_connect; /* Connect errors */
u32_t err_timeout; /* Timeout errors */
u32_t err_abrt; /* Abort errors */
};
```
在获取连接状态信息后,可以根据`state`字段的值来判断连接的状态。具体的状态值定义在`tcp.h`或`udp.h`中。例如,以下是TCP连接的状态值定义:
```
/* TCP connection states */
#define CLOSED 0 /* closed */
#define LISTEN 1 /* listening for connection */
#define SYN_SENT 2 /* active, have sent syn */
#define SYN_RCVD 3 /* have sent and received syn */
#define ESTABLISHED 4 /* established */
#define FIN_WAIT_1 5 /* have closed, sent fin */
#define FIN_WAIT_2 6 /* have closed, fin is acked */
#define CLOSE_WAIT 7 /* have closed, remote peer is closing */
#define CLOSING 8 /* closed xchd FIN; await FIN-ACK */
#define LAST_ACK 9 /* had fin and close; await FIN-ACK */
#define TIME_WAIT 10 /* in 2*msl quiet wait after close */
```
因此,可以通过判断`state`字段的值来确定连接的状态,进而进行相应的处理。
阅读全文