Solved

converting a string to in_addr

Posted on 2004-10-05
3
2,190 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
3 Comments
 
LVL 8

Expert Comment

by:mnashadka
Comment Utility
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
Comment Utility
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 250 total points
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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…
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now