移植的网上的程序,PC端使用调试助手做服务端,查看可以ping通但是无法建立联系,请问大佬们是什么原因
#include "lwip/netif.h"
#include "lwip/ip.h"
#include "lwip/tcp.h"
#include "lwip/init.h"
#include "netif/etharp.h"
#include "lwip/udp.h"
#include "lwip/pbuf.h"
#include <stdio.h>
#include <string.h>
#include "main.h"
static void client_err(void *arg, err_t err) //³öÏÖ´íÎóʱµ÷ÓÃÕâ¸öº¯Êý£¬´òÓ¡´íÎóÐÅÏ¢£¬²¢³¢ÊÔÖØÐÂÁ¬½Ó
{
printf("Á¬½Ó´íÎó!!\n");
printf("³¢ÊÔÖØÁ¬!!\n");
//Á¬½Óʧ°ÜµÄʱºòÊÍ·ÅTCP¿ØÖÆ¿éµÄÄÚ´æ
printf("¹Ø±ÕÁ¬½Ó£¬ÊÍ·ÅTCP¿ØÖÆ¿éÄÚ´æ\n");
//tcp_close(client_pcb);
//ÖØÐÂÁ¬½Ó
printf("ÖØгõʼ»¯¿Í»§¶Ë\n");
TCP_Client_Init();
}
static err_t client_send(void *arg, struct tcp_pcb *tpcb) //·¢Ëͺ¯Êý£¬µ÷ÓÃÁËtcp_writeº¯Êý
{
uint8_t send_buf[]= "ÎÒÊÇ¿Í»§¶Ë\n";
//·¢ËÍÊý¾Ýµ½·þÎñÆ÷
tcp_write(tpcb, send_buf, sizeof(send_buf), 1);
return ERR_OK;
}
static err_t client_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{
if (p != NULL)
{
/* ½ÓÊÕÊý¾Ý*/
tcp_recved(tpcb, p->tot_len);
/* ·µ»Ø½ÓÊÕµ½µÄÊý¾Ý*/
tcp_write(tpcb, p->payload, p->tot_len, 1);
memset(p->payload, 0 , p->tot_len);
pbuf_free(p);
}
else if (err == ERR_OK)
{
//·þÎñÆ÷¶Ï¿ªÁ¬½Ó
printf("·þÎñÆ÷¶Ï¿ªÁ¬½Ó!\n");
tcp_close(tpcb);
//ÖØÐÂÁ¬½Ó
TCP_Client_Init();
}
return ERR_OK;
}
static err_t client_connected(void *arg, struct tcp_pcb *pcb, err_t err)
{
printf("connected ok!\n");
//×¢²áÒ»¸öÖÜÆÚÐԻص÷º¯Êý
tcp_poll(pcb,client_send,2);
//×¢²áÒ»¸ö½ÓÊÕº¯Êý
tcp_recv(pcb,client_recv);
return ERR_OK;
}
void TCP_Client_Init(void)
{
struct tcp_pcb *client_pcb = NULL; //ÕâÒ»¾äÒ»¶¨Òª·ÅÔÚÀïÃ棬·ñÔò»áûÓÃ
ip4_addr_t server_ip; //ÒòΪ¿Í»§¶ËÒªÖ÷¶¯Á¬½Ó·þÎñÆ÷£¬ËùÒÔÒªÖªµÀ·þÎñÆ÷µÄIPµØÖ·
/* ´´½¨Ò»¸öTCP¿ØÖÆ¿é */
client_pcb = tcp_new();
IP4_ADDR(&server_ip, DEST_IP_ADDR0,DEST_IP_ADDR1,DEST_IP_ADDR2,DEST_IP_ADDR3);//ºÏ²¢IPµØÖ·
printf("¿Í»§¶Ë¿ªÊ¼Á¬½Ó!\n");
//¿ªÊ¼Á¬½Ó
tcp_connect(client_pcb, &server_ip, TCP_CLIENT_PORT, client_connected);
ip_set_option(client_pcb, SOF_KEEPALIVE);
printf("ÒѾµ÷ÓÃÁËtcp_connectº¯Êý\n");
//×¢²áÒì³£´¦Àí
tcp_err(client_pcb, client_err);
printf("ÒѾע²áÒì³£´¦Àíº¯Êý\n");
}
|