È presente una chiamata di funzione di Windows C++ incorporata che può ottenere nome host e indirizzo IP? Grazie.Chiamata di funzione di Windows C++ che ottiene nome host e indirizzo IP locali
risposta
Per ottenere il nome dell'host è possibile utilizzare: gethostname o il metodo asincrono WSAAsyncGetHostByName
per avere le informazioni indirizzo, è possibile utilizzare: getaddrinfo o la versione Unicode GetAddrInfoW
È possibile ottenere ulteriori informazioni sul computer nome come il dominio utilizzando l'API Win32: .
Ecco una soluzione multipiattaforma ... Windows, Linux e MacOSX. È possibile ottenere indirizzo IP, porta, sockaddr_in, porta.
BOOL GetMyHostName(LPSTR pszBuffer, UINT nLen)
{
BOOL ret;
ret = FALSE;
if (pszBuffer && nLen)
{
if (gethostname(pszBuffer, nLen) == 0)
ret = TRUE;
else
*pszBuffer = '\0';
}
return ret;
}
ULONG GetPeerName(SOCKET _clientSock, LPSTR _pIPStr, UINT _IPMaxLen, int *_pport)
{
struct sockaddr_in sin;
unsigned long ipaddr;
ipaddr = INADDR_NONE;
if (_pIPStr && _IPMaxLen)
*_pIPStr = '\0';
if (_clientSock!=INVALID_SOCKET)
{
#if defined(_WIN32)
int locallen;
#else
UINT locallen;
#endif
locallen = sizeof(struct sockaddr_in);
memset(&sin, '\0', locallen);
if (getpeername(_clientSock, (struct sockaddr *) &sin, &locallen) == 0)
{
ipaddr = GetSinIP(&sin, _pIPStr, _IPMaxLen);
if (_pport)
*_pport = GetSinPort(&sin);
}
}
return ipaddr;
}
ULONG GetSinIP(struct sockaddr_in *_psin, LPSTR pIPStr, UINT IPMaxLen)
{
unsigned long ipaddr;
ipaddr = INADDR_NONE;
if (pIPStr && IPMaxLen)
*pIPStr = '\0';
if (_psin)
{
#if defined(_WIN32)
ipaddr = _psin->sin_addr.S_un.S_addr;
#else
ipaddr = _psin->sin_addr.s_addr;
#endif
if (pIPStr && IPMaxLen)
{
char *pIP;
struct in_addr in;
#if defined(_WIN32)
in.S_un.S_addr = ipaddr;
#else
in.s_addr = ipaddr;
#endif
pIP = inet_ntoa(in);
if (pIP)
adjust_str(pIP, pIPStr, IPMaxLen);
}
}
return ipaddr;
}
int GetSinPort(struct sockaddr_in *_psin)
{
int port;
port = 0;
if (_psin)
port = _Xntohs(_psin->sin_port);
return port;
}
Questo è fantastico, grazie mille! – Stan
come testare la funzione GetSinIP? –
Si noti che il nome host restituito da 'gethostname()' è ** non ** necessariamente il nome restituito dalla risoluzione del nome di dominio per qualsiasi interfaccia dell'host! Tuttavia ** potrebbe ** essere lo stesso nome, se configurato in questo modo. – alk
- 1. Risoluzione indirizzo IP di un nome host
- 2. Indirizzo IP a nome host in Java?
- 3. conversione di un indirizzo IP nel nome host
- 4. C++ Indirizzo IP forma leggibile
- 5. Il file host può reindirizzare un indirizzo IP?
- 6. Risolvi IP su Nome host usando PHP
- 7. Ottieni nome host dall'indirizzo IP
- 8. Indirizzo IPv6 in base al nome host
- 9. Posso forzare le registrazioni Eureka a utilizzare un indirizzo IP invece di un nome host?
- 10. server di Jenkins non è accessibile tramite il nome host (indirizzo IP)
- 11. Indirizzo IP di analisi in .NET
- 12. Ottieni indirizzo IP e descrizione adattatore utilizzando C#
- 13. indirizzo IP firmato cloudfront indirizzo IP
- 14. Indirizzo IP in windows phone 8
- 15. Eseguire una ricerca DNS per risolvere un nome host in un indirizzo IP utilizzando JavaScript
- 16. Impossibile determinare l'indirizzo IP dal nome host
- 17. IP host Wordpress modificato
- 18. PHP display server Indirizzo IP
- 19. Convalida IPv4, IPv6 e nome host
- 20. Python: Ottieni indirizzo IP locale utilizzato per inviare dati IP a uno specifico indirizzo IP remoto
- 21. Debugger oggetto Facebook - Impossibile risolvere il nome host in un indirizzo IP valido
- 22. C++ funzione di chiamata identificativo
- 23. Nome host/IP non corrisponde al nome alternativo del certificato
- 24. Come rilevare un indirizzo IP di MS Windows senza polling
- 25. indirizzo IP Domain Name
- 26. Creazione di un IPEndPoint da un nome host
- 27. C++ distruttore e chiamata di funzione fine
- 28. Indirizzo IP da sk_buff
- 29. Indirizzo IP che termina con zero?
- 30. Docker-machine - indirizzo IP conflitto
gethostname(), getaddrinfo() – WhirlWind