下图1可以看到,配置了LWIP的连接数量是5,然后在PC机的客户端TCP工具重复打开-关闭的操作,到达5次之后,再也无法连接,提示如图2的错误,跟踪代码感觉应该是资源没有释放,但是程序里已经调用了closesocket()函数,为什么还会不释放资源呢?,代码如图3,这个是我操作的问题,还是lwip的问题呢?lwip版本2.1.2
图1
图2
void socket_server_accept_thread()
{
int s_fd = -1,rtn = -1;
int c_fd;
struct sockaddr_in addr_in,addr_client;
unsigned int port = 8080;
s_fd=socket(AF_INET,SOCK_STREAM,0); //创建套接字
memset(&addr_in,0,sizeof(struct sockaddr_in));
memset(&addr_client,0,sizeof(struct sockaddr_in));
addr_in.sin_family = AF_INET;
addr_in.sin_port = htons(port);
addr_in.sin_addr.s_addr= htonl(INADDR_ANY);//inet_addr("192.168.2.100"); //接受来自任何client的请求。
char net_rx_buffer[256];
int on = 1,len,rdlen = -1;
if(bind(s_fd,(struct sockaddr*)&addr_in,sizeof(struct sockaddr))==-1)
{
rt_kprintf("error2\r\n");
}
rtn = listen(s_fd,10);
rt_thread_delay(100);
while(1)
{
c_fd = accept(s_fd,(struct sockaddr*)&addr_client,(socklen_t *)&len);
while(1)
{
memset(net_rx_buffer,0,256);
rdlen = recv(c_fd,net_rx_buffer,256,0);
// rdlen = read(c_fd,net_rx_buffer,256);
if (rdlen > 0)
{
rt_kprintf("%s",net_rx_buffer);
}
else
{
closesocket(c_fd);
break;
}
rt_thread_delay(5);
}
}
}