c++ - "Destination address required"尝试通过 SOCK_RAW 套接字发送数据时

标签 c++ sockets network-programming freebsd

我正在尝试发送数据包(不是 ip 数据包,而是 EAP 请求标识),但是 send() 函数返回 -1。

    int sockfd = socket(AF_INET, SOCK_RAW, 0);
printf("socket: %d\n", sockfd);
struct sockaddr_in sock;
printf("creating packet\n");
char packet[27];
memcpy(packet + 0, (u_char* ) ethernet->ether_dhost, 6);
memcpy(packet + 6, (u_char* ) ethernet->ether_shost, 6);
memcpy(packet + 12, (u_char* ) ethernet + 12, 2);
memcpy(packet + 14, (u_char *)reqid, 1);
memcpy(packet + 15, (u_char *)reqid+1, 1);
memcpy(packet + 16, (u_char *)reqid+2, 4);
memcpy(packet + 20, (u_char *)reqid+6, 1);
memcpy(packet + 21, (u_char *)reqid+7, 1);
memcpy(packet + 22, (u_char *)reqid+8, 4);
memcpy(packet + 26, (u_char *)reqid+12, 1);
printf("sending packet\n");
if(send(sockfd, packet, sizeof(packet), 0) == -1)
{
     printf("packet not sent\n");
     //return;
}

数据包由以太网数据包和请求标识组成。 我知道数据包很好,每个值都在正确的位置。但是 send() 函数失败了。
errno show "Destination address required"并且 sockfd 值为 4。 顺便说一句,这是在 FreeBSD 上。
谢谢 编辑: 我没有目的地的 IP 地址。我只有它的MAC地址。

最佳答案

来自手册页:

The send() call may be used only when the socket is in a connected state (so that the intended recipient is known).

我相信你想要sendto,而不是send

关于c++ - "Destination address required"尝试通过 SOCK_RAW 套接字发送数据时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36570953/

相关文章:

c++ - Boost 网络库在 MAC OSX 上无法识别

c++ - 将带有存储在 std::function 中的捕获子句的 lambda 转换为原始函数指针

c++ - CUDA cudaMalloc 在运行具有巨大静态数组的内核后失败

c++ - 编译但得到 'uncaught exception of type std::bad_cast'

使用 url :port/url in getaddrinfo 的 c++ 套接字编程

c - 为什么 pcap_datalink() 总是返回 1(以太网),即使在无线设备上也是如此?

c++ - UI 配置模式

c# - socket 应该保持打开还是拆除

java - 如何在java中接收socketServer上的对象

c++ - 有没有办法阻止套接字 send() 直到我们得到该数据包的确认?