Solved

WSAAddressToString

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
only14 challenge 19 70
data scientists and AI 17 102
changeXy challenge 13 69
drawing animated level bar based on numbers 3 88
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

863 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

24 Experts available now in Live!

Get 1:1 Help Now