使用了STM32F407+LWIP+FreeRTOS,需要与服务器进行tcp通信,采用了socket接口,连接都成功了,但是使用函数recv接收时,一直返回0,即断开连接。怎么会这样子啊啊,我要疯了啊啊 。谁能帮帮我啊啊?代码如下:
while(gnetif.ip_addr.addr == 0)
{//获取到本机ip
osDelay(1000);
}
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("119.103.33.44");
server_addr.sin_port = htons(1234);
while(1)
{
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if(sock_fd == -1)
{
osDelay(1000); //1秒
}
else
{
res = connect(sock_fd, (struct sockaddr *)&server_addr, sizeof(struct sockaddr));
if(0 == res)
{
static s8 recv_flag;
while(1)
{
//接收
recv_flag = recv(sock_fd,recv_data_buf,RECV_INSTR_DATA_NUM,0);
if(recv_flag == 0)
{//另一端已关闭
break;
}
else if(recv_flag == -1)
{//接收失败,下次再接收
}
else
{
//接收正确
}
osDelay(50);
}
close(sock_fd);
}
else
{
close(sock_fd);
osDelay(500);
}
}
osDelay(500);
}
|