Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-22-2006, 12:30 PM
|
#1
|
Member
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216
Rep:
|
Proper Use Of gethostname?
First, here's the function signature:
Code:
int gethostname(char *name, size_t len);
What shall we use for name and len? I've seen code that looks like:
Code:
#include <limits.h>
char hostname[HOST_NAME_MAX+1];
gethostname(hostname, sizeof(hostname));
According to the POSIX standard, HOST_NAME_MAX is a "possibly indeterminate" definition meaning that it
Quote:
...shall be omitted from <limits.h> on specific implementations where the corresponding value is equal to or greater than the stated minimum, but is unspecified.
|
HOST_NAME_MAX is not defined on my Linux box, probably because the kernel developers don't want to specify a maximum host name. I suppose I could throw in a
Code:
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
but that doesn't seem right to me. So, what length for a host name do you specify on a system that has no defined maximum host name?
|
|
|
01-22-2006, 02:27 PM
|
#2
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
try MAXHOSTNAMELEN
My copy of "Unix Network Programming" (Stevens, 2nd ed) recommends using MAXHOSTNAMELEN, defined in sys/param.h. I tried it; it seems to work OK on my current Linux:
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>
int
main (int argc, char *argv[])
{
char host[MAXHOSTNAMELEN];
gethostname (host, sizeof host);
return printf ("host: %s\n", host);
}
|
|
|
01-22-2006, 02:39 PM
|
#3
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
Oh yeah - here's a version #ifdef'ed for Windows and *nix:
Code:
#include <stdio.h>
#if defined(WIN32)
#include <windows.h>
#else
#include <unistd.h>
#include <sys/param.h>
#endif
int
main (int argc, char *argv[])
{
#if defined(WIN32)
char host[MAX_COMPUTERNAME_LENGTH+1];
int ilen = sizeof (host);
GetComputerName (host, &ilen);
#else
char host[MAXHOSTNAMELEN];
gethostname (host, sizeof host);
#endif
return printf ("host: %s\n", host);
}
|
|
|
07-17-2019, 10:06 AM
|
#4
|
LQ Newbie
Registered: Aug 2015
Location: Dallas, TX
Distribution: Debian & Ubuntu
Posts: 1
Rep: 
|
Using Raspberry Pi 3B
1) If you have a Raspberry Pi with desktop image, open Geany IDE.
2) Copy the following into a new file:
// file: hostname.cpp
#include <unistd.h>
#include <stdio.h>
#include <limits.h>
int main(int argc, char *argv[])
{
char hostname[HOST_NAME_MAX];
int status = gethostname(hostname,sizeof(hostname));
printf("HOST_NAME_MAX: %u, Hostname: "%s", status: %d\n",HOST_NAME_MAX,hostname,status);
}
3) save file to "hostname.cpp"
4) click the "Build the current file" button
5) click the "run or view the current file" button
Resulting output:
HOST_NAME_MAX: 64, Hostname: "raspberrypi.frontiernet.net", status: 0
The API works great, but I thought I had already given my device a different hostname... Something else to investigate...
|
|
|
07-17-2019, 12:58 PM
|
#5
|
Moderator
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,327
|
Welcome to LQ JMerkle!
You have posted to a thread which has been inactive for 13 years, often called necroposting and generally frowned upon.
If you have a question of your own it is best to open your own thread which will give it better exposure among currently active members.
Please review the Site FAQ for guidance in posting your questions and general forum usage.
Good luck!
|
|
|
All times are GMT -5. The time now is 02:02 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|