Find the answer to your Linux question:
Results 1 to 7 of 7
Hello! I am a newbie in socket programming, and I wanted to create a client-serve model using sockets in C language. Server is my PC and client is my laptop. ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12

    Question about sockets in client-server model


    Hello!

    I am a newbie in socket programming, and I wanted to create a client-serve model using sockets in C language. Server is my PC and client is my laptop.

    I found some example code on the net, but there's something I dont understand and would like to ask.

    The code i found is available in Linux Howtos: C/C++ -> Sockets Tutorial when I searched it on the net..

    Now my question is, when the client says

    Code:
     server = gethostbyname(argv[1]);
    How/where does it find my PC and its hostname? In the desription of this function it says: "The gethostbyname() function searches the database and finds an entry which matches the specified host name, opening a connection to the database if necessary. "

    My question is what/where is this database? Is it setup when PC starts using the connection? How can the client query if a server with my PC's hostname exists? Can you plz explain this simply??

    PS. can you also explain
    Code:
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    Isnt s_addr the IP address of the server? But it writes on net that "anyone from any network can connect, using any IP address bound to the PC" by using this macro, then is this not the IP address of the server in reality? Can you plz clarify this also?

    Thanks!

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    664
    Quote Originally Posted by ccb85 View Post
    Hello!

    I am a newbie in socket programming, and I wanted to create a client-serve model using sockets in C language. Server is my PC and client is my laptop.

    I found some example code on the net, but there's something I dont understand and would like to ask.

    The code i found is available in Linux Howtos: C/C++ -> Sockets Tutorial when I searched it on the net..

    Now my question is, when the client says

    Code:
     server = gethostbyname(argv[1]);

    How/where does it find my PC and its hostname? In the description of this function it says: "The gethostbyname() function searches the database and finds an entry which matches the specified host name, opening a connection to the database if necessary. "
    If you look the example in more detail, you sill see that the function "main" has two arguments and "integer" (most likely called argc) and a "char**" or "char* []" called argv.

    The "argv[1]" woud reverence the first argument passed when you run the program.

    The "database" could be a DNS search or the file "/etc/hosts" where IP address and host names are mapped.
    My question is what/where is this database? Is it setup when PC starts using the connection? How can the client query if a server with my PC's hostname exists? Can you plz explain this simply??

    PS. can you also explain
    Code:
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    INADDR_ANY allows your program to work without knowing the IP address of the machine it was running on, or, in the case of a machine with multiple network interfaces, it allows your server to receive packets destined to any of the interfaces.
    Isnt s_addr the IP address of the server? But it writes on net that "anyone from any network can connect, using any IP address bound to the PC" by using this macro, then is this not the IP address of the server in reality? Can you plz clarify this also?

    Thanks!

  3. #3
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12
    Hi!

    I know that i give the host name in the client program with argv[1], but what I still dont understand is when i say "./client PC 2000" for example, how does it find my PC? I checked the etc/hosts file in the laptop, it doesnt say anything about my PC, it only has its own hostname.. when i use gethostbyname, does it search through the socket or something?

    Regarding INADDR_ANY, can I write the IP address of the machine for example instead?

    Thanks..

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    664
    Are you getting the address of your PC2000 assigned or is it a static IP address. Is there a DNS server that provides the name lookup service on your network? Well in any case, the "/etc/hosts" file can be edited (as root) to include a static address for your server by having an entry like:
    Code:
    10.69.69.111      PC2000
    Where "10.69.59111" is replaced with the IP address that is assigned to the machine known as PC2000. If you are not having problems finding the PC2000 machine, then I would say that you have a DNS server in use. The DNS server (in the Unix/Linux world is a package called bind or bind9 which is derived from the first one written at UofCA, Berekely back in the early 70's.) There is lots of online material on bind.

    To find out what is being used is to look at the file "/etc/resolv.conf" it may look like:
    Code:
    domain friends-of-raptors.nom
    search friends-of-raptors.nom
    nameserver 10.69.69.2
    nameserver 10.69.70.2
    Which tell where the DNS servers are (always an IP adress) located.

    As far as using INADDR_ANY, it is used so the compiled code can be moved to another machine without having to have the code and edit the IP address and re-building the application/service/daemon.

  6. #5
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12
    Hey! PC is the hostname, 2000 is the portname that the socket will use to bind
    Are you meaning /etc/hosts in the client machine(laptop)? Or server (PC)?
    If its a static IP, then where does the laptop search for my PC? Can you clarify that? In case of DHCP i think the laptop will look up the DNS server, wont it?

  7. #6
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    664
    The Unix/linux client needs to be able to turn an "name" into an IP address. This is ether done by a DNS server or your "/etc/hosts" file. There is a file called '/etc/nsswitch.conf" which would control the search order as well. It would have an entry like:
    Code:
    hosts:       files dns
    The example says when looking for a hostname (because of the entry name hosts) first look at files on the local machine, then it not found attempt a DNS lookup.

  8. #7
    Just Joined!
    Join Date
    Nov 2011
    Posts
    12
    Hi alf55!

    I think I kindof understood, the client looks for the DNS server and checks if the server's hostname exists there and gets the IP address of the server from there.
    But if there is no DNS server and if the server has a static IP, then where does the client look for the server's hostname? Since there will be no DNS translation possible? Thx.
    Last edited by ccb85; 01-09-2012 at 10:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •