I
I
istarik_std2015-12-21 08:46:12
UDP
istarik_std, 2015-12-21 08:46:12

UDP client how to send a packet to everyone?

Hello.
The following question arose. There is a home network consisting of a router (wl500, tomato firmware) and three computers (Linux) connected to it.
The router address is 192.168.5.99, computers receive addresses via dhcp (192.168.5.197, 192.168.5.224, 192.168.5.234).
UDP servers are running on computers.
What I want I want an udp client
to work on the router and send a packet to ALL computers (just a string of characters). Here is the udp client itself:

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

#define SERVERPORT "3492"    

int main(int argc, char *argv[])
{
    int sockfd;
    struct addrinfo hints, *servinfo, *p;
    int rv;
    int numbytes;
    int n=1;

    if (argc != 2) {
        exit(1);
    }

    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC; 
    hints.ai_socktype = SOCK_DGRAM;
    
    if ((rv = getaddrinfo(argv[1], SERVERPORT, &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
        return 1;
    }

    for(p = servinfo; p != NULL; p = p->ai_next) 
     {
        if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) 
          {
            continue;
          }

        break;
     }

    setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&n,sizeof(n)); 

    if (p == NULL) {
        return 2;
    }

    char str_iz_file[128] = {0,};

    while (1) 
      {
          usleep(910000);  

          FILE *f; 
          f = fopen("/tmp/file.db", "r"); // tmp
          if(f == NULL) 
            {
              continue;
            } 

           fgets(str_iz_file, 127, f);
           fclose(f); 

           if ((numbytes = sendto(sockfd, str_iz_file, strlen(str_iz_file), 0, p->ai_addr, p->ai_addrlen)) == -1) {
           exit(1);
           }

           printf("bla-bla: %s\n", str_iz_file);
           memset(str_iz_file, 0, sizeof(str_iz_file));

      }

    freeaddrinfo(servinfo);
    close(sockfd);
    return 0;
}

//gcc -W -Wall udpclient.c -o udpclient
//  make package/udpclient/compile V=s
// ./udpclient 255.255.255.255

I run it like this: ./udpclient 255.255.255.255 The
question is that if you run udpclient on one of the computers, then everything is fine, other computers receive the packet, but if you run it on the router, then no one gets anything.
Please explain what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
istarik_std, 2015-12-21
@istarik_std

Actually, the answer was found, you need to send ./udpclient 192.168.5.255 like this.
Here you can read it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question