LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-30-2013, 03:29 PM   #1
yingjusuen
LQ Newbie
 
Registered: Feb 2008
Posts: 2

Rep: Reputation: Disabled
How to display server's IP on its own terminal?


Hi,

I am writing a server and client program using TCP/IP. The client works with the server fine. But what I'd like to do is: when I run the server, I'd like it to display its IP and port number on its terminal. Below is what I did, but the part for printing out server IP doesn't work:

struct sockaddr_in serv_addr;

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5001); //specify server port number

printf("IP Address: %s\n", inet_ntoa(serv_addr.sin_addr)); //OK
printf("Port Number: %d\n", ntohs(serv_addr.sin_port)); //gets 0.0.0.0

Any ideas what went wrong? Thanks!
 
Old 05-30-2013, 10:42 PM   #2
jason_m
Member
 
Registered: Jun 2009
Posts: 33

Rep: Reputation: 12
struct sockaddr_in is only a data structure for holding address information, and nothing more. You're probably going to need to associate it with a socket descriptor and make some other system calls before you can get your hands on an ip address. It has been a long time since I've played around with this api, but I do remember this being an invaluable reference:
http://www.beej.us/guide/bgnet/outpu...age/index.html

Edit: To elaborate a little further:
Quote:
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
This is specifically the reason you are getting 0.0.0.0 back. That is the value for INADDR_ANY. What this is saying is allow connections from any network interface. Your computer might only have one network interface. But some computers have multiple (consider a personal computer with a wired ethernet device and a wireless device, or think of a commercial router with multiple network interfaces). An IP address is associated with a network interface / device.

Continuing with the example of the personal computer with a wired and wireless device: say the wired device has address 192.168.1.1 and the wireless device has address 192.168.1.2. If you only wanted your program to accept connections from the wired device, you could assign that 192.168.1.1 to serv_addr.sin_addr.s_addr. By assigning INADDR_ANY (which is represented internally by 0.0.0.0), you are saying incoming connections from either network device are allowed.

So, in that case, it doesn't make sense to talk about the IP address of the server. At least, not until a connection is made - because we don't know which interface that connection will come in on. If you want to see which interface an incoming connection came in on, you'll have to accept() an incoming connection and investigate from there.

Last edited by jason_m; 05-30-2013 at 11:05 PM.
 
2 members found this post helpful.
Old 05-30-2013, 11:17 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,869
Blog Entries: 1

Rep: Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870Reputation: 1870
> printf("Port Number: %d\n", ntohs(serv_addr.sin_port)); //gets 0.0.0.0

Not possible. Try again.

PS: functions getsockname and getpeername might be your friends

Last edited by NevemTeve; 05-30-2013 at 11:19 PM.
 
Old 05-31-2013, 03:01 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You can use getifaddrs, which I think is a BSD extension that's also available on Linux. Keep in mind that there might be multiple IP addresses for one interface (at least on FreeBSD, maybe not on Linux.)

Kevin Barry
 
1 members found this post helpful.
Old 06-06-2013, 10:28 AM   #5
yrs7
LQ Newbie
 
Registered: Apr 2006
Distribution: OpenSUSE, Debian, RedHat, FreeBSD
Posts: 6

Rep: Reputation: 0
Thanks, jason_m, for your detailed explaination. It really solved my doubts of struct sockaddr_in. As for what I did to get my IP, I used "ifconfig" cmd to list all my IP interfaces and manually entered the IP to my code. Not sure if it's the best way, but that's all I can get so far.

However, I'd really like to know a way for my server program to get it's local IP address without my hard-coding it into my code. Any idea? Thanks.

Last edited by yrs7; 06-06-2013 at 10:41 AM.
 
Old 06-06-2013, 02:45 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by yrs7 View Post
However, I'd really like to know a way for my server program to get it's local IP address without my hard-coding it into my code. Any idea? Thanks.
Code:
#include <sys/types.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>


int main(int argc, char *argv[])
{
  //get a linked list containing the interface addresses

  struct ifaddrs *addrs = NULL;
  if (getifaddrs(&addrs) != 0) {
    fprintf(stderr, "%s: error reading interface addresses: %s\n", argv[0], strerror(errno));
    return 1;
  }

  struct ifaddrs *current = addrs;

  //iterate through the linked list

  while (current) {
    //skip over anything with a NULL or zero netmask
    if (current->ifa_netmask && ((struct sockaddr_in*) current->ifa_netmask)->sin_addr.s_addr)
      fprintf(stderr, "%s\t%s\n", current->ifa_name,
              current->ifa_addr? inet_ntoa(((struct sockaddr_in*) current->ifa_addr)->sin_addr) : NULL);
    current = current->ifa_next;
  }

  freeifaddrs(addrs);
}
Tested on both Linux and FreeBSD.

Kevin Barry
 
  


Reply



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
screwed up display as terminal killfast Linux - Software 11 12-21-2009 01:48 PM
Recompiled kernel, no more terminal display spiffytech Slackware 4 11-05-2007 03:40 AM
How to get display resolution from terminal? Passions Linux - General 1 08-31-2007 01:32 PM
viewing the display of another terminal drkstr Linux - General 1 03-14-2006 03:06 PM
Change terminal display. Radical_Dreamer Linux - Newbie 2 08-23-2005 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration