Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 05-25-2004, 02:26 PM   #1
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]);

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);

Thank you in advance
Old 05-25-2004, 03:48 PM   #2
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
Old 05-25-2004, 03:48 PM   #3
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.


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 01:18 PM
Add another IP address to an Interface hcclnoodles Linux - Software 1 08-19-2004 03:50 AM
URGENT: how to get an ip address of sending interface in C dravya Linux - General 4 05-25-2004 04:32 PM
multiple ip address for single eth interface blackhound Linux - Networking 2 02-25-2004 07:02 AM
How can I retrieve the IP address of an interface? kobilevi Linux - Networking 2 05-15-2001 08:31 AM

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

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