Getting the server name

This is a discussion on Getting the server name within the C Programming forums, part of the General Programming Boards category; How would one get the name of the server (Linux) that the C code runs on? I want to fetch ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    Getting the server name

    How would one get the name of the server (Linux) that the C code runs on? I want to fetch and display the name of the server once the program is executed.

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Do you want the server name, or the host name? They aren't necessarily the same thing.

    For the server name do man 2 uname
    For the host name do man 2 gethostname

  3. #3
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Right, thanks. I googled them and came up with the following:
    http://www.cl.cam.ac.uk/cgi-bin/manpage?2+uname

    So how would I implement this in my code? I include the sys/utsname.h library, and include the definiton int uname(struct utsname *buf); but how do I use uname in my code?

  4. #4
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    I don't have a compiler handy, but something like

    Code:
    #include <stdio.h>
    #include <sys/utsname.h>
    
    int main(void)
    {
        int rc;
        struct utsname buf;
    
        rc = uname(&buf);
    
        if (rc == 0)
        {
            printf("Server name is %s\n", buf.nodename);
        }
        else
        {
            /* Error */
        }
        return(0);
    }

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    If you are on linux, then there is no need to google the man pages.... just man them on the CLI.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Opel_Corsa
    thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.
    lol, why not just drop C altogether and start shell scripting 100%

  8. #8
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    Quote Originally Posted by Laserve
    lol, why not just drop C altogether and start shell scripting 100%
    i wasn't initially planning to use unix system calls but because i was short on time i had no option...

  9. #9
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.
    What do you mean it didn't work? It worked fine on the linux box I ran it on later.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > thanks but that didn't work. anyway, i got the function implemented using system()... problem solved.
    *shakes head*
    Oh dear oh dear oh dear
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Server Architecture
    By coder8137 in forum Networking/Device Communication
    Replies: 2
    Last Post: 01-29-2008, 10:21 PM
  2. Where's the EPIPE signal?
    By marc.andrysco in forum Networking/Device Communication
    Replies: 0
    Last Post: 12-23-2006, 07:04 PM
  3. SMTP Server Not Working
    By (TNT) in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-15-2003, 05:33 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21