Robb1
Robb1

Reputation: 5025

recvfrom unexpected error: Invalid argument

I'm writing a server-client application using UDP protocol. When I run the recvfrom I get the following error and I really can't understand why:

recvfrom: Invalid argument

Here's what I assumed is the code relative to this error:

#define CLIE_PORT 5499
#define MAXMSGSIZE (1024 * 1024) // Max message size is 1MB
#define CLIE_PORT 5499
#define PACKETSZ 7

I'm running rcv_string in this way:

char *result = malloc(MAXMSGSIZE);
struct sockaddr_in sndaddr;
rcv_string((uint16_t) CLIE_PORT, &result, &sndaddr);

Here's the implementation of rcv_string (inside there's the call to recvfrom).

int rcv_string(uint16_t port, char **return_string, struct sockaddr_in *sndaddr) {
    // [...]
    memset((char *) sndaddr, 0, sizeof(*sndaddr));
    (*sndaddr).sin_family = AF_INET;
    (*sndaddr).sin_port = htons(port);
    int reuse = 1;
    if (setsockopt(sockfd_in, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse, sizeof(reuse)) < 0) {
        perror("setsockopt");
        return -1;
    }
    if (bind(sockfd_in, (struct sockaddr *) sndaddr, sizeof(*sndaddr)) < 0) {
        perror("bind");
        return -1;
    }
    socklen_t len;
    if ((recvfrom(sockfd_in, buff, PACKETSZ + 1, 0, (struct sockaddr *) sndaddr, &len)) < 0) {
            perror("recvfrom");
            return -1;
        }
    // [...]
}

What could be the problem? (If you think I should upload more code, tell me!)

Upvotes: 0

Views: 852

Answers (1)

yogit singh
yogit singh

Reputation: 1

I think it should be socklen_t len=sizeof(*sndaddr);.

Upvotes: 0

Related Questions