We help IT Professionals succeed at work.

How to retrieve hostname where application is running using C programming language?

jatinps
jatinps asked
on
I want to retrieve the hostname where my application is running to put into log file.

How can I do it in 'C'? Any library/function ??
Comment
Watch Question

you can use gethostname() under both windows and unix.

under windows:

gethostname()
Description
Return the standard host name for the local machine.

#include <winsock.h>

int PASCAL FAR gethostname ( char FAR * name, int namelen );

name
A pointer to a buffer that will receive the host name. namelen
The length of the buffer.
Remarks
This routine returns the name of the local host into the buffer specified by the name parameter. The host name is returned as a null-terminated string. The form of the host name is dependent on the Windows Sockets implementation--it may be a simple host name, or it may be a fully qualified domain name. However, it is guaranteed that the name returned will be successfully parsed by gethostbyname() and WSAAsyncGetHostByName().

Return ValueIf no error occurs, gethostname() reutrns 0, otherwise it returns SOCKET_ERROR and a specific error code may be retrieved by calling WSAGetLastError().

Error Codes

WSAEFAULT
The namelen parameter is too small WSAENOTINITIALISED
A successful WSAStartup() must occur before using this API. WSAENETDOWN
The Windows Sockets implementation has detected that the network subsystem has failed. WSAEINPROGRESS
A blocking Windows Sockets operation is in progress.


Under unix:

man 3 gethostname

Hope this helps.



delete.The.Duplicate
Shell to command prompt and place output of SET command into file named: Set.Log

This file can be input as text, and using pattern matching, values can be extruded, retrieved, and placed in variables according to format desired. Then output NewString to file named: HostName.Log

Commented:

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
 - PAQ'd and pts removed
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

Nic;o)
Commented:
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange

Explore More ContentExplore courses, solutions, and other research materials related to this topic.