title: Creating packet sniffer in c++
body: <p>I have before created a simple packet sniffer using python, with the following code:</p>
  <pre><code>import socket
  import os
  import time
  
  host = &quot;192.168.0.164&quot;
  
  if os.name == &quot;nt&quot;:
      socket_protocol = socket.IPPROTO_IP
  else:
      socket_protocol = socket.IPPROTO_ICMP
  
  while True:
      sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
  
      sniffer.bind((host, 0))
  
      sniffer.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1)
  
      sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)
  
      out = sniffer.recvfrom(65565)
  
      print(&quot;recieved {0} bytes from: {1}:{2}&quot;.format(len(out[0]), out[1][0], out[1][1]))
  
      sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_OFF)
  </code></pre>
  <p>In a loop it prints out the size in bytes of the recieved packet data and the ip adress of the sender and port.
  Im trying to recreate this program using c++ and this is what i have go so far:</p>
  <pre><code>#include &lt;winsock2.h&gt;
  #include &lt;Ws2tcpip.h&gt;
  #include &lt;Mstcpip.h&gt;
  #include &lt;iostream&gt;
  #include &lt;windows.h&gt;
  
  #define PORT 0
  
  struct sockaddr_in address;
  
  SOCKET sock;
  
  const char opt = 0;
  
  u_long mode_on = RCVALL_ON;
  
  u_long mode_off = RCVALL_OFF;
  
  char buffer;
  
  int main()
  {
  
      address.sin_family = AF_INET;
      address.sin_addr.s_addr = INADDR_ANY;
      address.sin_port = htons(PORT);
  
      sock = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
  
      setsockopt(sock, IPPROTO_IP, IP_HDRINCL, &amp;opt, sizeof(opt));
  
      bind(sock, (struct sockaddr*) &amp; address, sizeof(address));
      ioctlsocket(sock, SIO_RCVALL, &amp;mode_on);
  
      recv(sock, &amp;buffer, 65565, 0);
  
      std::cout &lt;&lt; buffer &lt;&lt; std::endl;
  
      ioctlsocket(sock, SIO_RCVALL, &amp;mode_off);
  
      return 0;
  }
  </code></pre>
  <p>I am not very familiar with socket programming in c++.
  When i run the program no output is being printed out.
  I have tried running the program in a loop to see if it just was that there wasnt any incoming packet at exactly that time but still no luck.
  There are no errors occuring when i run the program.</p>