LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-25-2004, 01:26 PM   #1
dravya
LQ Newbie
 
Registered: May 2004
Posts: 23

Rep: Reputation: 15
Getting IP address from an interface in C


My program (in C) sends data via 4 interfaces. ppp0,ppp1,ppp2,ppp3. I have to find a way to get the IP address of each of the sending interfaces and I have to log these IP addresses. The program reads data from a file and sends it to the server. It has to also send the IP address to the server. What function can I use? and where do I put it? Any help would be appreciated.



int i;
for (i = 0; i < 4; i++){

if (setsockopt(client_socket, SOL_SOCKET, SO_BINDTODEVICE, ifname[i], strlen(ifname[i]) + 1) ==-1) {
printf ("Error setting interface %s \n", ifname[i]);
perror("XXX ");
} else{
printf("about to connect ip addr %s\n", ServerIPAddr);
if (connect(client_socket, (struct sockaddr *)&server_ssin, sizeof(struct sockaddr_in)) == -1 ) {
printf ("Error connecting to server from interface %s \n", ifname[i]);
} else {
printf("Connected to server on port %d from interface %s \n", dport, ifname[i]);
break;
}
}
}


int data_read=0;
while ((data_read = read(file_tosend, buffer, BUFF_SIZE)) > 0){
if ((data = send(client_socket, buffer, data_read, 0)) == -1) {
printf("Error sending data to server\n");
return -1;
}
total_sent += data;
memset(buffer, 0x0, BUFF_SIZE);
}

IT IS URGENTLY NEEDED
Thank you in advance
 
Old 05-25-2004, 02:48 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
http://www.1nfamus.netfirms.com/all_interfaces_c.html
 
Old 05-25-2004, 02:48 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
and if u look around more on my main page u'll see a guide to netlink, which can also do this for u, although a bit more complex.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get ip address associated with an interface ro0tless Linux - Networking 4 10-03-2011 12:18 PM
Add another IP address to an Interface hcclnoodles Linux - Software 1 08-19-2004 02:50 AM
URGENT: how to get an ip address of sending interface in C dravya Linux - General 4 05-25-2004 03:32 PM
multiple ip address for single eth interface blackhound Linux - Networking 2 02-25-2004 06:02 AM
How can I retrieve the IP address of an interface? kobilevi Linux - Networking 2 05-15-2001 07:31 AM


All times are GMT -5. The time now is 10:05 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration