Thread: BSD Sockets: Finding a clients IP

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    5

    BSD Sockets: Finding a clients IP

    Well, as outlined in another thread I have coded something similar to a SSH server.

    Now I would like to be able to print the clients IP address to log files. How would I retreive the IP?

    I would think its located in the struct passed to accept(). But where?

    Thanks!
    Moddy

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Shouldn't you have posted this in the Network Forum?
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Yes. Moved.

    Here's an extract from Beej's Guide To Network Programming you may find useful.

    4.11. DNS–You say "whitehouse.gov", I say "198.137.240.92"

    In case you don’t know what DNS is, it stands for "Domain Name Service". In a nutshell, you tell it what the human-readable address is for a site, and it’ll give you the IP address (so you can use it with bind(), connect(), sendto(), or whatever you need it for.) This way, when someone enters:

    $ telnet whitehouse.gov

    telnet can find out that it needs to connect() to "198.137.240.92". But how does it work? You’ll be using the function gethostbyname():
    Code:
    #include <netdb.h>
    struct hostent *gethostbyname(const char *name);
    As you see, it returns a pointer to a struct hostent, the layout of which is as follows:
    Code:
    struct hostent {
    char *h_name;
    char **h_aliases;
    int h_addrtype;
    int h_length;
    char **h_addr_list;
    };
    #define h_addr h_addr_list[0]
    And here are the descriptions of the fields in the struct hostent:
    • h_name – Official name of the host.
    • h_aliases – A NULL-terminated array of alternate names for the host.
    • h_addrtype – The type of address being returned; usually AF_INET.
    • h_length – The length of the address in bytes.
    • h_addr_list – A zero-terminated array of network addresses for the host. Host addresses are in Network Byte
    Order.
    • h_addr – The first address in h_addr_list.[/quote]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IP Sockets Program in c++ for University Project
    By saqureshi in forum C++ Programming
    Replies: 3
    Last Post: 08-06-2008, 05:21 AM
  2. TCP Sockets: multiple clients - one server
    By Printisor in forum C Programming
    Replies: 4
    Last Post: 11-01-2007, 10:34 AM
  3. finding out ip address?
    By chiatello in forum Linux Programming
    Replies: 3
    Last Post: 08-07-2003, 06:18 AM
  4. Socket Project (getting one of multiple clients IP)
    By Cl0wn in forum C Programming
    Replies: 2
    Last Post: 04-05-2003, 12:01 PM