问题
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