2

I am having this problem with UDP client/server code. This code was copied from https://www.geeksforgeeks.org/udp-server-client-implementation-c/ and modified minimally to fix compilation errors and print errno in the server code.

// 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> 

#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; socklen_t len;

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

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

    close(sockfd); 
    return 0; 
} 

// Server 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 <errno.h>

#define PORT     8080 
#define MAXLINE 1024 

// Driver code 
int main() { 
    int sockfd; 
    char buffer[MAXLINE]; 

    struct sockaddr_in servaddr, cliaddr; 

    // 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)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 

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

    // Bind the socket with the server address 
    if ( bind(sockfd, (const struct sockaddr *)&servaddr, 
            sizeof(servaddr)) < 0 ) 
    { 
        perror("bind failed"); 
        exit(EXIT_FAILURE); 
    } 

    int n; socklen_t len;
    n = recvfrom(sockfd, (char *)buffer, MAXLINE, 
                MSG_WAITALL, ( struct sockaddr *) &cliaddr, 
                &len); 
    buffer[n] = '\0'; 
    printf("Client : %s\n", buffer); 
    int str_len = 0;
    sprintf(buffer, "%s", "hello from server");
    str_len = strlen(buffer);
    printf("Sendinger buffer: <%s>, size=%d\n", buffer, (int) (str_len+1));
    n = sendto(sockfd,buffer, (int)(str_len+1), 
        MSG_CONFIRM, (const struct sockaddr *) &cliaddr, 
            len); 

    printf("n=%d, errno=%d\n", n, errno);

    printf("Hello message sent.\n"); 

    return 0; 
} 

I compiled the server and client with straight-forward commands:

g++ udpserv.cpp -o udpserv
g++ client.cpp -o client

What I am seeing is that client succesfully sends the message to the server, server prints the message from client, and when the server sends the reply back to client it gets -1 from sendto and errno is 22. Client remains hung waiting for the reply message.

Here is the output from server:

[user@localhost udpserver]$ ./udpserv 
Client : Hello from client
Sendinger buffer: <hello from server>, size=18
n=-1, errno=22
Hello message sent.
[user@localhost udpserver]$ 

And here is the output from client:

[user@localhost client]$./client 
Hello message sent.
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
feeling_lonely
  • 6,665
  • 4
  • 27
  • 53

1 Answers1

4

Error 22 is EINVAL, which means you pass an invalid argument.

The invalid argument in this case could be the uninitialized len argument: You need to initialize it to sizeof cliaddr before you call recvfrom.

It could also be the MSG_CONFIRM flag you pass to sendto, which makes no sense in your application. Pass a zero flag when sending.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621