https://github.com/dashersw/docker-cloud-multicast/blob/master/mcreceive.c

$ ./mcreceive X.X.X.X  60001

其中,X.X是组播地址

Received 28 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: Ϭ(
Received 56 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: (
Received 28 bytes from X.X.X.X: Ϭ(
Received 56 bytes from X.X.X.X: Ϭ(
Received 168 bytes from X.X.X.X: Ϭ(
Received 28 bytes from X.X.X.X: %Ϭ(
Received 28 bytes from X.X.X.X: &Ϭ(
Received 28 bytes from X.X.X.X: 'Ϭ(

Ethernet II: 18 bytes
IP: Minimum 20 bytes (can be more with options)
UDP: 8 bytes

相关源代码:

/* create socket to join multicast group on */
if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
  perror("socket() failed");
  exit(1);
}

...

/* block waiting to receive a packet */
if ((recv_len = recvfrom(sock, recv_str, MAX_LEN, 0,
     (struct sockaddr*)&from_addr, &from_len)) < 0) {
  perror("recvfrom() failed");
  exit(1);
}

/* output received string */
printf("Received %d bytes from %s: ", recv_len,
       inet_ntoa(from_addr.sin_addr));           

如你所见,我收到了几个长度为28的包.如何接收长度小于18+20+8的UDP数据包?

推荐答案

您创建了一个UDP套接字,因此recvfrom只返回UDP有效负载,而不返回UDP或更低层报头.所以返回的长度就是有效载荷的长度.

原始套接字将使您能够访问以太网、IP和UDP报头.

C++相关问答推荐

与unions 的未定义行为

在C中使用JMP_buf数组进行线程化(在xv6中测试)

使用GOTO从多个嵌套循环C继续

如何在C客户端应用程序的ClientHello消息中添加自定义扩展?

在列表中插入Int指针(C)

如何将常量char*复制到char数组

在C++中访问双指针

我在反转双向链表时遇到问题

正数之和是负数

将回调/基于事件的C API转换为非回调API

pthread_create的用法

如何在zOS上编译共享C库

Linux/C:带有子进程的进程在添加waitid后都挂起

C中的空指针是什么(_N)?

意外的C并集结果

与指针的原始C数组或C++向量<;向量<;双>>;

为什么<到达*时不会转换为>?

SSE 向量与 Epsilon 的比较

创建 makefile 来编译位于不同目录中的多个源文件

为什么 C 字符串并不总是等同于字符数组?