lwip1.4.1连接经常会断开无法连接上,可以ping通

发布于 2014-11-13 08:23:40
描述:改自realtouch的代码,用作服务器端,使用tcpip通信,上位机连接一段时间后,会自动断开,无法收发数据,上位机报错the network connection was closed by the peer,下位机报错-12或者-13,此时可以ping通。这是什么原因?
服务器端代码如下:

static void tcpserv_thread_entry(void* parameter)
{
char *recv_data_buf,*send_data_buf; //
rt_uint32_t sin_size;
int sock, connected;
struct sockaddr_in server_addr, client_addr;
rt_uint16_t received_len,send_len;

recv_data_buf = (char *)rt_malloc(RECV_BUF_SIZE); //
send_data_buf = (char *)rt_malloc(SEND_BUF_SIZE);
if (NULL == recv_data_buf || NULL == send_data_buf)
{
rt_kprintf("No memory
");
goto TCP_SERVER_QUIT;
}

rt_memset(recv_data_buf,0x0,RECV_BUF_SIZE);
rt_memset(send_data_buf,0x0,SEND_BUF_SIZE);

//
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
//
rt_kprintf("Socket error
");
goto TCP_SERVER_QUIT;
}

//
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT); //
server_addr.sin_addr.s_addr = INADDR_ANY;
rt_memset(&(server_addr.sin_zero),8, sizeof(server_addr.sin_zero));

//
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
//
rt_kprintf("Unable to bind
");
goto TCP_SERVER_QUIT;
}

//
if (listen(sock, 5) == -1)
{
rt_kprintf("Listen error
");
goto TCP_SERVER_QUIT;
}

rt_kprintf("TCPServer Waiting for client on port %d...
",SERVER_PORT);

sin_size = sizeof(struct sockaddr_in);
while(1)
{
//
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);

//
received_len = recv(connected,recv_data_buf, RECV_BUF_SIZE, 0);
if (received_len > 0)
{
//ATE_LinkComing(ATE_LINK_ETH);
//recv_data_buf[received_len] = '';

TCP_data_process(recv_data_buf,received_len,send_data_buf,&send_len);
//rt_uint16_t temp = SHORT_little_endian_TO_big_endian(send_len);
send(connected, &send_len, sizeof(send_len), 0); //
send(connected, send_data_buf, send_len, 0);//

//rt_memset(send_data_buf,0x0,send_len);
}
/* 关闭这个connected socket */
lwip_close(connected);
}
TCP_SERVER_QUIT:
rt_kprintf("error,TCP Server quit
");
//
lwip_close(sock);

//
rt_free(recv_data_buf);
rt_free(send_data_buf);

RT_ASSERT(0);
return ;
}

无法连接后,lwip的log就停住了
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=48014
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=0, netbuf=200178b0
lwip_recvfrom: buflen=8 len=2048 off=0 sock->lastoffset=0
lwip_recvfrom(1): addr=192.168.1.20 port=48014 len=8
lwip_recvfrom: deleting netbuf=200178b0
lwip_send(1, data=2000e290, size=2, flags=0x0)
lwip_send(1) err=0 written=2
lwip_send(1, data=2001197c, size=8, flags=0x0)
lwip_send(1) err=0 written=8
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=48015
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=0, netbuf=200178b0
lwip_recvfrom: buflen=8 len=2048 off=0 sock->lastoffset=0
lwip_recvfrom(1): addr=192.168.1.20 port=48015 len=8
lwip_recvfrom: deleting netbuf=200178b0
lwip_send(1, data=2000e290, size=2, flags=0x0)
lwip_send(1) err=0 written=2
lwip_send(1, data=2001197c, size=8, flags=0x0)
lwip_send(1) err=0 written=8
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=47757
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=-12, netbuf=00000000
lwip_recvfrom(1): buf == NULL, error is "Connection closed."!
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=47758
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=-12, netbuf=00000000
lwip_recvfrom(1): buf == NULL, error is "Connection closed."!
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=47759
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=-12, netbuf=00000000
lwip_recvfrom(1): buf == NULL, error is "Connection closed."!
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=48017
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=0, netbuf=200178b0
lwip_recvfrom: buflen=8 len=2048 off=0 sock->lastoffset=0
lwip_recvfrom(1): addr=192.168.1.20 port=48017 len=8
lwip_recvfrom: deleting netbuf=200178b0
lwip_send(1, data=2000e290, size=2, flags=0x0)
lwip_send(1) err=0 written=2
lwip_send(1, data=2001197c, size=8, flags=0x0)
lwip_send(1) err=0 written=8
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=47760
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000

finsh />
finsh />
finsh />
此时,ping是正常的
1.JPG

查看更多

关注者
0
被浏览
8k
8 个回答
cris8259
cris8259 2014-11-13
log2:
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=43217
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=0, netbuf=200178b0
lwip_recvfrom: buflen=8 len=2048 off=0 sock->lastoffset=0
lwip_recvfrom(1): addr=192.168.1.20 port=43217 len=8
lwip_recvfrom: deleting netbuf=200178b0
lwip_send(1, data=2000e290, size=2, flags=0x0)
lwip_send(1) err=0 written=2
lwip_send(1, data=2001197c, size=8, flags=0x0)
lwip_send(1) err=0 written=8
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=43120
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=-12, netbuf=00000000
lwip_recvfrom(1): buf == NULL, error is "Connection closed."!
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=43121
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=-12, netbuf=00000000
lwip_recvfrom(1): buf == NULL, error is "Connection closed."!
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=43122
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=-12, netbuf=00000000
lwip_recvfrom(1): buf == NULL, error is "Connection closed."!
lwip_close(1)
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=43219
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000
lwip_recvfrom: netconn_recv err=0, netbuf=200178b0
lwip_recvfrom: buflen=8 len=2048 off=0 sock->lastoffset=0
lwip_recvfrom(1): addr=192.168.1.20 port=43219 len=8
lwip_recvfrom: deleting netbuf=200178b0
lwip_send(1, data=2000e290, size=2, flags=0x0)
lwip_send(1) err=0 written=2
lwip_send(1, data=2001197c, size=8, flags=0x0)
lwip_send(1) err=0 written=8
lwip_close(1)
lwip_accept(0)...
lwip_accept(0): netconn_peer failed, err=-13
lwip_recvfrom(-1, 20011170, 2048, 0x0, ..)
get_socket(-1): invalid
lwip_send(-1, data=2000e290, size=2, flags=0x0)
get_socket(-1): invalid
lwip_send(-1, data=2001197c, size=8, flags=0x0)
get_socket(-1): invalid
lwip_close(-1)
get_socket(-1): invalid
lwip_accept(0)...
lwip_accept(0) returning new sock=1 addr=192.168.1.20 port=43124
lwip_recvfrom(1, 20011170, 2048, 0x0, ..)
lwip_recvfrom: top while sock->lastdata=00000000

finsh />
finsh />
pangwei
pangwei 2014-11-14
received_len =0后会怎么样,分析下程序逻辑。
cris8259
cris8259 2014-11-14
如果把下位机lwip的log打开,更容易出现这个现象,像是下位机反应比较慢,上位机连接频繁的时候,更容易出现。不知道下位机发送的过程中,上位机断掉连接会怎么样?好像是上位机提前断掉连接造成的
cris8259
cris8259 2014-11-14
finsh />list_tcps()
Active PCB states:
#0 192.168.1.30:8259 <==> 192.168.1.20:9478 snd_nxt 0x00001996 rcv_nxt 0x91CF872A state: ESTABLISHED
#1 192.168.1.30:8259 <==> 192.168.1.20:9425 snd_nxt 0x00001991 rcv_nxt 0xD4BF749B state: ESTABLISHED
#2 192.168.1.30:8259 <==> 192.168.1.20:9424 snd_nxt 0x00001990 rcv_nxt 0x18D0CA05 state: ESTABLISHED
Listen PCB states:
#0 local port 8259 state: LISTEN
#1 local port 8258 state: LISTEN
TIME-WAIT PCB states:
#0 192.168.1.30:8259 <==> 192.168.1.20:9476 snd_nxt 0x00001ACE rcv_nxt 0x587BF184 state: TIME_WAIT
#1 192.168.1.30:8259 <==> 192.168.1.20:9474 snd_nxt 0x00001AC9 rcv_nxt 0xF8DA7249 state: TIME_WAIT
#2 192.168.1.30:8259 <==> 192.168.1.20:9472 snd_nxt 0x00001AC4 rcv_nxt 0xE6C2E03B state: TIME_WAIT
#3 192.168.1.30:8259 <==> 192.168.1.20:9470 snd_nxt 0x00001ABF rcv_nxt 0xA72865F4 state: TIME_WAIT
#4 192.168.1.30:8259 <==> 192.168.1.20:9469 snd_nxt 0x00001ABD rcv_nxt 0x77A2AC4B state: TIME_WAIT
#5 192.168.1.30:8259 <==> 192.168.1.20:9468 snd_nxt 0x00001ABC rcv_nxt 0x725C3CE9 state: TIME_WAIT
#6 192.168.1.30:8259 <==> 192.168.1.20:9467 snd_nxt 0x00001ABB rcv_nxt 0x336F4EE0 state: TIME_WAIT
#7 192.168.1.30:8259 <==> 192.168.1.20:9466 snd_nxt 0x00001ABA rcv_nxt 0xB844F9AA state: TIME_WAIT
#8 192.168.1.30:8259 <==> 192.168.1.20:9465 snd_nxt 0x00001AB9 rcv_nxt 0x22C1365B state: TIME_WAIT
#9 192.168.1.30:8259 <==> 192.168.1.20:9464 snd_nxt 0x00001AB8 rcv_nxt 0x4FD807FD state: TIME_WAIT
#10 192.168.1.30:8259 <==> 192.168.1.20:9463 snd_nxt 0x00001AB7 rcv_nxt 0xF7C5DC7E state: TIME_WAIT
#11 192.168.1.30:8259 <==> 192.168.1.20:9462 snd_nxt 0x00001AB6 rcv_nxt 0x967F9497 state: TIME_WAIT
#12 192.168.1.30:8259 <==> 192.168.1.20:9461 snd_nxt 0x00001AB5 rcv_nxt 0xFB08EDBB state: TIME_WAIT
#13 192.168.1.30:8259 <==> 192.168.1.20:9460 snd_nxt 0x00001AB4 rcv_nxt 0x7BAE068E state: TIME_WAIT
#14 192.168.1.30:8259 <==> 192.168.1.20:9459 snd_nxt 0x00001AB3 rcv_nxt 0xF957ACD1 state: TIME_WAIT
#15 192.168.1.30:8259 <==> 192.168.1.20:9458 snd_nxt 0x00001AB2 rcv_nxt 0x6F136874 state: TIME_WAIT
#16 192.168.1.30:8259 <==> 192.168.1.20:9457 snd_nxt 0x00001AB1 rcv_nxt 0x801398C5 state: TIME_WAIT
#17 192.168.1.30:8259 <==> 192.168.1.20:9456 snd_nxt 0x00001AB0 rcv_nxt 0xE35FF6AD state: TIME_WAIT
#18 192.168.1.30:8259 <==> 192.168.1.20:9455 snd_nxt 0x00001AAF rcv_nxt 0x353D635B state: TIME_WAIT
#19 192.168.1.30:8259 <==> 192.168.1.20:9454 snd_nxt 0x00001AAE rcv_nxt 0x8EA1F9D4 state: TIME_WAIT
#20 192.168.1.30:8259 <==> 192.168.1.20:9453 snd_nxt 0x00001AAE rcv_nxt 0x3C005A71 state: TIME_WAIT
#21 192.168.1.30:8259 <==> 192.168.1.20:9452 snd_nxt 0x00001AAE rcv_nxt 0xFBC2A967 state: TIME_WAIT
#22 192.168.1.30:8259 <==> 192.168.1.20:9451 snd_nxt 0x00001AAE rcv_nxt 0xEC83FD22 state: TIME_WAIT
#23 192.168.1.30:8259 <==> 192.168.1.20:9449 snd_nxt 0x00001AAE rcv_nxt 0xDE5D53D8 state: TIME_WAIT
#24 192.168.1.30:8259 <==> 192.168.1.20:9448 snd_nxt 0x00001AAE rcv_nxt 0xCD98303F state: TIME_WAIT
#25 192.168.1.30:8259 <==> 192.168.1.20:9447 snd_nxt 0x00001AAE rcv_nxt 0x8E43C3DD state: TIME_WAIT
#26 192.168.1.30:8259 <==> 192.168.1.20:9446 snd_nxt 0x00001AAE rcv_nxt 0xBBCF57CC state: TIME_WAIT
#27 192.168.1.30:8259 <==> 192.168.1.20:9445 snd_nxt 0x00001AAE rcv_nxt 0x8E183D2C state: TIME_WAIT
#28 192.168.1.30:8259 <==> 192.168.1.20:9444 snd_nxt 0x00001AAE rcv_nxt 0x7DFAD1DD state: TIME_WAIT
#29 192.168.1.30:8259 <==> 192.168.1.20:9443 snd_nxt 0x00001AAE rcv_nxt 0xA7089B8F state: TIME_WAIT
#30 192.168.1.30:8259 <==> 192.168.1.20:9442 snd_nxt 0x00001AAE rcv_nxt 0x441EC083 state: TIME_WAIT
#31 192.168.1.30:8259 <==> 192.168.1.20:9441 snd_nxt 0x00001AAE rcv_nxt 0x51ACFC8F state: TIME_WAIT
#32 192.168.1.30:8259 <==> 192.168.1.20:9440 snd_nxt 0x00001AAE rcv_nxt 0x590B31E9 state: TIME_WAIT
#33 192.168.1.30:8259 <==> 192.168.1.20:9439 snd_nxt 0x00001AAE rcv_nxt 0xE475F1D2 state: TIME_WAIT
#34 192.168.1.30:8259 <==> 192.168.1.20:9438 snd_nxt 0x00001AAE rcv_nxt 0x3C4A763E state: TIME_WAIT
#35 192.168.1.30:8259 <==> 192.168.1.20:9437 snd_nxt 0x00001AAE rcv_nxt 0x294EAAA5 state: TIME_WAIT
0, 0x00000000
cris8259
cris8259 2014-11-14
无法连接时出现多个time_wait
bernard
bernard 2014-11-14
lwIP这边不要使用RT的内存管理,不打补丁会造成多个TIME_WAIT状态的TCP连接。
cris8259
cris8259 2014-11-14
没有打开宏RT_LWIP_USING_RT_MEM。不打补丁是什么意思?问题大概出在哪里?之前没出现过?

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友