473,422 Members | 2,224 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,422 software developers and data experts.

Get list of all IP addresses

I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code.
thanks
svirdi

#include <unistd.h>
#include <stropts.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
//#include <sys/sockio.h>
int main(int argn,char** argv)
{
int sock,n;
struct ifreq *ifr;
struct ifconf ifc;
char buf[1024],addres[16];
unsigned char* adr;
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
// sock = socket(2, 2, 0);
printf("sock=%d\n", sock);
if (sock == -1)
{
printf("There is trouble of sock!! Exiting\n\n");
return 0;
}
ifc.ifc_buf = buf;
//ifc.ifc_len = 1024;
int retVal = ioctl(sock, SIOCGIFCONF, &ifc);
printf("retval ioctl(sock, SIOCGIFCONF, &ifc)=%d\n\n", retVal);

if (retVal < 0)
{
printf("There is trouble with ioctl\n\n");
close (sock);
return 0;
}
n = ifc.ifc_len/sizeof(struct ifreq);
for (ifr = ifc.ifc_req; n 0; n--, ifr++)
{
int rval = ioctl( sock, SIOCGIFADDR, ifr);
printf(" ioctl( sock, SIOCGIFADDR, ifr) rval = %d\n",
rval);
if (ifr->ifr_addr.sa_family == AF_INET ||
ifr->ifr_addr.sa_family == AF_INET6)
{
adr = reinterpret_cast<unsigned
char*>(ifr->ifr_ifru.ifru_addr.sa_data+2);
snprintf(addres,16,"%i.%i.%i.%i",adr[0],adr[1],adr[2],adr[3]);
printf("%s = %s\n",ifr->ifr_name,addres);
}
}
close(sock);
return 0;
}

Aug 8 '06 #1
2 5518
svirdi wrote:
I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code.
[..]
We cannot help you with that. Whatever mechanism you use to get all
IP addresses, it has nothing to do with C++. Neither the language nor
the library has any means for Internet communication, it is all done
using the functionality provided by your OS. Ask in the OS newsgroup.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #2
svirdi wrote:
I have been trying to get all ip addresses from all interfaces, with
the code listed below.
However, all I get is the 1st IP only. I have configured 2 IPs on a
single interface, I want to get both from my c++ code.
thanks
svirdi

#include <unistd.h>
#include <stropts.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <net/if.h>
#include <sys/ioctl.h>
I recommend comp.unix.programmer.


Brian
Aug 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: deko | last post by:
I have an html drop-down list (they're called combo boxes in vb, not sure what they're called in html): <select name="ipSelect" size="1" style="width:140px;"></select> The user is supposed to...
5
by: Paul Cheevers | last post by:
Hi, This is driving me nuts to say the least!!!!! I am trying to send an email from some server side ASP code and the CC field is giving me some problems. The code works fine if I have one...
15
by: C White | last post by:
I've got another drop list problem I am using the following code where users select a name, but it should pass a name and email into the table <select name="user"> <option value="<%...
4
by: Christine Forber | last post by:
I wonder if anyone knows of some javascript code to check a comma-delimited list of email addresses for basic formating. What I'm looking for is the javascript code to check a form field on form...
3
by: Mark V. | last post by:
Here's what I have and I'm stumped. I have a table that has several thousand names and addresses. I only want to send to one address in a household. So what I would like to do is create a new...
7
by: Evangelista Sami | last post by:
Hi all i have implemented a list type as an array of pointer like this typedef struct { int nb_elements; void **elements; } list; to avoid having a pointer for each element as it is done...
4
by: Andy M | last post by:
ALERT There is a person by the name of Mike Cox who's trying to turn this mailing list into a Big-8 newsgroup. Many of you know that this and most of the other postresql mailing lists are...
4
by: valeberry | last post by:
//Index.php <html><head><title>Mailing List Administration</title></head><body> <br> <center><H1>Mailing List Administration</H1></center> Send an email to a mailing list: <form method=post...
4
by: chris | last post by:
I need to maintain a list of subscribers to an email list for a "newsletter" that will be sent via a web form probably once a month. I anticipate low numbers--tens to maybe one hundred subscribers...
3
Sandboxer
by: Sandboxer | last post by:
I am not a coder, so please bear with me while I attempt to explain in only the surface language of Access. I have created a form to input all information about each of my customers. The base form...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.