473,385 Members | 1,772 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,385 software developers and data experts.

IP address of host...

Dheeraj Joshi
1,123 Expert 1GB
Hi.. are there any exposed functions in c which gives IP address of host directly, like in java or c#???

If yen in which header file?

Regards
Dheeraj Joshi
Aug 28 '09 #1
7 3349
newb16
687 512MB
It's os- and framework- dependent. There are no such functions in the standard.
Aug 28 '09 #2
There is a pretty standard way to do it through. getaddrinfo() in the berkley sockets API can get you the IP. Not ANSI C, but it works on Windows, 'nix, Mac and I bet a lot more systems.
Aug 28 '09 #3
Dheeraj Joshi
1,123 Expert 1GB
I got this program

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <netdb.h>
  4. #include <netinet/in.h>
  5. #include <sys/socket.h>
  6. #ifndef   NI_MAXHOST
  7. #define   NI_MAXHOST 1025
  8. #endif
  9.  
  10. int main(void)
  11. {
  12.     struct addrinfo * result;
  13.     struct addrinfo * res;
  14.     int error;
  15.  
  16.     error = getaddrinfo("www.google.com", NULL, NULL, &result);
  17.     if (0 != error)
  18.     {   
  19.         fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
  20.         return 1;
  21.     }   
  22.  
  23.     for (res = result; res != NULL; res = res->ai_next)
  24.     {   
  25.         char hostname[NI_MAXHOST] = "";
  26.         printf("%u\n",res->ai_addr);
  27.         error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname,            NI_MAXHOST, NULL, 0, 0); 
  28.         if (error != 0)
  29.         {
  30.             fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
  31.             continue;
  32.         }
  33.         if (*hostname)
  34.         {
  35.             printf("hostname: %s\n", hostname);
  36.         }
  37.      }   
  38.     freeaddrinfo(result);
  39.     return 0;
  40. }
  41.  
But unable to print the address in 10.50..... format..

And i am unable to get local host address also... Any inputs...

Regards
Dheeraj Joshi
Aug 31 '09 #4
Banfa
9,065 Expert Mod 8TB
a. There is no point posting code and telling us "it didn't work". How didn't it work? Did it compile without errors and warnings? If it compiled did you get unexpected results when you ran it? If you got unexpected results what was the expect result? What was the unexpected result? What, if any, was the input data to the program? In the light of these questions "it didn't work" can be seen as a wholly inadequate description of your error.

b. If you took the time to understand the program you have run instead of just running it and complaining when it doesn't produce the data you require you would get futher. In particular I suggest you examine the structures being used by this program to see if they contain the data you are interested in. If they do then you can modify the program to print out those values instead.
Aug 31 '09 #5
JosAH
11,448 Expert 8TB
That's what people often do: they search the internet for pieces of code; they copy and paste it compile it and try to run it. Most of the time it doesn't even compile and if it does it doesn't run. Surprised they enter the forums and desperately ask around: "it doesn't run, help me, urgent, plz". If they finally are spoonfed with working code they end up with another piece of not understood code but, hey, it runs. They copy it further to the internet as answers to other people's questions.

ICT isn't a science anymore, it's like stamps collecting but now they collect code that they don't understand, but hey, it runs. And then they end up at the other end of the outsourcing pipe line and we get all that crap back. It's a shame.

kind regards,

Jos
Aug 31 '09 #6
Dheeraj Joshi
1,123 Expert 1GB
Thanks for the inputs :)

Regards
Dheeraj Joshi
Sep 1 '09 #7
JosAH
11,448 Expert 8TB
@dheerajjoshim
And now what? Are you going to try out other forums hoping they will spoonfeed you without criticism? Do you understand that snippet of code you posted or are you just hoping for the best? It won't automagically do what you have in mind.

kind regards,

Jos
Sep 1 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Alexander N. Spitzer | last post by:
If I have a machine with 3 virtual IP addresses (192.168.1.), how can I start 3 instances of the same RMI application (each started with different properties/configs), each listening on the port...
7
by: none | last post by:
I want to determine the outside (non local, a.k.a. 127.0.0.x) ip addresses of my host. It seems that the socket module provides me with some nifty tools for that but I cannot get it to work...
3
by: roger beniot | last post by:
I would like to figure out how to detect an IP address change for an XP/Win2K3 machine that is leasing an IP via DHCP (and do it in C#)... Is there any event that indicates an IP address...
24
by: Daniel Crespo | last post by:
Hi, I tried: import ctypes import socket import struct def get_macaddress(host): """ Returns the MAC address of a network host, requires >= WIN2K. """
4
by: andreas.w.h.k. :-\) | last post by:
How do I change the address location in the wsdl <wsdl:port name="SearchSoap12" binding="tns:SearchSoap12"> <soap12:address location="http://searchservices/engine/search.asmx" /> </wsdl:port> ...
5
by: Hooyoo | last post by:
Hi, here, How to get local machine name and IP address? Thanks.
2
by: martin lanny | last post by:
Simple network scanner is a part of my dotnet solution. It pings ip addresses in a selected network range and gives me the response time for each computer it finds. Anyhow, I would need to...
14
by: John Kotuby | last post by:
Hi all, I have a situation where a Web Server at an install site does not currently have a publically registered Domain Name associated with the site. This requires that the a user access the...
0
by: eddiefisher41 | last post by:
Hi guys. Im having a little trouble with one of my phyon cgi scripts. Basically i need to a function that runs on the server size as a python cgi script but returns the IP address of the web...
0
by: sganeshsvk | last post by:
sir, In Linux, We use send mail from some specific client host IP address to main server by using postfix configuration. Suppose Some unwanted users or other third persons hosts send the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.