LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-02-2004, 12:20 AM   #1
hal2000
LQ Newbie
 
Registered: Jul 2004
Location: Christchurch - NewZealand
Distribution: Fedora Core 2
Posts: 2

Rep: Reputation: 0
gethostbyname() is not returning my IP address


Hi everybody,
I am trying to get a simple TCP client program on linux talking to a TCP server program running winXP (and using winsock32) over ethernet. I can connect to the windows server, but the server detects the IP address of the linux client as being the same as its own IP address and will not recieve any data.

i.e windows server IP = 192.168.1.20
linux client IP = 192.168.1.24 (from /sbin/ifconfig eth0)

the windows server detects the connecting socket IP address (from listen(...)) to be 192.168.1.20! - the local IP address!?

I tried to bind the linux clients socket to the local address before connecting but no luck. Then i tried using the gethostbyname() function and that gave me the correct hostname it only found the local loop IP address 127.0.0.1?

could the client be binding the socket address to 127.0.0.1 which the server interprets as its own local IP address?

samba is configured to share files with the rest of the lan (win2000 DNS server) and i can see the lan machines by IP address only - not there hostnames. does this mean there perhaps may be a problem with the DHCP client not obtaining the DNS information from the win2000 DNS name server properly?

Linux gethostbyname function

bool GetLocalAddress(struct in_addr* p_local)
{
bool ret = false;

char ac[80];
char ac_username[100];

// Get local host name
if (gethostname(ac, sizeof(ac)) != -1)
{
std::cout << "Host name: " << ac << "\n";

// Find the IP address using the host name
struct hostent* p_he = gethostbyname(ac);

if (p_he != 0)
{
for (int i=0; p_he->h_addr_list[i] != 0; ++i)
{
struct in_addr addr;

memcpy(&addr, p_he->h_addr_list[i], sizeof(struct in_addr));

std::cout << "Local IP address " << i << ": " << inet_ntoa(addr) << "\n";

// Find first address that isn't 127.0.0.1
if(strcmp(inet_ntoa(addr), "127.0.0.1") != 0)
{
p_local->s_addr = inet_addr(inet_ntoa(addr));

std::cout << "Binding to IP address " << i << ": " << inet_ntoa(addr) << "...\n";

ret = true;
break;
}
}
}
else
{
std::cout << "Cannot find IP address for " << ac << "\n";
}
}

return ret;
}

function outputs:

Host name: myhostname
Local IP address 0: 127.0.0.1


Any help would be much appreciated
thanks
Pete
 
Old 07-04-2004, 05:48 AM   #2
deloptes
Member
 
Registered: Feb 2004
Location: AT
Distribution: debian etch and SUSE 10.2
Posts: 123

Rep: Reputation: 15
I suppose you don't have any dhcp or DNS server

In this case you should edit manually on both windows and linux the file called hosts

on linux it's in the /etc directory on windows it's in c:\windows\system32\drivers\etc\hosts as far as I remember

watch out that those files are equivalent.

What I do is simply put it down on my linux server (/etc/hosts)

127.0.0.1 localhost
192.168.40.XX linuxmachine
129.168.40.XY windowsmachine

after this I copy it to lets say /tmp and do unix2dos /tmp/hosts and copy it on the windows machine

IT's working like a charm
 
Old 07-04-2004, 05:12 PM   #3
hal2000
LQ Newbie
 
Registered: Jul 2004
Location: Christchurch - NewZealand
Distribution: Fedora Core 2
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the reply deloptes,
My local network does have a DNS server on a windows machine, and it is assigning my linux machine an ip address of 192.168.1.24 (under interface eth0).

By editing the hosts file I can obtain this ip address using the gethostbyname(...) function like you suggest. But I would like the code to be portable - hence the need to get the local ip address without editing the hosts file or hard-coding an ip address into the code.

Also I found the windows server i am trying to connect to still detects an incorrect ip address from the linux client even when I am using a hard-coded IP or edited hosts file.

Any ideas?

Thanks again deloptes I appreciate the help
Cheers
Pete
 
Old 07-05-2004, 01:27 AM   #4
deloptes
Member
 
Registered: Feb 2004
Location: AT
Distribution: debian etch and SUSE 10.2
Posts: 123

Rep: Reputation: 15
I am not familiar with windows DNS issues

check out your hosts.conf and of course the resolv.conf

If you have a local domain you can create the file defaultdomain on the linux client if there are multipule networks.

Are you really sure windows is giving you the right IP ... or IP at all

After all I've seen recently some XP machine just giving up their winsock32 libraries ... very strange behavior... As metter of fact it seemed to be a virus and the windows boxes couldn't make socket-connections anymore Stupid M$!

Do you use firewall or perhaps iptables/chains on the linux side so that the queries are blocked

what did you put in your /etc/networking/interfaces file or whatever is the equivalent on your systems

very strange

regards
 
Old 09-29-2004, 08:16 AM   #5
castlesteve
LQ Newbie
 
Registered: Sep 2004
Location: Atlanta
Posts: 1

Rep: Reputation: 0
Curious...

hal2000, have you figured what the problem was? I too have this issue with uClinux. Appear to have the same setup (dns setup correctly and IP address is dynamic to eth0).

-steve
 
Old 10-11-2004, 05:54 PM   #6
deloptes
Member
 
Registered: Feb 2004
Location: AT
Distribution: debian etch and SUSE 10.2
Posts: 123

Rep: Reputation: 15
samba works as wins or can use wins from win-pdc this way DNS-resolution is over samba ... or the pc acting as pdc ...
I use DHCP and DNS servers on machine A
i have samba as PDC on machine B

my clients connect without problem to those machines ... gethostbyname resolves properly ...

sorry bro's there are so many things to check ...

if you have gethostbyname() error this means that on your local machine there is something wrong. like wrong information provided by dhcp/dns server or local misconfiguration of your IP/hostname

starting with /etc/hostname, hosts, smb.conf etc. check all possible configuration issues

I hope you'll manage it
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Weird card problem - DHCP returning IPv6 address lacitpo Linux - Wireless Networking 2 03-16-2005 11:28 PM
gethostbyname in script? farry Linux - Newbie 2 03-14-2005 02:14 AM
kmalloc returning overlapped address dipakn Linux - General 4 10-29-2003 11:29 PM
Program returning ip-address? smellofsunshine Linux - Software 4 08-11-2003 05:59 AM
gethostbyname error for aimstr8 Linux - Security 7 03-09-2002 04:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration