title: How to stop socket from receiving data when you want?
body: <p>I am working with a mp3 link sniffing project, which i'm successfully to sniff the link with a simple raw-socket method. The problem is this project requires to download the link, so my first thought is to stop the socket from receiving raw data when you don't want to sniff, then choose the link that is stored, but i don't know how to stop the socket while the program is running. Any help is welcome to show me the point of this problem, or even a new way to deal with it.</p>
  
  <p>Edit: here is my code:</p>
  
  <pre><code>#define _WINSOCK_DEPRECATED_NO_WARNINGS
  #define callerr {cout &lt;&lt; "Error: " &lt;&lt; WSAGetLastError() &lt;&lt; endl; system("pause"); return 0;}
  #define failerr {cout &lt;&lt; "Failed.\n"; system("pause"); return;}
  #define SIO_RCVALL _WSAIOW(IOC_VENDOR, 1)
  
  #include &lt;WinSock2.h&gt;
  #include &lt;iostream&gt;
  #include &lt;iphlpapi.h&gt;
  #include &lt;sstream&gt;
  #include &lt;vector&gt;
  
  using namespace std;
  
  struct ip_addr
  {
      u_char byte1;
      u_char byte2;
      u_char byte3;
      u_char byte4;
  };
  
  struct ip_hdr
  {
      unsigned char ip_header_len : 4;
      unsigned char ip_version : 4;
      unsigned char ip_tos;
      unsigned short ip_total_length;
      unsigned short ip_id;
      unsigned char ip_frag_offset : 5;
      unsigned char ip_more_fragment : 1;
      unsigned char ip_dont_fragment : 1;
      unsigned char ip_reserved_zero : 1;
      unsigned char ip_frag_offset1;
      unsigned char ip_ttl;
      unsigned char ip_protocol;
      unsigned short ip_checksum;
      unsigned int ip_srcaddr;
      unsigned int ip_destaddr;
  };
  
  struct tcp_hdr
  {
      unsigned short source_port;
      unsigned short dest_port;
      unsigned int sequence;
      unsigned int acknowledge;
      unsigned char ns : 1;
      unsigned char reserved_part1 : 3;
      unsigned char data_offset : 4;
      unsigned char fin : 1;
      unsigned char syn : 1;
      unsigned char rst : 1;
      unsigned char psh : 1;
      unsigned char ack : 1;
      unsigned char urg : 1;
      unsigned char ecn : 1;
      unsigned char cwr : 1;
      unsigned short window;
      unsigned short checksum;
      unsigned short urgent_pointer;
  };
  
  sockaddr_in source, dest;
  PIP_ADAPTER_ADDRESSES adapter(NULL), i;
  u_long bs = 65536;
  ip_hdr *ih;
  tcp_hdr *th;
  vector &lt;char*&gt; url;
  
  void getUrl(char *tmp, char *tmp1)
  {
      int i = 0;
      char *t = strstr(tmp, "Host: ");
      t += 6;
      while (*t != 13)
      {
          tmp1[i] = *t;
          ++t;
          ++i;
      }
      t = strstr(tmp, "GET ");
      t += 4;
      while (*t != ' ')
      {
          tmp1[i] = *t;
          ++t;
          ++i;
      }
      tmp1[i] = 0;
      url.push_back(tmp1);
  
      cout &lt;&lt; "Url: " &lt;&lt; tmp1 &lt;&lt; endl;
  }
  
  void processPacket(char *buff, int size, SOCKET capture)
  {
      ih = (ip_hdr*)buff;
      if (ih-&gt;ip_protocol == 6)
      {
          th = (tcp_hdr*)(buff + ih-&gt;ip_header_len * 4);
          char *data = (char*)((char*)th + th-&gt;data_offset * 4);
          int len = ntohs(ih-&gt;ip_total_length) - ih-&gt;ip_header_len * 4 - th-&gt;data_offset * 4;
          data[len] = 0;
          char tmp[65536], tmp1[65536];
          for (int i = 0; i &lt;= len; ++i) tmp[i] = data[i];
          if (strstr(tmp, "GET ") != 0 and strstr(tmp, ".mp3 ")) getUrl(tmp, tmp1);
      }
  }
  
  void capturePacket(SOCKET capture)
  {
      char *buff = (char*)malloc(bs); //Kich thuoc toi da cua mot packet
      int res;
  
      if (!buff) failerr;
  
      do
      {
          res = recvfrom(capture, buff, bs, 0, 0, 0);
          if (res &gt; 0) processPacket(buff, res, capture);
          else failerr;
      } while (res &gt; 0);
  
      free(buff);
  }
  
  int main()
  {
      adapter = (PIP_ADAPTER_ADDRESSES)malloc(bs);
      SOCKET capture;
      int inum = 0, id, j, in, set = 1;
      WSAData wsa;
  
      cout &lt;&lt; "Initializing Winsock... ";
      if (WSAStartup(MAKEWORD(2, 2), &amp;wsa)) callerr;
      cout &lt;&lt; "Succesfully initialized.\n\nSearching interface list... ";
  
      if (GetAdaptersAddresses(2, 0, NULL, adapter, &amp;bs) != ERROR_SUCCESS) callerr;
      cout &lt;&lt; "Interface list: \n";
      for (i = adapter; i; i = i-&gt;Next)
      {
          wcout &lt;&lt; ++inum &lt;&lt; ": " &lt;&lt; i-&gt;FriendlyName &lt;&lt; ", IPv4 address: ";
          sockaddr_in *tmp = reinterpret_cast&lt;sockaddr_in*&gt;(i-&gt;FirstUnicastAddress-&gt;Address.lpSockaddr);
          cout &lt;&lt; inet_ntoa(tmp-&gt;sin_addr) &lt;&lt; endl;
      }
  
      do
      {
          cout &lt;&lt; "\nEnter interface number: "; cin &gt;&gt; id;
          if (id &lt; 1 or id &gt; inum) cout &lt;&lt; "Invalid number.";
      } while (id &lt; 1 or id &gt; inum);
      for (i = adapter, j = 0; j &lt; id - 1; ++j, i = i-&gt;Next);
  
  
      sockaddr_in *tmp = reinterpret_cast&lt;sockaddr_in*&gt;(i-&gt;FirstUnicastAddress-&gt;Address.lpSockaddr);
      memset(&amp;dest, 0, sizeof(dest));
      memcpy(&amp;dest.sin_addr.s_addr, &amp;tmp-&gt;sin_addr.S_un, sizeof(dest.sin_addr.s_addr));
      dest.sin_family = AF_INET;
      dest.sin_port = 0;
  
      cout &lt;&lt; "\nInitializing RAW socket... ";
      capture = socket(AF_INET, SOCK_RAW, IPPROTO_IP);
      if (capture == INVALID_SOCKET) callerr;
      cout &lt;&lt; "Successfully initialized.\nBinding socket to localhost port 0... ";
  
      if (bind(capture, (sockaddr *)&amp;dest, sizeof(dest)) == SOCKET_ERROR) callerr;
      cout &lt;&lt; "Successfully binded.\nSetting socket... ";
  
  
      if (WSAIoctl(capture, SIO_RCVALL, &amp;set, sizeof(set), 0, 0, (PDWORD) &amp;in, 0, 0) == SOCKET_ERROR) callerr;
      cout &lt;&lt; "Successfully set-up.\n\nStarting TCP packet capturing. \n";
  
      capturePacket(capture);
  }
  </code></pre>