天线|RF射频
直播中

李青

7年用户 405经验值
私信 关注
[问答]

如何利用LWIP重新设置ip?

我的板子是作为服务器的程序,移植的lwip 我想能够在程序运行过程中重新修改服务器自身的ip地址,可是试了几次还是不行,首先我直接把程序中的  IP4_ADDR(&ipaddr, 192, 168, 1, 18);  //设置网络接口的ip地址  IP4_ADDR(&netmask, 255, 255, 255, 0);//子网掩码  IP4_ADDR(&gw, 192, 168, 1, 1);//网关IP改了,然后 执行tcp_close(iris_PCB);
然后再执行LwIP_Init();
               CMD_init();
结果作为client的PC根本就没办法再连上作为服务器的板子了(不管是新设的ip还是以前老的IP)。请各位大虾指点迷津 O(∩_∩)O谢谢
下面是两个函数的具体代码:
void LwIP_Init( void ){  struct ip_addr ipaddr;  struct ip_addr netmask;  struct ip_addr gw;
   /*调用LWIP初始化函数,   初始化网络接口结构体链表、内存池、pbuf结构体*/   lwip_init();  #if LWIP_DHCP   //若使用DHCP协议  ipaddr.addr = 0;  netmask.addr = 0;  gw.addr = 0; #else///*//by iris 2013.6.6//  IP4_ADDR(&ipaddr, 192, 168, 0, 18);  //设置网络接口的ip地址  IP4_ADDR(&netmask, 255, 255, 255, 0);//子网掩码  IP4_ADDR(&gw, 192, 168, 0, 1);//网关*///by iris 2013.6.6//  IP4_ADDR(&ipaddr, 192, 168, 1, 18);  //设置网络接口的ip地址  IP4_ADDR(&netmask, 255, 255, 255, 0);//子网掩码  IP4_ADDR(&gw, 192, 168, 1, 1);//网关
#endif     /*初始化enc28j60与LWIP的接口,参数为网络接口结构体、ip地址、  子网掩码、网关、网卡信息指针、初始化函数、输入函数*/  netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, ðernetif_init, ðernet_input);   /*把enc28j60设置为默认网卡 .*/  netif_set_default(&enc28j60);
#if LWIP_DHCP   //若使用了DHCP  /*  Creates a new DHCP client for this interface on the first call.  Note: you must call dhcp_fine_tmr() and dhcp_coarse_tmr() at  the predefined regular intervals after starting the client.  You can peek in the netif->dhcp struct for the actual DHCP status.*/  dhcp_start(&enc28j60);   //启动DHCP#endif
  /*  When the netif is fully configured this function must be called.*/  netif_set_up(&enc28j60); //使能enc28j60接口}
  struct tcp_pcb *pcb;//定义一个tcp控制块               /* Create a new TCP control block  */  pcb = tcp_new();   //给tcp控制块分配内存空间              
  /* Assign to the new pcb a local IP address and a port number */  /* Using IP_ADDR_ANY allow the pcb to be used by any local interface */  tcp_bind(pcb, IP_ADDR_ANY, 23);     //把PCB控制块绑定到本机的所有IP地址,端口为23  
  /* Set the connection to the LISTEN state */  pcb = tcp_listen(pcb);  //监听该端口
  /* Specify the function to be called when a connection is established */  tcp_accept(pcb, CMD_accept);    //监听的端口接通后调用的函数HelloWorld_accept
  iris_pcb=pcb;  //the mathod is wrong //by iris 2013.6.3}

回帖(2)

李维嘉

2019-7-30 09:51:53
你好,我现在也在做这方便的内容,不知道你的功能实现了没?你是通过什么机制修改IP的呢?是外部有个命令触发吗?
举报

张东群

2019-7-30 09:59:01
受到警告
提示: 作者被禁止或删除 内容自动屏蔽
举报

更多回帖

发帖
×
20
完善资料,
赚取积分