Solved

Question on gethostbyname() function

Posted on 2008-10-13
4
305 Views
Last Modified: 2012-05-05
Hello group,

I'm new to socket programming in C (Linux) and currently trying to retrieve a page which works fine as long as there argument is only something like www.example.com but  for instance if I pass:

myprog   www.example.com

the code returns the html page contents and all tags ...


but if I use it as

myprog  www.example.com/corporate/aboutus.php

It won't work if I add a / or page at the end of it. Any idea why?


Your help is appreciated.

Regards.


It seems in new IPv6 I should use getaddrinfo() but since I'm using somebody's else code so don't want to change it too much.



0
Comment
Question by:akohan
  • 2
  • 2
4 Comments
 
LVL 17

Expert Comment

by:sweetfa2
ID: 22707893
gethostbyname is trying to resolve an ip address for the host itself.  in your second instance you are passing a full url which cannot be resolved through a dns.
0
 

Author Comment

by:akohan
ID: 22707937


Thanks! now what approach should I take to return an sub page or a page from a server?

Regards.
0
 
LVL 17

Accepted Solution

by:
sweetfa2 earned 500 total points
ID: 22707950
Depends on what you have at the moment to retrieve this.

What does your myprog do after it resolves the hostname.  I am presuming it does asocket  open on port 80 of the resolved address, sends a "GET /" to the opened socket and reads the input.

If this is the case, your myprog will have to split out the host component (before the /) and pass that to gethostbyname, and the remainder of the string should be appended to the GET string.
0
 

Author Comment

by:akohan
ID: 22707976


Yes, that should be it.

Thanks!

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question