#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    26
    Rep Power
    0

    Explain gethostname() in WinSock


    Alright, I was reading beej's tutorial for WinSock, and I got to the part where he tells us about the gethost name fuctioin. He gives us a sample code:
    int main(int argc, char *argv[])
    {
    struct hostent *h;

    if (argc != 2) { // error check the command line
    fprintf(stderr,"usage: getip address\n");
    exit(1);
    }

    if ((h=gethostbyname(argv[1])) == NULL) { // get the host info
    herror("gethostbyname");
    exit(1);
    }

    printf("Host name : %s\n", h->h_name);
    printf("IP Address : %s\n", inet_ntoa(*((struct in_addr *)h->h_addr)));

    return 0;
    }

    But what does are argc and argv[1]? And why do you need those two parameters in int main()?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    These are accepting parameters that are passed to the main function.
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    This applies to C programs both under UNIX/Linux and under Windows/DOS: the command-line arguments are passed to the program through the main() function arguments argc and argv[] (terminology police, please go easy on me here; I haven't had my morning coffee yet).

    char *argv[] is an array of character strings. Basically, the command is tokenized (split up) using white space as the delimiter and each token is placed in argv[] ("arg vector") in the order that they occur. int argc ("arg count") is the number of entries in argv[]. The entries are:
    argv[0] -- the name of the program
    argv[1] -- the first argument
    argv[2] -- the second argument
    etc.

    Since you write the program, you define what kind of data is expected in each argument. For example, in my earlier experiments in developing a broadcast server, I gave it this syntax:
    bctimed <IP Address> <Port> <Send String>
    So this command:
    bctimed 192.168.5.240 7 "This is a test"
    results in these values:
    argc = 4
    argv[0] = "bctimed"
    argv[1] = "192.168.5.240"
    argv[2] = "7"
    argv[3] = "This is a test"

    One of the implications of this is that if you pass the program a numeric argument, it will receive it as a string, so you will need to convert it to numeric, as with the function atoi().

    And, of course, if your program does not accept arguments from the command line, then you can always declare the main() function as:
    int main(void)

    Hope that explains.

    PS
    Options, such as "-b" , are also passed in to the program in this manner. There is a great C function, getopt, that is very useful in extracting options, option variables (eg, "-o prog" as in gcc -o prog main.c), and the arguments as described above. So far, I have not seen it in any Windows/DOS C compiler I've used and it was missing from the MinGW port of gcc that I use (even though the header file was there). If you're going to be doing much with command-line options and parameters, you might want to look into it. I found a source listing online and added it to my MinGW library.
    Last edited by dwise1_aol; July 10th, 2003 at 09:49 AM.
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    26
    Rep Power
    0
    Thanks.... I'm sure if I read that serveral times (and get Dr Pepper into me quickly) that I'll understand it ^_^

IMN logo majestic logo threadwatch logo seochat tools logo