I am trying to accomplish something so very simple, and yet I can't seem to figure it out. What I need: a NSString containing the IP address given to a particular interface. Most importantly, I want to accomplish this programmaticly, not through some sort of shell script. I have searched for several days, and no one else seems to have had this problem.... which means it is probably so simple no one had to ask...
I am rather unskilled in C. My approach so far has been this
Unfortunately, this does not give me a specific interface and it does not give me anything but my bonjour ip. My gethostname() returns iWhit.local. I end up with something like 10.211.55.2 instead of 192.168.2.1
In a shell script I would use
but I don't want to use a shell script if at all possible... Plus I haven't been able to figure out how to get the output from
into a string..
Any help is much appreciated
Thanks,
Whit
I am rather unskilled in C. My approach so far has been this
Code:
char hostname[30];
gethostname(hostname, 30);
hostent * record = gethostbyname(hostname);
in_addr * address=(in_addr * )record->h_addr;
std::string ipstring = inet_ntoa(* address);
NSString *addressstr = [NSString stringWithCString:ipstring.c_str() encoding:NSUTF8StringEncoding];
In a shell script I would use
Code:
ifconfig en1 | grep -E "inet ([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})" | awk '{print $2}'
Code:
system("my shell script");
Any help is much appreciated
Thanks,
Whit