• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2487
  • Last Modified:

converting a string to in_addr

I am trying to convert a string to an in_addr type defined in the winsock library.  It seems I want to use the inet_addr function, which returns a long which in turn I guess I can assign to the in_addr structure.   When I try something like inet_addr(mystring.c_str) I runinto  error C2664: 'inet_addr' : cannot convert parameter 1 from 'const char *(void) const' to 'const char *'  so then I try using mystring.data and get another conversion error... could someone provide a snippet on how to go from string to in_addr.


0
mvoiers
Asked:
mvoiers
1 Solution
 
mnashadkaCommented:
Change it to:
inet_addr(mystring.c_str())

c_str is a member function, and it needs to be called to get the const char *.  Hope this helps.
0
 
mvoiersAuthor Commented:
yes, I just caught that one myself, but now after I call inet_addr I have a long, how do I get this into the in_addr structure?

0
 
itsmeandnobodyelseCommented:
Here is sample code (the ip address is in unsigned long ipServer)

    /* fill awsome sockaddr_in structure */
    memset (&host, 0, sizeof (host));               /* set all zero                 */
    host.sin_family      = AF_INET;                 /* the only family supported    */
    host.sin_port        = htons ((UShort)portNo);  /* host order to network order  */
    host.sin_addr.s_addr = htonl (ipServer);        /* that is little to big endian */

Rergards, Alex
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now