Friday, August 26, 2011

Using socket gethostname(2) function

The gethostname(2) function can be used to determine your current hostname. Its function synopsis is given as follows:

#include <unistd.h>
int gethostname(char *name, size_t len);

This function takes two arguments:
  • The receiving buffer name, which must be len bytes in length or longer.
  • The maximum length (len) of the receiving buffer name in bytes.

The return value is the value zero if it is successful. A value of -1 is returned if an error occurs. The error is described by the external variable errno.

TIP
The len argument of gethostname(2) must include the total length of the hostname to be returned and the terminating null byte.


Example:
Using gethostname function sample C code

No comments:

Post a Comment