Sockets: Define Host for gethostbyname

The standard examples of using sockets :
int main(int argc, char *argv[])
. . . . .
server = gethostbyname(argv[1]);
where argv[1] is the hostname.

How would I define hostname as a variable, instead of a passed parameter ?

string hostname="somehost";
server = gethostbyname(&hostname); ???
 
server = gethostbyname(hostname.c_str());


assuming C++ and string is std::string.
It's not clear what you're asking, but gethostbyname returns a structure that contains all the names that the network resolver knows for the host with the specified name. It does not return a string.

There's an example here that you may find helpful.
https://www.stev.org/post/cgethostbynameexample
https://docs.microsoft.com/en-us/windows/win32/api/wsipv6ok/nf-wsipv6ok-gethostbyname
Last edited on
Topic archived. No new replies allowed.