Getting ip of client using winsock
-
Hi, Ive done some searching with no luck to get the ip of client connected to a socked using winsock. heres what ive got which may be totally off... accept( ListenSocket, (SOCKADDR*) &clientip, NULL); int strangeip=clientip.sin_addr.s_addr; strange ip reports some strange int... cant figure out how to get the actual ip there.. or convert the u_long to hostent.. i think i can get hostent converted to ip however cant get that far... any help would be appreciated...
-
Hi, Ive done some searching with no luck to get the ip of client connected to a socked using winsock. heres what ive got which may be totally off... accept( ListenSocket, (SOCKADDR*) &clientip, NULL); int strangeip=clientip.sin_addr.s_addr; strange ip reports some strange int... cant figure out how to get the actual ip there.. or convert the u_long to hostent.. i think i can get hostent converted to ip however cant get that far... any help would be appreciated...
See if this helps inet_ntoa(client.sin_addr) Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
-
See if this helps inet_ntoa(client.sin_addr) Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_
hmm.. getting 204.204.204.204 from the inet_ntoa.. which probably means.. that part of the accept is not going to help me get the ip... i assume.. ive seen this before.... char hostname[256]; struct hostent *host; in_addr ip; gethostname(hostname, 256); host = gethostbyname(hostname); memcpy(&ip, host->h_addr, sizeof(ip)); printf("First host IP is: %s\n", inet_ntoa(ip)); which seems to get the local ip.. which is not what im trying to do...
-
Hi, Ive done some searching with no luck to get the ip of client connected to a socked using winsock. heres what ive got which may be totally off... accept( ListenSocket, (SOCKADDR*) &clientip, NULL); int strangeip=clientip.sin_addr.s_addr; strange ip reports some strange int... cant figure out how to get the actual ip there.. or convert the u_long to hostent.. i think i can get hostent converted to ip however cant get that far... any help would be appreciated...
I think you can get the IP in this manner:
sockaddr_in clientAddress; accept( ..., (SOCKADDR*)&clientAddress, ...); in_addr inAddr = clientAddress.sin_addr;
Now the four components of IP, from left to right, are in
inAddr.S_un.S_un_b.s_b#
members:printf("The IP is: %i.%i.%i.%i", inAddr.S_un.S_un_b.s_b1, inAddr.S_un.S_un_b.s_b2, inAddr.S_un.S_un_b.s_b3, inAddr.S_un.S_un_b.s_b4);
Hope it helps.