1

Is it possible to read a Network Adapter similar to a Serial Port? I know that Serial Ports can be read with CreateFile WINAPI Function. Is there a similar way to read raw bytes from a Network Adapter?

I am aware of the WiFi/Network Functions but the WiFi Examples are fairly sparse.

  • Duplicate of http://stackoverflow.com/questions/703713/windows-networking-using-only-ethernet-frames – Prof. Falken Oct 11 '10 at 08:26
  • @Kant: Sorry I do not believe this is a duplicate because 1. I do not want to communicate in Ethernet Frames. 2. I would like a WINAPI answer preferably. –  Oct 11 '10 at 13:43
  • Please be more precise then. What exactly are you trying to communicate over? – Axel Gneiting Oct 11 '10 at 17:17

2 Answers2

1

If you want to capture raw packets you need a support driver like WinPCAP to do that.

Axel Gneiting
  • 5,293
  • 25
  • 30
1

You can pass the SOCK_RAW flag when you create the socket using WSASocket() (or socket(), as your tastes run). This is described in further detail under TCP/IP Raw Sockets on MSDN.

From that page --

Once an application creates a socket of type SOCK_RAW, this socket may be used to send and receive data. All packets sent or received on a socket of type SOCK_RAW are treated as datagrams on an unconnected socket.

Of note, Microsoft crippled their raw sockets implementation after Windows XP SP2; the details are described on the MSDN page in the section Limitations on Raw Sockets:

  • TCP data cannot be sent over raw sockets.
  • UDP datagrams with an invalid source address cannot be sent over raw sockets.
  • A call to the bind function with a raw socket is not allowed.

If these limitations are too restrictive, you can fall back to the previously recommended winpcap library.

J.J.
  • 5,019
  • 2
  • 28
  • 26