用STM32F103ZET6+ENC28J60+LWIP,将开发板和电路通过路由连到同一局域网内,成功实现了client,server功能,正常连接,通信正常,也无大家说了上线下线的现象。后面加上了DHCP功能,成功分配IP,server功能测试,正常;但,测试client功能时,无法连接成功。
发现tcp_connect(client_pcb, &destip, 4, tcp_client_connected);并不调用tcp_client_connected;不知道什么原因。有人能解答一下么。
部分代码
int main(void)
{
sys_config();
SysTick_Init();
LwIP_Init();
tcp_server_init();
tcp_client_init();
while(1)
{
LwIP_Periodic_Handle(LocalTime);
}
}
void tcp_server_init(void)
{
struct tcp_pcb *pcb;
printf("tcp server init\n");
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 1200);
pcb = tcp_listen(pcb);
tcp_accept(pcb, tcp_server_accept);
}
void tcp_client_init(void)
{
struct tcp_pcb *client_pcb;
struct tcp_client_app_arg* app_arg;
static struct ip_addr destip;
printf("tcp client inti\n");
IP4_ADDR(&destip,192,168,0,105);
client_pcb = tcp_new();
tcp_bind(client_pcb,IP_ADDR_ANY,4);
if(client_pcb != NULL)
{
tcp_arg(client_pcb, mem_calloc(sizeof(struct tcp_client_app_arg), 1));
app_arg = client_pcb->callback_arg;
app_arg->app_state = CLIENT_WAITING_FOR_CONNECTION;
tcp_connect(client_pcb, &destip, 4, tcp_client_connected);
}
else
{
printf("tcp alloc failed\n");
}
}
err_t tcp_client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
struct tcp_client_app_arg *app_arg = (struct tcp_client_app_arg *)arg;
uint8_t* str = "Welcome to the client\n";
printf("tcp client connected\n");
tcp_err(pcb, tcp_client_errf); //指定出错时的回调函数
tcp_recv(pcb, tcp_client_recv); //指定接收到新数据时的回调函数
tcp_sent(pcb, tcp_client_sent); //指定远程主机成功接收到数据的回调函数
tcp_poll(pcb, tcp_client_poll, 4); //指定轮询的时间间隔和回调函数(*250ms)
tcp_write(pcb, (const char *)str, strlen(str), 1);
app_arg->app_state = CLIENT_WAITING_FOR_CMD;
return ERR_OK;
}