Solved

WSAAddressToString

Posted on 2003-11-10
4
1,088 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
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.

691 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