gethostbyname(string here)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ruroma@gmail.com

    gethostbyname(string here)

    Hello,

    I have one problem, and can think of two possible solutions but I
    can't manage to make them work. I'm open to other suggestions if you
    thik is better.

    [Brief Situation] The main function calls sendSocket, but I don't know
    how to tell it where to send the socket. ( I think I'm messing it up
    with pointers)

    struct hostent *hp;

    sendSocket(){
    hp= gethostbyname(P ROBLEM HERE);
    }

    main(){
    sendSocket();
    }

    [Possible First Solution]
    This would be probably the best approach to what I need. Ask the user
    for the hostname

    sendSocket(){
    char hostname[256];
    printf("Enter hostname: ");
    fgets ( hostname, 256, stdin );
    hp= gethostbyname(¿ WHAT SHOULD I PUT HERE?);
    }

    [Possible Second Solution]
    A way to make argc and argv available outside the main function.

    struct hostent *hp;
    sendSocket(){
    hp = gethostbyname(a rgv[1]);
    }
    main(int argc; char *argv[]){
    sendSocket();
    }

    Any suggestion? Thanks

  • Artie Gold

    #2
    [OT, redirect] Re: gethostbyname(s tring here)

    ruroma@gmail.co m wrote:[color=blue]
    > Hello,
    >
    > I have one problem, and can think of two possible solutions but I
    > can't manage to make them work. I'm open to other suggestions if you
    > thik is better.
    >
    > [Brief Situation] The main function calls sendSocket, but I don't know
    > how to tell it where to send the socket. ( I think I'm messing it up
    > with pointers)
    >
    > struct hostent *hp;
    >
    > sendSocket(){
    > hp= gethostbyname(P ROBLEM HERE);
    > }
    >
    > main(){[/color]
    int main(void) {[color=blue]
    > sendSocket();
    > }
    >[/color]

    As such things are handled in a platform specific way (they are not part
    of standard C) please ask in a forum specific to your platform.

    [And, of course, you *should* have read the FAQ before posting.]

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com (new post 8/5)

    "If you have nothing to hide, you're not trying!"

    Comment

    • TKS

      #3
      Re: gethostbyname(s tring here)

      Hi,

      Sorry. I found that the cause why the first solution didn't work was
      the '\n' being introduced in the last position before '\0'.

      Thank you Artie. I read the FAQ but didn't find anything about this,
      sorry if this wasn't the appropriate group.

      Bye

      Comment

      • Kenneth Brody

        #4
        Re: [OT, redirect] Re: gethostbyname(s tring here)

        Artie Gold wrote:[color=blue]
        >
        > ruroma@gmail.co m wrote:[color=green]
        > > Hello,
        > >
        > > I have one problem, and can think of two possible solutions but I
        > > can't manage to make them work. I'm open to other suggestions if you
        > > thik is better.
        > >
        > > [Brief Situation] The main function calls sendSocket, but I don't know
        > > how to tell it where to send the socket. ( I think I'm messing it up
        > > with pointers)[/color][/color]
        [...][color=blue]
        > As such things are handled in a platform specific way (they are not part
        > of standard C) please ask in a forum specific to your platform.
        >
        > [And, of course, you *should* have read the FAQ before posting.][/color]

        While the example is using gethostbyname() , the real question being
        asked is "how can I get a string from the user, and pass it to a
        function", which would certainly not be off-topic. The fact that
        the function is gethostbyname() and not print_foo(), is irrelevant
        in this instance.

        His followup post showed he found the correct answer -- that fgets()
        leaves the newline intact, and he had to remove it before passing the
        string to the function.


        --
        +-------------------------+--------------------+-----------------------------+
        | Kenneth J. Brody | www.hvcomputer.com | |
        | kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer .h> |
        +-------------------------+--------------------+-----------------------------+
        Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


        Comment

        • Artie Gold

          #5
          Re: [OT, redirect] Re: gethostbyname(s tring here)

          Kenneth Brody wrote:[color=blue]
          > Artie Gold wrote:
          >[color=green]
          >>ruroma@gmail. com wrote:
          >>[color=darkred]
          >>>Hello,
          >>>
          >>> I have one problem, and can think of two possible solutions but I
          >>>can't manage to make them work. I'm open to other suggestions if you
          >>>thik is better.
          >>>
          >>>[Brief Situation] The main function calls sendSocket, but I don't know
          >>>how to tell it where to send the socket. ( I think I'm messing it up
          >>>with pointers)[/color][/color]
          >
          > [...]
          >[color=green]
          >>As such things are handled in a platform specific way (they are not part
          >>of standard C) please ask in a forum specific to your platform.
          >>
          >>[And, of course, you *should* have read the FAQ before posting.][/color]
          >
          >
          > While the example is using gethostbyname() , the real question being
          > asked is "how can I get a string from the user, and pass it to a
          > function", which would certainly not be off-topic. The fact that
          > the function is gethostbyname() and not print_foo(), is irrelevant
          > in this instance.
          >
          > His followup post showed he found the correct answer -- that fgets()
          > leaves the newline intact, and he had to remove it before passing the
          > string to the function.
          >
          >[/color]
          Point taken.

          However, even in a case like that, it behooves the OP to provide a
          visible prototype for any non-standard function (yes I know what
          gethostbyname() does -- but not when I'm wearing this hat ;-)).

          Cheers,
          --ag

          --
          Artie Gold -- Austin, Texas
          http://goldsays.blogspot.com (new post 8/5)

          "If you have nothing to hide, you're not trying!"

          Comment

          • SM Ryan

            #6
            Re: [OT, redirect] Re: gethostbyname(s tring here)

            Artie Gold <artiegold@aust in.rr.com> wrote:

            # However, even in a case like that, it behooves the OP to provide a
            # visible prototype for any non-standard function (yes I know what
            # gethostbyname() does -- but not when I'm wearing this hat ;-)).

            When you find yourself in a hole, stop digging.

            --
            SM Ryan http://www.rawbw.com/~wyrmwif/
            What kind of convenience store do you run here?

            Comment

            Working...