-1

Im just trying to figure out my error, yes I have tried googling it. Any help would be appreciated. I am just trying to make a C++ program of a server and a client that communicate with each other using UDP.

#include <iostream>
#include <stdio.h>
#include <strings.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include<netinet/in.h>
#include "m-c2.h"

#define PORT 8080
#define MAXLINE 1000

int main()
{   
    char buffer[100];
    char *message = "Hello Client";
    int listenfd, len;
    struct sockaddr_in servaddr, cliaddr;
    bzero(&servaddr, sizeof(servaddr));
  
    // Create a UDP Socket
    listenfd = socket(AF_INET, SOCK_DGRAM, 0);        
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(PORT);
    servaddr.sin_family = AF_INET; 
   
    // bind server address to socket descriptor
    bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
       
    //receive the datagram
    len = sizeof(cliaddr);
    int n = recvfrom(listenfd, buffer, sizeof(buffer),
            0, (struct sockaddr*)&cliaddr,&len); //receive message from server
    buffer[n] = '\0';
    puts(buffer);
           
    // send the response
    sendto(listenfd, message, MAXLINE, 0,
          (struct sockaddr*)&cliaddr, sizeof(cliaddr));
}

I tried googling, and that didn't help.

syphaeric
  • 1
  • 1
  • 2
    Does this answer your question? [Socket Programming Pointer Error](https://stackoverflow.com/questions/28550748/socket-programming-pointer-error) – Etienne de Martel Nov 04 '22 at 03:21

1 Answers1

2

The last parameter of recvfrom() expects a pointer to a socklen_t, but you are passing it a pointer to an int instead. They are not the same type.

You just need to fix your declaration of len accordingly, eg change this:

int listenfd, len;

To this instead:

int listenfd;
socklen_t len; // <--
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770