Thread: gethostbyname and structs

  1. #16
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    I guess I was trying to write a client of some kind. Do you never need to use bind when you create a client?

    As long as the port is open, I can get a response from the host? How about if you try to connect to a service, lets say ssh (port 22), how would you go about do that? Do you need to do something specific to get that working?

  2. #17
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    I should mention that my app fails on connect(). Usig herror() it says "Resolver Error 0 (no error)" and with perror() it says "Operation timed out". Does this indicate a server or client/code problem?

  3. #18
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	address.sin_addr.s_addr = *ip; /* getting the IP into the struct */ /* WRONG ??? */
    You correctly diagnosed the problem. This needs an ip address in numeric format, not a string format.
    Use:
    Code:
    memcpy(&address.sin_addr, host->h_addr_list[0], sizeof(address.sin_addr));
    This will copy the struct in_addr returned in the hostent structure to the struct in_addr in the sockaddr_in structure.

  4. #19
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by kristy
    Do you never need to use bind when you create a client?
    as you problably know, the OS has a specific range of local ports it can allocate for outgoing connections. so, if you decide not to bind your program's outgoing port, the OS will allocate a random port for you.

    Quote Originally Posted by kristy
    As long as the port is open, I can get a response from the host? How about if you try to connect to a service, lets say ssh (port 22), how would you go about do that? Do you need to do something specific to get that working?
    you can define a "service" as a program that listens on a given port (a server), in this case, port 22 (tcp).
    now all you need is a client program to connect that server program through the listening (remote) port. you don't really need to care about your client's local port, so just forget about it (know it's there, but don't mind it).

    Quote Originally Posted by anonytmouse

    Code:
    memcpy(&address.sin_addr, host->h_addr_list[0], sizeof(address.sin_addr));

    This will copy the struct in_addr returned in the hostent structure to the struct in_addr in the sockaddr_in structure.
    memcpy is all nice and good, but why use a function?
    simply do:
    Code:
    address.sin_addr = *((struct in_addr *) host->h_addr;   // #define h_addr h_addr_list[0]
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  5. #20
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    I just wanted to say thanks for all the help! I just finished writing my first app that actually works, using sockets. anonytmouse, after you pointed out what was wrong it started working much better at once. I also used perror instead of herror, wherever possible, and the error messages started to make sense.

    Now, I have to figure out how to be able to communicate with the server. What I want to do is to write a very think SSH client. And, of course, try to make proper sense of all the structs..

  6. #21
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I don't know if an SSH client is really what you want to start with...
    the SSH protocol itself is pretty complex! start simple, like the TELNET protocol.

    goodluck.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #22
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Hm, yeah, maybe telnet is better, though telnet isn't very secure. But on a LAN I guess it's no problem.

    Btw, this is a bit off-topic really; I tried to find the available values for the exit() function, but I didn't find them. Where are they defined? I only found errno (in /usr/include/sys/errno.h).

  8. #23
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    If I'm not mistaken, exit doesn't have any defined values...
    the input value to this function is a value to be returned from the program... for example: exit(1);
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  9. #24
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    Yeah, that was what I ment, I just didn't explain it well (English is not my native language). Usually I use either 0 or -1, but I've heard it's better to use the predefined macros, like EXIT_FAILURE instead, since they're more 'porting friendly'.

  10. #25
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    In general, you would be right, but in the exit case, there is nothing more portable than 0 and 1 it's very basic, that what makes this so portable.

    personally, i don't really use exit() too often; i prefer "return" my way out


    btw, I don't know what is your native language, but engish neither is mine
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  11. #26
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    I use exit all the time! If the predefined macros really are easier to port apps with, I don't know. I just read it, and I wanted to find out what macro that was defined to what value.

    Oh well, now I have to go nuts not knowing..

  12. #27
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    if you really want, here is exit as it described by msdn:

    The exit function, declared in the standard include file STDLIB.H, terminates a C++ program.

    The value supplied as an argument to exit is returned to the operating system as the program’s return code or exit code. By convention, a return code of zero means that the program completed successfully.

    Note You can use the constants EXIT_FAILURE and EXIT_SUCCESS, defined in STDLIB.H, to indicate success or failure of your program.

    Issuing a return statement from the main function is equivalent to calling the exit function with the return value as its argument.

    tell, are you new to c/c++ and sockets only, or to the entire programming in general...
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  13. #28
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    The UNIX man page says something like that, too (I run Mac OS X).

    I'm not new to C or programming in general. But there's a few things with C I've never gotten the hang of, and that has just become something I know I have to learn, but never found the proper motivation to do. Like network.

  14. #29
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    hmmm... true.
    well, i hope i helped alittle

    if you have any more questions you're welcome to address this forum in general, and my mail in particular.

    goodluck.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  15. #30
    UNIX chick
    Join Date
    Mar 2003
    Posts
    92
    That's generous, thanks.

Popular pages Recent additions subscribe to a feed