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

x
?
Solved

WSAAddressToString

Posted on 2003-11-10
4
Medium Priority
?
1,114 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 500 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 does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

650 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