Halaviw
Halaviw

Reputation: 75

UDP INET socket, client not getting response

I am trying to make socket UDP Inet communication, whenever I send from client to server then in my server console I receive my message, but I am not seeing anything in the client terminal when server responds, it hangs on recvfrom. Can someone advice what I am doing wrong?

Server

int main()
{
    int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    struct sockaddr_in adres;

    adres.sin_addr.s_addr = INADDR_ANY;
    adres.sin_port = htons(2001);
    adres.sin_family = AF_INET;

    int bind_socket = bind(udp_socket, (struct sockaddr*) &adres, sizeof(adres));
    if (bind_socket == -1)
    {
        perror("Bind");
        return 0;
    }
    char msg[50];
    char msg1[50];
    strcpy(msg1, "Sending to client!");

    int rcv = recvfrom(udp_socket, msg, sizeof(msg), 0, (struct sockaddr *)&adres, (socklen_t *)sizeof(adres));
    int sending = sendto(udp_socket, msg1, sizeof(msg1), 0, (struct sockaddr *)&adres, sizeof(adres));
    close(udp_socket);

    return 0;
}

Client

int main()
{       
    int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    if(udp_socket < 0) {
        perror("Socket");
    }
    struct sockaddr_in adres;
    adres.sin_addr.s_addr = INADDR_ANY;
    adres.sin_port = htons(2001);
    adres.sin_family = AF_INET;
    char msg[50];
    strcpy(msg, "Sending to server!");
    int sendx = sendto(udp_socket, msg, 50, 0, (struct sockaddr*) &adres, sizeof(adres));
    char rcvmsg[50];
    int rcv = recvfrom(udp_socket, rcvmsg, 50, 0, (struct sockaddr*) &adres, (socklen_t*) sizeof(adres)); 
    printf("%s\n:", rcvmsg);
    close(udp_socket);

    return 0;
}

Upvotes: 0

Views: 44

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595377

Some issues I see:

  • your server is not validating that socket() is successful.

  • you are not zeroing out any unused bytes of your sockaddr_in variables, which may or may not cause them to be interpreted incorrectly, depending on the system.

  • you are ignoring the return values of recvfrom() and sendto() to know whether they are successful or failed.

  • you are passing an invalid socklen_t* pointer to recvfrom(), which will leadd to corruption if recvfrom() tries to write to that address. The parameter is expected to be a pointer to a valid socklen_t variable that specifying the size of adres upon input, and that will receive the size of adres on output.

  • your client is trying to send to INADDR_ANY (0.0.0.0), which is not a valid IP which you can send to, only bind/listen on.

Try this instead:

Server

int main()
{
    int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    if (udp_socket < 0)
    {
        perror("Socket");
        return 1;
    }

    struct sockaddr_in adres;
    memset(&adres, 0, sizeof(adres));
    adres.sin_family = AF_INET;
    adres.sin_addr.s_addr = INADDR_ANY;
    adres.sin_port = htons(2001);

    int bind_socket = bind(udp_socket, (struct sockaddr*) &adres, sizeof(adres));
    if (bind_socket < 0)
    {
        perror("Bind");
        close(udp_socket);
        return 1;
    }

    char msg[50];
    socklen_t adres_len = sizeof(adres);

    int rcv = recvfrom(udp_socket, msg, sizeof(msg), 0, (struct sockaddr *)&adres, &adres_len);
    if (rcv < 0)
    {
        perror("Recv");
        close(udp_socket);
        return 1;
    }

    printf("Received: %.*s\n", (int)rcv, rcvmsg);

    char msg1[50];
    strcpy(msg1, "Sending to client!");

    int sending = sendto(udp_socket, msg1, strlen(msg1), 0, (struct sockaddr *)&adres, adres_len);
    if (sending < 0)    
    {
        perror("Send");
        close(udp_socket);
        return 1;
    }

    close(udp_socket);
    return 0;
}

Client

int main()
{       
    int udp_socket = socket(AF_INET, SOCK_DGRAM, 0);
    if (udp_socket < 0)
    {
        perror("Socket");
        return 1;
    }

    struct sockaddr_in adres;
    memset(&adres, 0, sizeof(adres));
    adres.sin_family = AF_INET;
    adres.sin_addr.s_addr = INADDR_LOOPBACK; // or a real IP, ie from inet_addr()
    adres.sin_port = htons(2001);

    char msg[50];
    strcpy(msg, "Sending to server!");

    int sendx = sendto(udp_socket, msg, strlen(msg), 0, (struct sockaddr*) &adres, sizeof(adres));
    if (sendx < 0)
    {
        perror("Send");
        close(udp_socket);
        return 1;
    }

    char rcvmsg[50];
    socklen_t adres_len = sizeof(adres);

    int rcv = recvfrom(udp_socket, rcvmsg, sizeof(rcvmsg), 0, (struct sockaddr*) &adres, &adres_len);
    if (rcv < 0)
    {
        perror("Recv");
        close(udp_socket);
        return 1;
    }

    printf("Received: %.*s\n", (int)rcv, rcvmsg);

    close(udp_socket);
    return 0;
}

Upvotes: 1

Related Questions