gpt4 book ai didi

C UDP组播接收包丢失

转载 作者:行者123 更新时间:2023-11-30 17:22:09 26 4
gpt4 key购买 nike

我编写了一个 C 程序来接收 UDP 多播数据包,并且它可以工作。然而,当程序运行几秒钟时,丢包的情况经常发生。当我按Ctrl + C终止程序并重新启动它时,它会收到大部分数据包,但几秒钟后就会发生数据包丢失。这很奇怪,到底是什么问题呢?这是我的 C 代码。

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <android/log.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>

#define BUF_SIZE 4

int mjpeg_main(){
int sockfd;
struct sockaddr_in localSock;
struct ip_mreq group;
char buf[BUF_SIZE];
int len = sizeof(localSock);

/* Create a datagram socket on which to receive. */
if((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("Can't create socket!");
exit(1);
}

/* Enable SO_REUSEADDR to allow multiple instances of this */
/* application to receive copies of the multicast datagrams. */
int reuse = 1;
if(setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&reuse, sizeof(reuse)) < 0)
{
perror("Setting SO_REUSEADDR error");
close(sockfd);
exit(1);
}

int n = 1024 * 1024;
if (setsockopt(sockfd,SOL_SOCKET,SO_RCVBUFFORCE,(const char*)&n, sizeof(int)) == -1)
{
// deal with failure, or ignore if you can live with the default size
}



/* Bind to the proper port number with the IP address */
/* specified as INADDR_ANY. */
memset((char *) &localSock, 0, sizeof(localSock));
localSock.sin_family = AF_INET;
localSock.sin_port = htons(2068);
localSock.sin_addr.s_addr = INADDR_ANY;
if(bind(sockfd, (struct sockaddr*)&localSock, sizeof(localSock)))
{
perror("Binding datagram socket error");
close(sockfd);
exit(1);
}

/* Join the multicast group 239.100.15.101 on the local 203.106.93.94 */
/* interface. Note that this IP_ADD_MEMBERSHIP option must be */
/* called for each local interface over which the multicast */
/* datagrams are to be received. */
group.imr_multiaddr.s_addr = inet_addr("239.100.15.101");
group.imr_interface.s_addr = inet_addr("10.100.15.115");
if(setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0)
{
perror("Adding multicast group error");
close(sockfd);
exit(1);
}


printf("Start to receive!n");
__android_log_print(ANDROID_LOG_INFO,"mjpeg","mjpeg start !!");

//Wait for connect
while(1)
{
memset(buf, 0, sizeof(buf));
int z = recvfrom(sockfd, // Socket
buf, // Receiving buffer
sizeof buf, // Max recv buf size
0, // Flags: no options
(struct sockaddr *)&localSock,// Addr
&len); // Addr len, in & out
if ( z < 0 )
{
perror("recv");
exit(1);
}

printf("Frame: %02X:%02X, Sequence: %02X:%02X\n", buf[0], buf[1], buf[2], buf[3]);
}

close(sockfd);
return 0;
}

而且,这就是结果。发送端是一个硬件,一个帧包含很多序列。序列号将依次增加。

