RAW Sockets in C++ Windows

两盒软妹~` 提交于 2019-12-10 10:23:31

问题


I want to use a RAW Socket in Visual C++.

I saw a function on Linux which is

int out = socket(AF_INET, SOCK_RAW, htons(ETH_P_ALL));

Using this code in linux we can do that, but how to use RAW SOCKET on Windows Platform because when I use this in Visual C++ I am getting Error.

Thanks in Advance.

EDIT

int out1 = socket(AF_INET, SOCK_RAW, IPPROTO_SCTP);
for (; ;)
{
    int bytesIn = recvfrom(out1, buf, 2000, 0, (sockaddr*)&server, &serverLength);
    if (bytesIn == SOCKET_ERROR)
    {
        cout << "Error Receving from Server" << WSAGetLastError() << endl;
    }
    else
    {
        cout << "MESSAGE RECV from Server " << " : " << bytesIn << endl;
    }
}

This is my code to receive the packets


回答1:


In Windows, the closest equivalent is SOCK_RAW and you will have to use a technique to make your code cross platform compatible.

For example use macros and extend a base generic virtual class into a windows derived class and a linux derived class, using WSAPROTOCOL for Windows and the POSIX library for Linux.

Here is a guide on how to use raw sockets with Winsock. Here is an an answer on how to identify platform with macros.



来源:https://stackoverflow.com/questions/50999531/raw-sockets-in-c-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!