4

I wanted to compile sample code for a UDP client on a Mac OS X, but got a following error:

client.c:91:9: error: use of undeclared identifier 'MSG_CONFIRM'
        MSG_CONFIRM, (const struct sockaddr *) &servaddr,
        ^

I googled for the error for several hours but couldn't find anything related to this problem. Does anybody know what is the issue and how to fix it?

The code for UDP client is below

// Client side implementation of UDP client-server model
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>

#define PORT     8080
#define MAXLINE 1024

// Driver code
int main() {
    int sockfd;
    char buffer[MAXLINE];
    char *hello = "Hello from client";
    struct sockaddr_in     servaddr;

    // Creating socket file descriptor
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
        perror("socket creation failed");
        exit(EXIT_FAILURE);
    }

    memset(&servaddr, 0, sizeof(servaddr));

    // Filling server information
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(PORT);
    servaddr.sin_addr.s_addr = INADDR_ANY;

    int n, len;

    sendto(sockfd, (const char *)hello, strlen(hello),
        MSG_CONFIRM, (const struct sockaddr *) &servaddr,
            sizeof(servaddr));
    printf("Hello message sent.\n");

    while (1)
    {
        n = recvfrom(sockfd, (char *)buffer, MAXLINE,
                MSG_WAITALL, (struct sockaddr *) &servaddr,
                &len);
        buffer[n] = '\0';
        printf("Server : %s\n", buffer);
    }

    close(sockfd);
    return 0;
}
flashburn
  • 4,180
  • 7
  • 54
  • 109
  • 2
    Clearly your socket SDK does not define `MSG_CONFIRM` for the platform you are compiling for. So either define it yourself, or don't use it at all (you shouldn't be using it in your example anyway: see [Why should I use, or not use, MSG_CONFIRM?](https://stackoverflow.com/questions/16594387/)). – Remy Lebeau Jun 08 '18 at 21:20
  • I think you got the code from https://www.geeksforgeeks.org/udp-server-client-implementation-c/. I also tested in on my Mac OSX. Mac OS doesn't define MSG_CONFIRM. I set it 0. The code compiles and runs but the client doesn't receive the message sent from the server. I ran the code on a linux box and both worked fine. – yoshi Mar 20 '19 at 11:25
  • 5
    There's an error in that code on geeksforgeeks, you need to initialize the `len` variable used in `recvfrom` on the server side to `sizeof cli_addr`. Otherwise the client host and port in that struct will be 0. I got it working on MacOS after making that correction without using the `MSG` flags. https://stackoverflow.com/questions/23472533/recvfrom-not-filling-the-from-ip-address-even-for-udp-messages-in-first-call – gary69 Jun 29 '19 at 02:53

1 Answers1

-3

My friend suggest me to define MSG_CONFIRM as 0. He said search what is use for it. You also research it.

#define MSG_CONFIRM 0
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197