下面是我的程序,能实现收发功能了。但是前提必须是我先把服务器打开,再运行程序才能连接上服务器进行传输。如果我先运行程序,再打开服务器就连不上了。
因此我想问一下我在main里的循环程序里,怎么判断网络连接还在不在,不在的话我怎么重新连接?void tcp_client_init(void){uint16_t ei;IP4_ADDR(&ipaddr, 192, 168, 100, 174); //????t
PCB = tcp_new();TPCD = tpcb;if(tpcb != NULL){ei = tcp_bind(tpcb, IP_ADDR_ANY, 3000);if(ei == ERR_OK){ tcp_connect(tpcb, &ipaddr, 3000, tcp_client_accept);}}}err_t tcp_client_accept(void *arg, struct tcp_pcb *tpcb, err_t err){tcp_recv(tpcb, tcp_client_recv);return ERR_OK; }
//TCP的接收函数sta
tic err_t tcp_client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err){uint8_t Data_len;tcp_recved(tpcb, p->tot_len);//接收到的数据存在了 p->payload地址下Data_len = p->len;tcp_write(tpcb,p->payload,Data_len,1); //接收到的数据回传pbuf_free(p); //把接收缓冲区清空return ERR_OK;}
uint16_t tcp_ei;int main(void){
/* Configure ethernet (GPIOs, clocks, MAC, DMA) */ ETH_BSP_Config(); /* Initilaize the LwIP stack */ LwIP_Init(); tcp_client_init();
/* Infinite loop */ while (1) { /* check if any packet received 下面几条语句以后放到任务里?任务切换要及时?*/if (ETH_CheckFrameReceived()){ /* process received ethernet packet */ LwIP_Pkt_Handle();}/* handle periodic timers for LwIP */LwIP_Periodic_Handle(LocalTime); } }