Solved

WSAAddressToString

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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 …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

12 Experts available now in Live!

Get 1:1 Help Now