0

嘿伙计们,我一直在尝试制作一个简单的控制台应用程序来查看我的网络流量:P 我试图只是阅读它。

无论如何,我从 WSAIoctl 函数中得到一个错误,WSAGetLastError 给我的错误代码是 10022,MSDN 告诉我它与此相关:

无效的论点。提供了一些无效参数(例如,为 setsockopt 函数指定无效级别)。在某些情况下,它还指代套接字的当前状态——例如,在未侦听的套接字上调用接受。

我试过在没有运气的情况下弄乱论点:/请帮助我:P

这是我的代码:

WSADATA wsaData;
int startup = WSAStartup(0x0202, &wsaData);

if(startup != 0) {
    cout << "Error: could not initalize WSADATA for target socket." << endl;
    system("pause");
}

unsigned long BytesReturned;
int InBuffer, OutBuffer, LPCVoid;
int optValue = 1;
SOCKET sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);

Sleep(await);
cout << "creating and configuring RAW_SOCK" << endl;

int listening = listen(sock, SOMAXCONN); // tried debugging using this.
int sockopt = setsockopt(sock, IPPROTO_IP, 2, (char*)&optValue, sizeof(optValue));
int SockMode = WSAIoctl(sock, SIO_RCVALL, &InBuffer, sizeof(InBuffer), &OutBuffer, sizeof(OutBuffer), &BytesReturned, NULL, NULL);
//0x98000001
if(SockMode == 0) {
    Sleep(await);
    cout << "RAW_SOCKET created successfully!" << endl << "Trying to listen for incoming network packets..." << endl;
    int listeningk = listen(sock, SOMAXCONN);

    if(listening == 0) {
        Sleep(await);
        cout << "socket listening without problems, looking for incoming request..." << endl;
    }
    else {
        Sleep(await);
        cout << "Error: could not listen on socket." << endl;
        exit(EXIT_FAILURE);
    }
}
else {
    Sleep(await);
    cout << "Error: could not create RAW_SOCKET..." << endl << "Dumping SockMode!\r\nint SockMode = " << SockMode << endl;
    cout << "setsockopt = " << sockopt << endl; 
    cout << "WSAGetLastError: " << WSAGetLastError() << endl;
    system("pause");
}
4

1 回答 1

0

您的套接字需要先绑定,然后才能收听。此外,对于此WSAIoctl选项,您必须遵守(来自MSDN 文档):

套接字还必须绑定到显式本地 IPv4 或 IPv6 接口,这意味着您不能绑定到 INADDR_ANY 或 in6addr_any。

我建议在尝试改进此代码之前进行一些基本的自我教育。MSDN 中有一些常见操作的代码示例,例如套接字设置。

于 2013-01-29T19:23:22.847 回答