?
Solved

converting a string to in_addr

Posted on 2004-10-05
3
Medium Priority
?
2,320 Views
Last Modified: 2008-02-01
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
Comment
Question by:mvoiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 8

Expert Comment

by:mnashadka
ID: 12226556
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
 

Author Comment

by:mvoiers
ID: 12226650
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
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1000 total points
ID: 12226990
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question