I am trying to write a client/server application using **RAW** sockets.

There are multiple problems:

1. When the client sends a message to the server using *sendto()* method, an error *invalid argument* is returned by *sendto()* method. **Why this error message?**. The corresponding code is marked under the section **ERROR 1**. The code of *sendto()* is commented in this post.
2. Since I have commented the send message part, the client should wait for a message; *recvfrom()* being a blocking system call. Instead, *recvfrom()* returns with a message **E** always. **From where did this message arrive?**. The corresponding code is marked as **ERROR 2**.
3. If I change *protocol* (3rd) argument in *socket()* to **0** or **IPPROTO\_RAW** I get **Protocol not supported error.** **Why these errors?**

The operating system is **Ubuntu**.

```
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    #include <sys/socket.h> // For the socket () etc. functions.
    #include <netinet/in.h> // For IPv4 data struct..
    #include <string.h>     // For memset.
    #include <arpa/inet.h>  // 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 --> IPv4, SOCK_RAW for Raw socket,  
            * 0 --> 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 (&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", &srv_addr.sin_addr); /* Note
            * that third field should point to an in_addr (in6_addr). */
    if (rst <= 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 *) &srv_addr, 
                    sizeof (struct sockaddr_in));
    if (rst < 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 (&srv_addr, 0, addrlen); 

    char buf[BUF_SIZE] = {'\0'};

    rst = recvfrom (sfd, buf, BUF_SIZE, flags, 
                    (struct sockaddr *) &srv_addr, 
                    &addrlen);
    if (rst < 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 *) &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);

}

```