我的stm32107+lwip作为tcp服务器端可以跟pc客户端正常通信,而如果作为tcp客户端则无法连接到pc服务器。这是哪里出了问题啊?希望有这方面经验的指导一下小弟,先谢谢了!
代码如下:
#include "lwip/tcp.h"
#include "lwip/ip_addr.h"
#include <string.h>
#define GREETING "Hello. \r\n"
#define HELLO "Hello "
#define MAX_NAME_SIZE 32
struct name
{
int length;
char bytes[MAX_NAME_SIZE];
};
void tcp_client_init(void);
err_t tcp_client_connect(void *arg, struct tcp_pcb *pcb, err_t err);
static void tcpaa_conn_err(void *arg, err_t err);
static err_t tcpaa_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
static err_t tcpaa_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
tcp_write(pcb, GREETING, strlen(GREETING), 1);
return ERR_OK;
}
void tcp_client_init(void)
{
err_t ret;
struct tcp_pcb *pcb=NULL;
IP4_ADDR(&client_addr, 192, 168, 0, 192);
IP4_ADDR(&server_addr, 192, 168, 0, 32);
/* Create a new TCP control block */
pcb = tcp_new();
if(pcb!=NULL)
{
/* Assign to the new pcb a local IP address and a port number */
tcp_bind(pcb, &client_addr, 1010);//客户端端口号为33
//注册连接出错处理函数
tcp_err(pcb, tcpaa_conn_err); //hrz
tcp_recv(pcb, tcpaa_recv);//接收到数据的回调函数设置
/* Connect to the server: send the SYN */
ret=tcp_connect(pcb, &server_addr, 1010, tcp_client_connect);//服务器端口号为25
}
}
err_t tcp_client_connect(void *arg, struct tcp_pcb *pcb, err_t err)
{
tcp_arg(pcb, mem_calloc(sizeof(struct name), 1));
tcp_recv(pcb, tcpaa_recv);//接收到数据的回调函数设置
tcp_write(pcb, GREETING, strlen(GREETING), 1);
return ERR_OK;
}
static void tcpaa_conn_err(void *arg, err_t err)
{
struct name *name;
name = (struct name *)arg;
mem_free(name);
while(1);
}
|