cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
UNIX/Linux Programming
Sockets: Define Host for gethostbyname
Sockets: Define Host for gethostbyname
Sep 21, 2020 at 5:46pm UTC
Michael002
(2)
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); ???
Sep 21, 2020 at 5:49pm UTC
seeplus
(6591)
server = gethostbyname(hostname.c_str());
assuming C++ and string is std::string.
Sep 23, 2020 at 11:02pm UTC
kbw
(9488)
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
Sep 23, 2020 at 11:04pm UTC
Topic archived. No new replies allowed.