13.07.2015 Views

Beej's Guide to Network Programming Using Internet Sockets

Beej's Guide to Network Programming Using Internet Sockets

Beej's Guide to Network Programming Using Internet Sockets

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Beej’s <strong>Guide</strong> <strong>to</strong> <strong>Network</strong> <strong>Programming</strong> <strong>Using</strong> <strong>Internet</strong> <strong>Sockets</strong> 468.5. gethostname()Returns the name of the systemPro<strong>to</strong>types#include int gethostname(char *name, size_t len);DescriptionYour system has a name. They all do. This is a slightly more Unixy thing than the rest of thenetworky stuff we’ve been talking about, but it still has its uses.For instance, you can get your host name, and then call gethostbyname() <strong>to</strong> find out your IPaddress.The parameter name should point <strong>to</strong> a buffer that will hold the host name, and len is the sizeof that buffer in bytes. gethostname() won’t overwrite the end of the buffer (it might return anerror, or it might just s<strong>to</strong>p writing), and it will NUL-terminate the string if there’s room for it in thebuffer.Return ValueReturns zero on success, or -1 on error (and errno will be set accordingly.)Examplechar hostname[128];gethostname(hostname, sizeof(hostname));printf("My hostname: %s\n", hostname);See Alsogethostbyname()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!