[clug-progsig] C Question about getting host ip address in Linux

Michael Petch mpetch at capp-sysware.com
Mon Sep 13 23:03:41 PDT 2004


I will take one stab at this though. Assuming you are trying to get your
own local IP address, after creating a socket you fill int he data
structure as you have. You then need to "bind" to the socket. Lastly you
probably want to call getsockname to return you the information about
the bound socket, and then display the information in the returned data
structure. You can use the function inet_ntoa to convert a sin_addr
value to an x.x.x.x formatted string.


On Mon, 2004-09-13 at 23:34, Michael Petch wrote:
> I might be missing something. Are you trying to figure out your own IP
> address or the IP address of another system? AMong other things I see
> you fill out a structure without a target IP address, but you never
> attempt to connect to anything?
> 
> On Mon, 2004-09-13 at 23:00, Reno L wrote:
> > Hi, there, I have trouble to get the host ip address
> > with following code:
> > 
> > #include <....>
> > int NetEntity::openSocket() //open a socket, setup the
> > 
> >                             //port and IP address
> > {  //open a socket
> >   if ((sockFd = socket(AF_INET, SOCK_STREAM, 0)) ==
> > -1)
> >    {          
> >             perror("socket open error\n");
> >             exit(1);
> >    }
> > 
> >   // zero the rest of the struct
> >   bzero(&myAddr, sizeof(myAddr));
> > 
> >   // host byte order
> >   myAddr.sin_family = AF_INET;
> >  
> >   // short, network byte order
> >   myAddr.sin_port = htons(PORT); 
> > 
> >   // auto-fill with my IP
> >   myAddr.sin_addr.s_addr = INADDR_ANY; 
> >   
> >   cout<<" Open the socket....\n";
> >   cout<<" The address is "<<myAddr.sin_addr.s_addr<<",
> >  port number is "<<myAddr.sin_port<<".\n";
> >   return sockFd;
> > }
> > 
> > void NetEntity::toClose()
> > {
> >   int c=close(sockFd);
> >   if ( c == -1) {     
> >      cout<<"socket close error\n";
> >   exit(1);
> >   }
> >   cout<<"Closing network socket.....\n";
> > }
> > 
> > int main()
> > {
> >   NetEntity ne;
> >   ne.openSocket();
> >   ne.toClose();
> >   return 0;
> > }
> > -----------------------
> > Compile passed.
> > -----------------------
> > Output:
> >  Open the socket....
> >  The address is 0,  port number is 41485.
> > Closing network socket.....
> > -------------------------------------------
> > My question is why the address is 0???? 
> > and how can I output my host address as
> > xxxx.xxxx.xxxx.xxxx format? Thanks.
> > 
> > 
> > 
> > 
> > 		
> > __________________________________
> > Do you Yahoo!?
> > Yahoo! Mail is new and improved - Check it out!
> > http://promotions.yahoo.com/new_mail
> > 
> > _______________________________________________
> > clug-progsig mailing list
> > clug-progsig at clug.ca
> > http://clug.ca/mailman/listinfo/clug-progsig_clug.ca
-- 
Mike Petch
CApp::Sysware Consulting Ltd.
Suite 1002,1140-15th Ave SW.
Calgary, Alberta, Canada.
T2R 1K6.
(403)804-5700.




More information about the clug-progsig mailing list