TCP LwIP服务器未对TCP客户端发送的[FIN,ACK]做出响应。

huangapple go评论67阅读模式
英文:

TCP LwIP server does not respond to [FIN,ACK] from the TCP client?

问题

Here is the translated content you provided:

所以,如果你查看我的帖子历史记录,你会知道我一直在尝试使用LwIP进行TCP项目。我无法在服务器端捕获数据,但现在我已经解决了这个问题。但现在我有一个新的问题。

我连接到TCP服务器(STM32-nucleo-144),Wireshark显示服务器和客户端之间发生了三次握手,然后我向服务器发送消息,服务器按照编程响应,现在我在客户端断开连接,客户端发送了一个带有[FIN, ACK]标志的数据包,但服务器从未回应[FIN, ACK]。我使用一个名为Hercules的TCP传输应用程序,当我按下“断开连接”时,它显示连接已关闭,但从Wireshark中可以看出这实际上并没有发生。

我还使用了另一个应用程序进行检查,该应用程序以预定的时间间隔连续发送TCP数据包,它创建一个连接,发送数据包,接受响应,然后关闭连接,然后在间隔之后再次重复此过程。当我启动此应用程序时,首次创建了连接,客户端发送了数据并收到了响应,客户端发送了[FIN, ACK]标志,但从那时起服务器没有响应。

我进行了ping测试,似乎服务器在断开连接开始后变得无法访问。

这是我的代码:(代码已被翻译)

以下是Wireshark中在Hercules中按下“断开”按钮后显示的内容:(内容已被翻译)

英文:

so if you look into my post history you will know that I have been trying a project in TCP using LwIP. I was unable to capture data in the server side, but now I have resolved that issue. But now I have a new one.

I connect to the TCP server(STM32-nucleo-144) and the wireshark shows the three-way-handshake happening between the server and the client, then I send messages to the server and the server responds as per the programming, now I terminate the connection on the client side, the client sends a packet with [FIN,ACK] flags set, but the server never responds back with a [FIN,ACK]. I use an App for TCP transmission called hercules, when I press 'disconnect' it displays the message connection closed when it never really happened as it can be seen from wireshark.

I also used another app to check, this app sends TCP packets continuously at predefined intervals, it creates a connection, sends packets, accepts a response then closes the connection, and after the interval it repeats this process again. When I started this app, the first connection was created, data was sent and received by the client, the [FIN,ACK] flags were sent by the client but no response from server from there on.

I did a ping test, it seems the server becomes unreachable as soon as the disconnect starts.

Here is my code:-

 /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LWIP_Init();
  /* USER CODE BEGIN 2 */


  IP_ADDR4(&myIPADDR,192,168,33,2);
  IP_ADDR4(&destip,192,168,33,101);

  tcp_pcb = tcp_new();

  err_t err = tcp_bind(tcp_pcb, &myIPADDR, 4100);

  if(err == ERR_OK)
    {
  	  tcp_pcb = tcp_listen_with_backlog(tcp_pcb, 2);

  	  tcp_accept(tcp_pcb, tcp_echoserver_accept);
    }

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  ethernetif_input(&gnetif);
	  sys_check_timeouts();

  }
  /* USER CODE END 3 */
}
err_t tcp_echoserver_accept(void *arg, struct tcp_pcb *newpcb, err_t err)
{


	tcp_arg(tcp_newpcb, tcp_newpcb);
	tcp_recv(newpcb, recv_callback);
	return ERR_OK;

}

err_t recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
{

	sprintf(data, "this is the message: '%s'", p->payload);

    tcp_recved(tpcb, p->len);
	tcp_write(tpcb, data,p->len+23, 0x02);

    pbuf_free(p);

	err = ERR_OK;
	return err;
}

Here is what is shown is wireshark after i press the disconnect butotn in hercules

13366	6286.571000	192.168.33.101	192.168.33.2	TCP	54	52649 → 4100 [FIN, ACK] Seq=12 Ack=35 Win=65358 Len=0

13367	6286.883778	192.168.33.101	192.168.33.2	TCP	54	[TCP Retransmission] 52649 → 4100 [FIN, ACK] Seq=12 Ack=35 Win=65358 Len=0

13369	6287.498291	192.168.33.101	192.168.33.2	TCP	54	[TCP Retransmission] 52649 → 4100 [FIN, ACK] Seq=12 Ack=35 Win=65358 Len=0

13371	6288.709492	192.168.33.101	192.168.33.2	TCP	54	[TCP Retransmission] 52649 → 4100 [FIN, ACK] Seq=12 Ack=35 Win=65358 Len=0

13374	6291.112720	192.168.33.101	192.168.33.2	TCP	54	[TCP Retransmission] 52649 → 4100 [FIN, ACK] Seq=12 Ack=35 Win=65358 Len=0

答案1

得分: 1

在你的接收回调函数中,你需要检查指针变量 p 是否为 NULL。如果是的话,表示连接在另一端已经关闭,你需要调用 tcp_close。服务器变得无法访问的原因是因为你正在对一个空指针进行解引用,这会触发严重错误。

英文:

In your receive callback, you have to check if the pbuf pointer p is NULL in which case, the connection was closed at the other end and you need to call tcp_close. The reason why the server becomes unreachable is because you are dereferencing a NULL pointer, which would trigger a hard fault.

huangapple
  • 本文由 发表于 2023年4月17日 14:22:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定