Winsock, lokale ip adresse
-
Hi,
ich habe einen Server geschrieben und alles klappt perfekt. Nun möchte ich verhindern, dass sich ein lokaler Client zu diesem verbindet. Wie kann ich also die lokale IP Adresse(192.168....) herausfinden? Ich nutze Winsock.
Danke,
Sockee
-
getpeername
-
Im Prinzip bräuchte ich nur eine Funktion, die mir die lokale IP-Adresse vom Computer ausgibt, auf dem das Programm gerade ausgeführt wird.
IF (MEINE_IP != AKTUELLE_IP) connect(AKTUELLE_IP, ...); ELSE NOTHING
-
Is copy&paste aus 3 Jahre alten Notizen:
unsigned long longToIp(const char* szIp) { int octets[4]; const char* auxCad = szIp; unsigned long lIp = 0; for(int i = 0; i < 4; ++i) { octets[i] = atoi(auxCad); if(octets[i] < 0 || octets[i] > 255) return 0; lIp |= (octets[i] << (i * 8)); auxCad = strchr(auxCad, '.'); if(auxCad == 0 && i != 3) return -1; auxCad++; } return lIp; } // ALL UGLY... PMIB_UDPTABLE pUdpTable; unsigned long size = 0; bool free = 0; if(GetUdpTable(0, &size, 0) == ERROR_INSUFFICIENT_BUFFER) { pUdpTable = (MIB_UDPTABLE*) new char[size]; free = true; } if(GetUdpTable(pUdpTable, &size, 0) == NO_ERROR) { if(pUdpTable->dwNumEntries > 0) { for(unsigned i = 0; i < pUdpTable->dwNumEntries; ++i) { if(htons(*((unsigned short *)&pUdpTable->table[i].dwLocalPort)) == gamePort) { localIp = inet_ntoa(*(struct in_addr *)(char*)&pUdpTable->table[i].dwLocalAddr); ulLocalIp = longToIp(localIp.c_str()); } } } } if(free) delete [] pUdpTable; OR char hostName[MAX_PATH]; WSADATA wsaData; WSAStartup(MAKEWORD(2, 0), &wsaData); gethostname(hostName, MAX_PATH); HOSTENT *hostEnt = gethostbyname(hostName); in_addr *host = reinterpret_cast<LPIN_ADDR>(hostEnt->h_addr); localIp = inet_addr(inet_ntoa(*host)); WSACleanup();