Click here to Skip to main content
16,005,467 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionHow to get Windows Installed Drive? Pin
Sarvan AL10-Oct-06 2:01
Sarvan AL10-Oct-06 2:01 
AnswerRe: How to get Windows Installed Drive? Pin
Rajesh R Subramanian10-Oct-06 2:08
professionalRajesh R Subramanian10-Oct-06 2:08 
GeneralRe: How to get Windows Installed Drive? Pin
Hamid_RT10-Oct-06 2:53
Hamid_RT10-Oct-06 2:53 
QuestionSystem Name & IP using gcc in Linux Pin
Andy Rama10-Oct-06 1:48
Andy Rama10-Oct-06 1:48 
AnswerRe: System Name & IP using gcc in Linux Pin
Galatei10-Oct-06 3:33
Galatei10-Oct-06 3:33 
GeneralRe: System Name & IP using gcc in Linux Pin
Andy Rama11-Oct-06 1:12
Andy Rama11-Oct-06 1:12 
GeneralRe: System Name & IP using gcc in Linux Pin
markkuk11-Oct-06 4:08
markkuk11-Oct-06 4:08 
GeneralRe: System Name & IP using gcc in Linux Pin
Galatei11-Oct-06 4:13
Galatei11-Oct-06 4:13 
Aniket Salunkhe wrote:
name ("mylinux")?

That was just an example.

To get the name of your system, simply call getenv("HOSTNAME") which will give you full name of your system in return.
example:
printf("HOSTNAME: %s\n", getenv("HOSTNAME"));


In order to read address of particular interface (eg. eth0), you may do something like this:
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int getifaddr(char *pifname, struct sockaddr_in* psai)
{
    int s;		// socket descriptor
    struct ifreq ifr;	// interface request structure, for SIOCGIFADDR
    struct sockaddr_in *saitmp = (struct sockaddr_in*)&ifr.ifr_addr;
    // open socket
    if (!pifname || !psai)
	return -1;
    s = socket(AF_INET, SOCK_STREAM, 0);
    if (s < 0)
	return -2;	// socket creation error
    memset(&ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, pifname);
    saitmp->sin_family = AF_INET;
    if (ioctl(s, SIOCGIFADDR, &ifr) != 0)
	return -3;
    memcpy(psai, saitmp, sizeof(struct sockaddr_in));
    return 0;
}

then use inet_ntoa function to get address string.

example:
#include <stdio.h>
#include <string.h>

int main(void)
{
    struct sockaddr_in sai;
    if (!getifaddr("eth0", &sai))
        printf("IP: %s\n", inet_ntoa(sai.sin_addr));
    else
	perror("getifaddr error!\n");
}


Hope that helps.

Regards
GeneralRe: System Name & IP using gcc in Linux Pin
Andy Rama15-Oct-06 21:13
Andy Rama15-Oct-06 21:13 
QuestionSelected Item in list control Pin
zon_cpp10-Oct-06 1:43
zon_cpp10-Oct-06 1:43 
QuestionRe: Selected Item in list control Pin
David Crow10-Oct-06 4:03
David Crow10-Oct-06 4:03 
AnswerRe: Selected Item in list control Pin
zon_cpp10-Oct-06 19:25
zon_cpp10-Oct-06 19:25 
AnswerRe: Selected Item in list control Pin
zon_cpp10-Oct-06 19:51
zon_cpp10-Oct-06 19:51 
Questionprogram transfer error Pin
mt_samiei10-Oct-06 1:01
mt_samiei10-Oct-06 1:01 
AnswerRe: program transfer error Pin
Galatei10-Oct-06 1:26
Galatei10-Oct-06 1:26 
AnswerRe: program transfer error Pin
Hamid_RT10-Oct-06 18:37
Hamid_RT10-Oct-06 18:37 
Questionsimulate ALT Pin
viliam10-Oct-06 1:01
viliam10-Oct-06 1:01 
AnswerRe: simulate ALT Pin
Rajesh R Subramanian10-Oct-06 1:10
professionalRajesh R Subramanian10-Oct-06 1:10 
QuestionDisplay the folder Pin
radhika2810-Oct-06 0:46
radhika2810-Oct-06 0:46 
AnswerRe: Display the folder Pin
Rajesh R Subramanian10-Oct-06 1:06
professionalRajesh R Subramanian10-Oct-06 1:06 
GeneralRe: Display the folder Pin
radhika2810-Oct-06 1:19
radhika2810-Oct-06 1:19 
GeneralRe: Display the folder Pin
Rajesh R Subramanian10-Oct-06 1:32
professionalRajesh R Subramanian10-Oct-06 1:32 
GeneralRe: Display the folder Pin
Galatei10-Oct-06 1:39
Galatei10-Oct-06 1:39 
GeneralRe: Display the folder Pin
Rajesh R Subramanian10-Oct-06 1:48
professionalRajesh R Subramanian10-Oct-06 1:48 
GeneralRe: Display the folder Pin
Rajesh R Subramanian10-Oct-06 2:35
professionalRajesh R Subramanian10-Oct-06 2:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.