|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Senior Member
Iscritto dal: Apr 2001
Città: Milano
Messaggi: 3736
|
[c++] windows getnameinfo
ciao,
è da un po' che utilizzo la funzione getnameinfo di Windows ovviamente la eseguo in un thread separato, ma non ho mai compreso come mai quando chiede il nome ad un apparato come ad esempio uno switch, intercorre sempre un tempo di 18 secondi il che rende letto il processo di scansione di una rete. Tale funzione di windows non implementa alcun timeout quindi non è possibile intervenire a questo livello per evitare di attendere 18 secondi. Mi ricordo di "looklan" uno scanner di rete, che esegue una scansione simile al programma da me scritto ma risulta più veloce, non mi spiego il motivo però. Grazie Il seguente codice l'ho preso in prestito dal sito di Microsoft e compilato funziona correttamente. Se come argomento fornite uno switch oppure una stampante il tempo che intercorre senza risolverne il nome è 18 secondi circa. Nel caso di PC invece, la risposta è quasi immediata. Codice:
#include <winsock2.h> #include <ws2tcpip.h> #include <stdio.h> // link with ws2_32.lib #pragma comment(lib, "Ws2_32.lib") int __cdecl main(int argc, char **argv) { //----------------------------------------- // Declare and initialize variables WSADATA wsaData = {0}; int iResult = 0; DWORD dwRetval; struct sockaddr_in saGNI; char hostname[NI_MAXHOST]; char servInfo[NI_MAXSERV]; u_short port = 27015; // Validate the parameters if (argc != 2) { printf("usage: %s IPv4 address\n", argv[0]); printf(" to return hostname\n"); printf(" %s 127.0.0.1\n", argv[0]); return 1; } // Initialize Winsock iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { printf("WSAStartup failed: %d\n", iResult); return 1; } //----------------------------------------- // Set up sockaddr_in structure which is passed // to the getnameinfo function saGNI.sin_family = AF_INET; saGNI.sin_addr.s_addr = inet_addr(argv[1]); saGNI.sin_port = htons(port); //----------------------------------------- // Call getnameinfo dwRetval = getnameinfo((struct sockaddr *) &saGNI, sizeof (struct sockaddr), hostname, NI_MAXHOST, servInfo, NI_MAXSERV, NI_NUMERICSERV); if (dwRetval != 0) { printf("getnameinfo failed with error # %ld\n", WSAGetLastError()); return 1; } else { printf("getnameinfo returned hostname = %s\n", hostname); return 0; } } |
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 02:20.