[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

converting a string to in_addr

Posted on 2004-10-05
3
Medium Priority
?
2,421 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

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.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

656 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