Frame: 09:1F, Sequence: 00:00Frame: 09:1F, Sequence: 00:01Frame: 09:1F, Sequence: 00:02Frame: 09:1F, Sequence: 00:03Frame: 09:1F, Sequence: 00:04Frame: 09:1F, Sequence: 00:05Frame: 09:1F, Sequence: 00:06Frame: 09:1F, Sequence: 00:07Frame: 09:1F, Sequence: 00:08Frame: 09:1F, Sequence: 00:09Frame: 09:1F, Sequence: 00:0AFrame: 09:1F, Sequence: 00:0BFrame: 09:1F, Sequence: 00:0CFrame: 09:1F, Sequence: 00:0DFrame: 09:1F, Sequence: 00:0EFrame: 09:1F, Sequence: 00:0FFrame: 09:1F, Sequence: 00:10Frame: 09:1F, Sequence: 00:11Frame: 09:1F, Sequence: 00:12Frame: 09:1F, Sequence: 00:13Frame: 09:1F, Sequence: 00:14Frame: 09:1F, Sequence: 00:15Frame: 09:1F, Sequence: 00:16Frame: 09:1F, Sequence: 00:17Frame: 09:1F, Sequence: 00:18Frame: 09:1F, Sequence: 00:19Frame: 09:1F, Sequence: 00:1AFrame: 09:1F, Sequence: 00:1BFrame: 09:1F, Sequence: 00:1CFrame: 09:1F, Sequence: 00:1DFrame: 09:1F, Sequence: 00:1EFrame: 09:1F, Sequence: 00:1FFrame: 09:1F, Sequence: 00:20Frame: 09:1F, Sequence: 00:21Frame: 09:1F, Sequence: 00:22Frame: 09:1F, Sequence: 00:23Frame: 09:1F, Sequence: 00:24Frame: 09:1F, Sequence: 00:25Frame: 09:1F, Sequence: 00:26Frame: 09:1F, Sequence: 00:27Frame: 09:1F, Sequence: 00:28Frame: 09:1F, Sequence: 00:29Frame: 09:1F, Sequence: 00:2AFrame: 09:1F, Sequence: 00:2BFrame: 09:1F, Sequence: 00:2CFrame: 09:1F, Sequence: 00:2DFrame: 09:1F, Sequence: 00:2EFrame: 09:1F, Sequence: 00:2FFrame: 09:1F, Sequence: 00:30Frame: 09:1F, Sequence: 00:31Frame: 09:1F, Sequence: 00:32Frame: 09:1F, Sequence: 00:33Frame: 09:1F, Sequence: 00:34Frame: 09:1F, Sequence: 00:35Frame: 09:1F, Sequence: 00:36Frame: 09:1F, Sequence: 00:37Frame: 09:1F, Sequence: 00:38Frame: 09:1F, Sequence: 00:39Frame: 09:1F, Sequence: 00:3AFrame: 09:1F, Sequence: 00:3BFrame: 09:1F, Sequence: 00:3CFrame: 09:1F, Sequence: 00:3DFrame: 09:1F, Sequence: 00:3EFrame: 09:1F, Sequence: 00:3FFrame: 09:1F, Sequence: 00:40Frame: 09:1F, Sequence: 00:41Frame: 09:1F, Sequence: 00:42Frame: 09:1F, Sequence: 00:43Frame: 09:1F, Sequence: 00:44Frame: 09:1F, Sequence: 00:45Frame: 09:1F, Sequence: 00:46Frame: 09:1F, Sequence: 00:47Frame: 09:1F, Sequence: 00:48Frame: 09:1F, Sequence: 00:49Frame: 09:1F, Sequence: 00:4AFrame: 09:1F, Sequence: 00:4BFrame: 09:1F, Sequence: 00:4CFrame: 09:1F, Sequence: 00:4DFrame: 09:1F, Sequence: 80:4EFrame: 09:20, Sequence: 00:00Frame: 09:20, Sequence: 00:01Frame: 09:20, Sequence: 00:02Frame: 09:20, Sequence: 00:03Frame: 09:20, Sequence: 00:04Frame: 09:20, Sequence: 00:05Frame: 09:20, Sequence: 00:06Frame: 09:20, Sequence: 00:07Frame: 09:20, Sequence: 00:08Frame: 09:20, Sequence: 00:09Frame: 09:20, Sequence: 00:0AFrame: 09:20, Sequence: 00:0BFrame: 09:20, Sequence: 00:0CFrame: 09:20, Sequence: 00:0DFrame: 09:20, Sequence: 00:0EFrame: 09:20, Sequence: 00:0FFrame: 09:20, Sequence: 00:10Frame: 09:20, Sequence: 00:11Frame: 09:20, Sequence: 00:12Frame: 09:20, Sequence: 00:13Frame: 09:20, Sequence: 00:14Frame: 09:20, Sequence: 00:15Frame: 09:20, Sequence: 00:16Frame: 09:20, Sequence: 00:17Frame: 09:20, Sequence: 00:18Frame: 09:20, Sequence: 00:19Frame: 09:20, Sequence: 00:1AFrame: 09:20, Sequence: 00:1BFrame: 09:20, Sequence: 00:1CFrame: 09:20, Sequence: 00:1DFrame: 09:20, Sequence: 00:1EFrame: 09:20, Sequence: 00:1FFrame: 09:20, Sequence: 00:20Frame: 09:20, Sequence: 00:21Frame: 09:20, Sequence: 00:22Frame: 09:20, Sequence: 00:23Frame: 09:20, Sequence: 00:24Frame: 09:20, Sequence: 00:25Frame: 09:20, Sequence: 00:26Frame: 09:20, Sequence: 00:27Frame: 09:20, Sequence: 00:28Frame: 09:20, Sequence: 00:29Frame: 09:20, Sequence: 00:2EFrame: 09:20, Sequence: 00:31Frame: 09:20, Sequence: 00:37Frame: 09:20, Sequence: 00:3BFrame: 09:20, Sequence: 00:3DFrame: 09:20, Sequence: 00:3FFrame: 09:20, Sequence: 00:41Frame: 09:20, Sequence: 00:42Frame: 09:20, Sequence: 00:44Frame: 09:20, Sequence: 00:46Frame: 09:20, Sequence: 00:4BFrame: 09:20, Sequence: 80:4EFrame: 09:21, Sequence: 00:00Frame: 09:21, Sequence: 00:01start packet lost...Frame: 09:21, Sequence: 00:06Frame: 09:21, Sequence: 00:10Frame: 09:21, Sequence: 00:1CFrame: 09:21, Sequence: 00:25Frame: 09:21, Sequence: 00:2FFrame: 09:21, Sequence: 00:35Frame: 09:21, Sequence: 00:3BFrame: 09:21, Sequence: 00:3CFrame: 09:21, Sequence: 00:3EFrame: 09:21, Sequence: 00:40Frame: 09:21, Sequence: 00:42Frame: 09:21, Sequence: 00:43Frame: 09:21, Sequence: 00:45Frame: 09:21, Sequence: 00:47Frame: 09:21, Sequence: 00:4DFrame: 09:22, Sequence: 00:01Frame: 09:22, Sequence: 00:05Frame: 09:22, Sequence: 00:0DFrame: 09:22, Sequence: 00:1BFrame: 09:22, Sequence: 00:23Frame: 09:22, Sequence: 00:2FFrame: 09:22, Sequence: 00:34Frame: 09:22, Sequence: 00:3AFrame: 09:22, Sequence: 00:3CFrame: 09:22, Sequence: 00:3EFrame: 09:22, Sequence: 00:40Frame: 09:22, Sequence: 00:41Frame: 09:22, Sequence: 00:43Frame: 09:22, Sequence: 00:45Frame: 09:22, Sequence: 00:47Frame: 09:22, Sequence: 00:4DFrame: 09:23, Sequence: 00:01Frame: 09:23, Sequence: 00:04Frame: 09:23, Sequence: 00:0AFrame: 09:23, Sequence: 00:19Frame: 09:23, Sequence: 00:20Frame: 09:23, Sequence: 00:2EFrame: 09:23, Sequence: 00:32Frame: 09:23, Sequence: 00:39Frame: 09:23, Sequence: 00:3CFrame: 09:23, Sequence: 00:3EFrame: 09:23, Sequence: 00:3FFrame: 09:23, Sequence: 00:41Frame: 09:23, Sequence: 00:43Frame: 09:23, Sequence: 00:45Frame: 09:23, Sequence: 00:46Frame: 09:23, Sequence: 00:4D

最佳答案

问题已经解决了。原因是我们的PHY使用的是外部晶振,内核时钟配置不正确。

关于C UDP组播接收包丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28081907/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com