ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.)
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.