Solved

converting a string to in_addr

Posted on 2004-10-05
3
2,231 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
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 250 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

830 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