Re: gethostbyname return internal ip address when it should return external
- From: Test <test@.nil.invalid.com>
- Date: Wed, 09 Apr 2008 14:39:30 +0300
Test <test@.nil.invalid.com> wrote:
I use the following code to get the external internet IP address (the same that
is returned by ipconfig)
memset((char *)&saSockAddr,0,sizeof(saSockAddr));
pHostEntry=gethostbyname("");
if (pHostEntry)
{
memcpy((char *)&saSockAddr.sin_addr,pHostEntry->h_addr,pHostEntry->h_length);
}
wsprintf(sa1,"%d.%d.%d.%d",(((saSockAddr).sin_addr).S_un).S_un_b.s_b1,(((saSockAddr).sin_addr).S_un).S_un_b.s_b2,(((saSockAddr).sin_addr).S_un).S_un_b.s_b3,(((saSockAddr).sin_addr).S_un).S_un_b.s_b4);
Some users report they get internal ip 10.0.0.15 etc.
Most of the time it the external one.
How do I get the right IP everytime?
I tried another approach. This time from server
memset((char *)&saSockAddr,0,sizeof(saSockAddr));
getpeername(param->accepts,(void *)&saSockAddr,&sockelen);
wsprintf(sa1,"Client IP
%d.%d.%d.%d",(((saSockAddr).sin_addr).S_un).S_un_b.s_b1,(((saSockAddr).sin_addr).S_un).S_un_b.s_b2,(((saSockAddr).sin_addr).S_un).S_un_b.s_b3,(((saSockAddr).sin_addr).S_un).S_un_b.s_b4);
Unfortunately I get 0.0.0.0. socklen is 16
My clients send my server large text files with their ip as the first line. When
all has been received at the server the server sends back an "All received" text
message using the IP it got. Unfortunately I get a wrong ip embedded in the
textfile AND and the server from getpeername. For most clients its OK. The
problems i probably due to cleint sitting behind a router.
.
- Follow-Ups:
- Re: gethostbyname return internal ip address when it should return external
- From: Volodymyr M. Shcherbyna
- Re: gethostbyname return internal ip address when it should return external
- References:
- Prev by Date: Re: non-blocking connect might block winsock? ?????????
- Next by Date: Re: Lost network connection detection
- Previous by thread: gethostbyname return internal ip address when it should return external
- Next by thread: Re: gethostbyname return internal ip address when it should return external
- Index(es):
Relevant Pages
|