Solved

WSAAddressToString

Posted on 2003-11-10
4
1,075 Views
Last Modified: 2013-11-13
I have a string and an int
string = address (IP or name)
int = port

I need to convert it so it can be used by various winsock functions.

Question is: How
0
Comment
Question by:Daij-Djan
[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
  • 2
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
_nn_ earned 125 total points
ID: 9717327
I use inet_addr() and gethostbyname() in such cases. Something like :

// parameters
    char * host;
    int port;

// local variables
    struct hostent * hostentptr;
    struct hostent hostentstruct;
    struct sockaddr_in sock_name;

    unsigned long iaddr = inet_addr( host );
    if( iaddr==INADDR_NONE ) {
        if( (hostentptr=gethostbyname(host))==NULL ) {
            // todo: error handling
            return -1;
        }
        hostentstruct = *hostentptr;
        sock_name.sin_family = hostentstruct.h_addrtype;
        sock_name.sin_addr = *((struct in_addr *)hostentstruct.h_addr);
    } else {
        sock_name.sin_family = AF_INET;
        sock_name.sin_addr.S_un.S_addr = iaddr;
    }
    sock_name.sin_port = htons( port );
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 9717608
Okidoki. msdn samples use that too.
0
 
LVL 16

Expert Comment

by:_nn_
ID: 9717640
Thanks. :)

Well, if you checked MSDN, you've certainly found about WSAStringToAddress()
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/wsastringtoaddress_2.asp

I like the "old" way though.
0
 
LVL 5

Author Comment

by:Daij-Djan
ID: 9717824
I did ;) but after I asked here - In the end I took your code so you earned the points
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

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 117
sum13 challenge 24 135
Need Multiple Versions of Python Using Virutalenvwrapper-win and Virtualenv 5 72
troubleshoot a python script 8 78
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

730 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