Thread: UDP talker problem executing

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    24

    UDP talker problem executing

    Hello everyone,
    I am trying to program an UDP talker using sockets.
    My program compiles with no problems but when I try to execute it with any volume number, I am getting always the error : sendto : message too long
    and I really don't know why !!
    Here is the code of my UDP talker:
    Code:
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    #define BUFSIZE 100000
    
    int main(int argc, char **argv) {
      int sfd, s, val, volume;
      struct addrinfo hints;
      struct addrinfo *result, *rp;
      struct sockaddr *sa;
      socklen_t salen;
      char buf[BUFSIZE];
      ssize_t nsend;
    
      if (argc != 4) {
        printf("Usage: %s service_adress server_port volume\n", argv[0]);
        exit(EXIT_FAILURE);
      }
    
      volume = atoi(argv[3]);
      if (volume == 0 || volume > BUFSIZE) {
        fprintf(stderr, "give a volume less then %d bytes\n", BUFSIZE);
        exit(EXIT_FAILURE);
      }
    
      /* buffer initializing */
      memset(buf, 'a', volume);
    
      memset(&hints, 0, sizeof(struct addrinfo));
      hints.ai_family = AF_UNSPEC;          /* IPv4 ou IPv6 */
      hints.ai_socktype = SOCK_DGRAM;       /* Datagram socket */
      hints.ai_flags = 0;
      hints.ai_protocol = 0;                /* Any protocol */
    
      s = getaddrinfo(argv[1], argv[2], &hints, &result);
      if (s != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
        exit(EXIT_FAILURE);
      }
    
      for (rp = result; rp != NULL; rp = rp->ai_next) {
        /* open socket */
        sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
        if (sfd >= 0)
          break;
      }
      if (rp == NULL) {     /* no valid adress */
        fprintf(stderr, "Unable to open a socket to %s\n", argv[1]);
        perror("socket");
        exit(EXIT_FAILURE);
      }
    
      /*
       * Construction of the remote address structure
       */
      sa=malloc(rp->ai_addrlen);
      memcpy(sa, rp->ai_addr, rp->ai_addrlen);
      salen=rp->ai_addrlen;
    
      freeaddrinfo(result); /* Not needed anymore */
    
      /* Allow broadcast (IPv4 only) */
      val = 1;
      if (setsockopt(sfd, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val)) < 0) {
        perror("setsockopt");
        exit(EXIT_FAILURE);
      }
    
      /* send data */
      nsend = sendto(sfd, &buf, BUFSIZE, 0, (struct sockaddr *)&sa, salen);
      if (nsend < 0)
        perror("sendto");
    
      exit(EXIT_SUCCESS);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    > #define BUFSIZE 100000
    You can't just invent a size and hope to send it over UDP without a care in the world.
    networking - How to find the largest UDP packet I can send without fragmenting? - Stack Overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    yes that is right thank you ! I reduced the BUFSIZE to 10.000 bytes only , it is quite enough for me.

    However, I am having a new error message sendto: Address family not supported by protocol

    Actually in my UDP listener, I am forcing to have an IPv6 address, does this cause the problem ?

  4. #4
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    Can anyone help me solve the error sendto: Address family not supported by protocol please ?

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    3,883
    Code:
    struct sockaddr *sa;
    Code:
    (struct sockaddr *)&sa
    Why are you casting sa?
    Why are you casting an address of a pointer?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Apr 2020
    Posts
    24
    Quote Originally Posted by stahta01 View Post
    Code:
    struct sockaddr *sa;
    Code:
    (struct sockaddr *)&sa
    Why are you casting sa?
    Why are you casting an address of a pointer?

    Tim S.
    That is right !! that was the origin of the error thanks !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Executing Programs
    By stdq in forum C Programming
    Replies: 3
    Last Post: 01-05-2013, 01:52 AM
  2. compiling and executing file problem
    By ilikeapplepie in forum C Programming
    Replies: 7
    Last Post: 04-03-2011, 10:05 PM
  3. Please Help Me! Executing Problem
    By hattourie in forum C Programming
    Replies: 1
    Last Post: 11-05-2007, 04:04 AM
  4. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  5. Problem executing C program in Visual C++ 6.0
    By ladyscarlet99 in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2005, 05:48 AM

Tags for this Thread