问题描述
我想在Visual C ++中使用RAW Socket
.
I want to use a RAW Socket
in Visual C++.
我在Linux
上看到了一个功能,
I saw a function on Linux
which is
int out = socket(AF_INET, SOCK_RAW, htons(ETH_P_ALL));
我们可以在linux
中使用此代码,但是如何在Windows平台上使用RAW SOCKET,因为在Visual C ++中使用此代码时出现错误.
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.
预先感谢.
编辑
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
推荐答案
在Windows中,最接近的等效项是 SOCK_RAW ,您将不得不使用一种技术来使您的代码跨平台兼容.
In Windows, the closest equivalent is SOCK_RAW and you will have to use a technique to make your code cross platform compatible.
例如,使用宏,并使用适用于Windows的WSAPROTOCOL 和适用于Linux的POSIX库.
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.
此处是有关如何在Winsock中使用原始套接字的指南. .这是关于如何使用宏识别平台的答案
Here is a guide on how to use raw sockets with Winsock.Here is an an answer on how to identify platform with macros.
这篇关于C ++ Windows中的RAW套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!