gpt4 book ai didi

c - winsock2:RAW SOCKET recvfrom() 返回错误 10022(无效参数)

转载 作者:行者123 更新时间:2023-12-03 11:59:08 35 4
gpt4 key购买 nike

我正在尝试在 Windows 10 中使用 RAW SOCKET 处理传入的网络数据包。

当我调用 recvfrom()函数它返回 -1 值。 WSAGetLastError是 10022。

Microsoft Docs 页面给了我以下描述:

WSAEINVAL: 10022
Invalid argument.
Some invalid argument was supplied (for example, specifying an invalid level to the setsockopt function). In some instances, it also refers to the current state of the socket—for instance, calling accept on a socket that is not listening.



我尝试使用 setsockopt()设置 IP_HDRINCL为 1,但当我调用 recvfrom() 时返回相同的错误.

我的源代码:
#include "stdio.h"
#include "winsock2.h"
#include "ws2tcpip.h"


int main(){
SOCKET s;
char* buff = malloc(256);
int optval = 1;
struct sockaddr_in adr;
int adr_length = sizeof(adr);
int i;
int rcv_len;
WSADATA wsa;

memset(buff, 0, 256);

if(WSAStartup(MAKEWORD(2,2), &wsa) != 0){
printf("Error in WSAStartup: %d\n", WSAGetLastError());
getch();
return 0;
}

s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if(s == SOCKET_ERROR){
printf("Error in socket creation: %d\n", WSAGetLastError());
getch();
return 0;
}

if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval)) == -1){
printf("Error in setsockopt(): %d\n", WSAGetLastError());
getch();
closesocket(s);
return 0;
}

while(1){
rcv_len = recvfrom(s, buff, 256, 0, (struct sockaddr*)&adr, &adr_length);

if(rcv_len == -1){
printf("Error in recvfrom(): %d\n", WSAGetLastError());
getch();
break;
}

for(i = 0; i < 256; i ++){
printf("%c", *(buff + i));
}
}

closesocket(s);

return 1;
}

返回:
Error in recvfrom(): 10022

最佳答案

我已经解决了这个问题。

缺少什么:

  • 使用 bind() 打开 SOCKET使用您正在处理的接口(interface)的 IP
  • 设置选项 SO_RCVALL 1 与 WSAIoctl()

  • 所以,现在代码正在使用这个源:
    #include "stdio.h"
    #include "winsock2.h"
    #include "ws2tcpip.h"

    int main(){
    SOCKET s;
    unsigned char* buff = malloc(32768);
    int optval = 1;
    struct sockaddr_in adr;
    struct sockaddr_in sadr;
    int adr_length = 0;
    int i;
    int in;
    int rcv_len;
    WSADATA wsa;

    memset(buff, 0, 32768);

    if(WSAStartup(MAKEWORD(2,2), &wsa) != 0){
    printf("Error in WSAStartup: %d\n", WSAGetLastError());
    getch();
    return 0;
    }

    s = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
    if(s == SOCKET_ERROR){
    printf("Error in socket creation: %d\n", WSAGetLastError());
    getch();
    return 0;
    }

    sadr.sin_family = AF_INET;
    sadr.sin_addr.s_addr = inet_addr("127.0.0.1");
    sadr.sin_port = htons(0);

    bind(s, (struct sockaddr*)&sadr, sizeof(sadr));

    if(setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&optval, sizeof(optval)) == -1){
    printf("Error in setsockopt(): %d\n", WSAGetLastError());
    getch();
    closesocket(s);
    return 0;
    }

    optval = 1;
    WSAIoctl(s, SIO_RCVALL, &optval, sizeof(optval), 0, 0, (LPDWORD) &in, 0, 0);

    while(1){
    adr_length = sizeof(adr);
    rcv_len = recvfrom(s, buff, 32768, 0, (struct sockaddr*)&adr, &adr_length);

    if(rcv_len == -1){
    printf("Error in recvfrom(): %d\n", WSAGetLastError());
    getch();
    break;
    }

    for(i = 0; i < rcv_len; i ++){
    printf("%d ", *(buff + i));
    }
    }

    closesocket(s);

    return 1;
    }

    谢谢你们!

    关于c - winsock2:RAW SOCKET recvfrom() 返回错误 10022(无效参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61701164/

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