4

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

  • So, you got an error. Do you want to elaborate? What error? – harper Jun 23 '18 at 09:15
  • There is nothing like "C++ Windows", but there are different NDIS implementation in different Windows versions. Without knowing the version your are lost. – harper Jun 23 '18 at 09:17

1 Answers1

4

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.

Attersson
  • 4,755
  • 1
  • 15
  • 29
  • I don't have much knowledge can you provide any example please. – Chandrapal Singh Jhala Jun 23 '18 at 09:12
  • About SOCK_RAW or about cross platform code? Both are kinda vast topics. Can you show some of your existing code? – Attersson Jun 23 '18 at 09:14
  • And so my answer. They are extremely vast topics, so I included links to facilitate your study. – Attersson Jun 23 '18 at 09:36
  • Thank you Sir for the link and your guidance – Chandrapal Singh Jhala Jun 23 '18 at 09:42
  • Glad to help. Windows and Linux are very different and usually you need to use different implementations (libraries) creating 2 versions of your code or 1 version with macros to compile 1 part or another. – Attersson Jun 23 '18 at 09:47
  • Actually the server is camera, which continuously sends RAW packets to the IP address of my PC, I don't want to send anything, So should I create a header as shown in tutorial – Chandrapal Singh Jhala Jun 23 '18 at 10:09
  • @ChandrapalSinghJhala there is no such thing as RAW packets. RAW is not a protocol. I think you misunderstand what the camera is actually sending. For instance, it is likely sending UDP packets containing RAW image data. RAW sockets and RAW image data are two completely different and unrelated things. – Remy Lebeau Jun 23 '18 at 17:08