|
本帖最后由 oneV 于 2021-4-8 20:19 编辑
在调试NETXDUO的TCP/UDP过程中遇到很奇怪的现象,求教
ST例程中使用如下方式接收数据
- ret = nx_udp_socket_receive(&UDPSocket, &data_packet, NX_WAIT_FOREVER);
- if (ret == NX_SUCCESS)
- {
- /* data is available, read it into the data buffer */
- nx_packet_data_retrieve(data_packet, data_buffer, &bytes_read);
-
- /* get info about the client address and port */
- nx_udp_source_extract(data_packet, &source_ip_address, &source_port);
- /* print the client address, the remote port and the received data */
- // PRINT_DATA(source_ip_address, source_port, data_buffer, bytes_read);
- /* resend the same packet to the client */
- ret = nx_udp_socket_send(&UDPSocket, data_packet, source_ip_address, source_port);
- }
复制代码 这里在创建UDP的时候使用
- /* create the UDP socket */
- ret = nx_udp_socket_create(&IpInstance, &UDPSocket, "UDP Server Socket", NX_IP_NORMAL, NX_FRAGMENT_OKAY, NX_IP_TIME_TO_LIVE, QUEUE_MAX_SIZE);
复制代码 QUEUE_MAX_SIZE为512,data_buffer大小为512,如果外部发送来的数据大于512程序就会死掉,原因是 nx_packet_data_retrieve函数在复制数据包到data_buffer中的时候如果这个buffer小了就会破坏内存引发不可以预测的结果。那么问题来了,在不知道外界数据包大小的情况下这个情况如何避免呢?而我试过TCP并不存在这个问题。
第二个就是上面的例程中如果将nx_udp_socket_send注释掉,那么数据只能接收6次就死掉了,TCP、UDP皆如此,这又是什么原因?
|
|