add a function to get the local hostname
This commit is contained in:
parent
1e43f7b17a
commit
a76d96f716
2 changed files with 22 additions and 0 deletions
src
|
@ -114,6 +114,8 @@ extern void NetExit();
|
|||
extern unsigned long NetResolveHost(const std::string &host);
|
||||
/// Get local IPs
|
||||
extern int NetSocketAddr(unsigned long *ips, int maxAddr);
|
||||
/// Get local hostname
|
||||
extern std::string NetGetHostname();
|
||||
|
||||
/// Open a UDP Socket port. (param in network format)
|
||||
extern Socket NetOpenUDP(unsigned long ip, int port);
|
||||
|
|
|
@ -206,6 +206,26 @@ unsigned long NetResolveHost(const std::string &host)
|
|||
return INADDR_NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the system hostname.
|
||||
*/
|
||||
std::string NetGetHostname()
|
||||
{
|
||||
char hostname_buffer[256];
|
||||
#ifdef _WIN32
|
||||
DWORD hostname_size = (DWORD)sizeof(hostname_buffer);
|
||||
if (GetComputerNameA(hostname_buffer, &hostname_size)) {
|
||||
return std::string(hostname_buffer);
|
||||
}
|
||||
#else
|
||||
size_t hostname_size = sizeof(hostname_buffer);
|
||||
if (gethostname(hostname_buffer, hostname_size) == 0) {
|
||||
return std::string(hostname_buffer);
|
||||
}
|
||||
#endif
|
||||
return "localhost";
|
||||
}
|
||||
|
||||
/**
|
||||
** Get IP-addrs of local interfaces from Network file descriptor
|
||||
**
|
||||
|
|
Loading…
Add table
Reference in a new issue