countofmontecristo
countofmontecristo

Reputation: 381

I can't figure out how to enter the recvfrom parameters in this UDP daytime client

I'm making a simple UDP daytime client. I'm getting this when I try to compile. I've tried casting these to see if they would work but that doesn't lead anywhere. How should I go about entering these parameters with what I have (or am I missing something that should be put there)?

UDPday2.cpp:57:86: error: invalid conversion from ‘int*’ to ‘socklen_t* {aka unsigned int*}’ [-fpermissive]
     n = recvfrom(sockfd, buf,(int) sizeof(buf), 0, (sockaddr*)&serveraddr, &serverlen);
                                                                                      ^
In file included from UDPday2.cpp:6:0:
/usr/include/i386-linux-gnu/sys/socket.h:174:16: error:   initializing argument 6 of ‘ssize_t recvfrom(int, void*, size_t, int, sockaddr*, socklen_t*)’ [-fpermissive]
 extern ssize_t recvfrom (int __fd, void *__restrict __buf, size_t __n,



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

#define BUFSIZE 1024


void error(char *msg) {
    perror(msg);
    exit(0);
}

int main(int argc, char **argv) {
    int sockfd, portno, n;
    int serverlen;
    struct sockaddr_in serveraddr;
    struct hostent *server;
    char *hostname;
    char buf[BUFSIZE];

        portno = 13;

    if(argc == 1)
        hostname = "localhost";
    else
        hostname = argv[1];

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0)
        error("socket");
      server = gethostbyname(hostname);
    if (server == NULL) {
        fprintf(stderr," no host %s\n", hostname);
        exit(0);
    }

    bzero((char *) &serveraddr, sizeof(serveraddr));
    serveraddr.sin_family = AF_INET;
    bcopy((char *)server->h_addr,
          (char *)&serveraddr.sin_addr.s_addr, server->h_length);
    serveraddr.sin_port = htons(portno);

    serverlen = sizeof(serveraddr);
    n = sendto(sockfd, buf,(int)strlen(buf)+1, 0,(sockaddr*)&serveraddr , serverlen);
    if (n < 0)
      error("ERROR send");

    n = recvfrom(sockfd, buf,(int) sizeof(buf), 0, (sockaddr*)&serveraddr, &serverlen);
    if (n < 0)
      error("ERROR  recvfrom");
    printf(" %s", buf);
    return 0;
}

Upvotes: 0

Views: 1116

Answers (1)

user207421
user207421

Reputation: 310911

serverlen needs to be declared as unsigned, or as a socklen_t. You should have been able to deduce that from the error message.

Upvotes: 1

Related Questions