title: Raw sockets: sendto() and recvfrom() not working
body: <p>I am trying to write a client/server application using <strong>RAW</strong> sockets.</p>
  
  <p>There are multiple problems:</p>
  
  <ol>
  <li><p>When the client sends a message to the server using <em>sendto()</em> method, an error <em>invalid argument</em> is returned by <em>sendto()</em> method. <strong>Why this error message?</strong>. The corresponding code is marked under the section <strong>ERROR 1</strong>. The code of <em>sendto()</em> is commented in this post.</p></li>
  <li><p>Since I have commented the send message part, the client should wait for a message; <em>recvfrom()</em> being a blocking system call. Instead, <em>recvfrom()</em> returns with a message <strong>E</strong> always. <strong>From where did this message arrive?</strong>. The corresponding code is marked as <strong>ERROR 2</strong>.</p></li>
  <li><p>If I change <em>protocol</em> (3rd) argument in <em>socket()</em> to <strong>0</strong> or <strong>IPPROTO_RAW</strong> I get <strong>Protocol not supported error.</strong> <strong>Why these errors?</strong></p></li>
  </ol>
  
  <p>The operating system is <strong>Ubuntu</strong>.</p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;unistd.h&gt;
  
      #include &lt;sys/socket.h&gt; // For the socket () etc. functions.
      #include &lt;netinet/in.h&gt; // For IPv4 data struct..
      #include &lt;string.h&gt;     // For memset.
      #include &lt;arpa/inet.h&gt;  // For inet_pton ().
  
      #define BUF_SIZE 30
  
      void main ()
  
      {
          int rst; // Return status of functions.
      /**************** Create a socket. *******************************/
      int sfd; // Socket file descriptor.
      sfd = socket (AF_INET, SOCK_RAW, IPPROTO_UDP); /* 
              * AF_INET --&gt; IPv4, SOCK_RAW for Raw socket,  
              * 0 --&gt; for any protocol. */
      if (sfd == -1) 
      {
          perror ("Client: socket error");
          exit (1);
      }
  
  
      /*********** Server's address ***********************************/
      struct sockaddr_in srv_addr;
      socklen_t addrlen = sizeof (struct sockaddr_in); 
  
      // Initializing the server's address to zero.
      memset (&amp;srv_addr, 0, addrlen); 
  
      srv_addr.sin_family = AF_INET;    // Address is in IPv4 format.
      // srv_addr.sin_port   = htons (0);  // Port number of the server. 
  
  
      rst = inet_pton (AF_INET, "127.0.0.1", &amp;srv_addr.sin_addr); /* Note
              * that third field should point to an in_addr (in6_addr). */
      if (rst &lt;= 0)
      {
          perror ("Client Presentation to network address conversion.\n");
          exit (1);
      }        
  
  
  
      /****************** ERROR 1 ************************************
      ******************* Sending message to the server. *************/
      const int flags = 0;
      const char *msg = "Hello";
      /* rst = sendto (sfd, msg, strlen(msg)+1, flags, 
                      (struct sockaddr *) &amp;srv_addr, 
                      sizeof (struct sockaddr_in));
      if (rst &lt; 0)
      {
          perror ("Client: Sendto function call failed");
          exit (1);
      }
      else 
          printf ("Client: Sent data size = %d\n", rst);
  
      */
  
  
      /******************* ERROR 2 ***********************************
       ******************* Receiving message from server. ************/
      // Initializing the server's address to zero.
      memset (&amp;srv_addr, 0, addrlen); 
  
      char buf[BUF_SIZE] = {'\0'};
  
      rst = recvfrom (sfd, buf, BUF_SIZE, flags, 
                      (struct sockaddr *) &amp;srv_addr, 
                      &amp;addrlen);
      if (rst &lt; 0)
      {
          perror ("Client: couldn't receive");
          exit (1);
      }
      printf ("Message from server = |%s|\n", buf);
  
      /* Address of the server. */
      const char *buf2 = inet_ntop (AF_INET, 
                         (struct sockaddr *) &amp;srv_addr, buf, BUF_SIZE);
      if (buf2 == NULL)
      {
          perror ("Client: Conversion of sender's address to presentation failed");
          exit (1);
      }
  
      printf ("Servers address,  = %s\n", buf2);
      close (sfd);
  
  }
  </code></pre>