ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am new to networks and c in linux. I am running ubuntu and I wanted to know if anyone knew of a tutorial that I could use to get information on writing a pgm to open up a web page from the command line in C. Thanks in advance.
I take it you want to make a program that lets you get web pages from the command line. Is this correct? First, you do not need to do this. There already exists a number of programs that do this exact same thing (man wget or man curl). If you're still intent on doing this, then check out the libcurl page ( http://curl.haxx.se/libcurl/c/ ). There are other options then libcurl though.
You should listen to bigearsbillys very good advice. Use netcat (nc) as your web server on localhost, until you see that you are issuing the correct HTTP request. In fact use your own web server, once you have accomplished the very basics. It will allow you to control the responses to cover all sorts of circumstances, and also provide you with acces to log files which can be revealing.
--- rod.
Okay, so taking my own and bigearsbilly's advice, I built and ran your code against netcat, and against an Apache webserver on localhost, and it worked fine for me. Running seamonkey against netcat on localhost, I see it adding several additional HTTP headers, which I would speculate that Google requires before it will reply.
you may have to be careful.
I haven't grabbed web pages but I have done SOAP stuff, over http with perl.
using fgets expects a line with a newline at the end.
but it may block waiting for input that doesn't come so that may give the appearance of hanging.
because the server will squirt say, 100 bytes and keep the socket open, while fgets is
waiting for the rest of it's input or a newline, which may not come.
it's all quite awkward to get right.
what you may need to do is, read the Content-Length: header and fread the correct amount of bytes.
I can't do any checks here because of our firewall and proxy server.
If you are playing about just learning about networks,
maybe use your favourite socket enabled scripting language?
(perl python tcl)
It makes life easier till you get the hang of it.
look up the HTTP specification or look carefully at the terminating characters in a sniffer. in a character-based protocol like HTTP, a specific character sequence indicates end of transmission. i would guess your code appears to hang because the webserver doesn't send anything back to you. (because your request is not properly formatted).
for debugging, watch your program in a sniffer to determine where in your code you are having problems (i.e. the writing functions or the reading functions).
HTTP uses characters \r\n to indicate end of line & end of request. (your code shows \n\n - which might work?)
I think the only required portion of an HTTP GET request is the GET command. I think the other fields are optional. For your fprintf call, try the following string:
Code:
fprintf(fsock,"GET / HTTP/1.0\r\n\r\n");
does that work?
Last edited by jdiggitydogg; 09-23-2007 at 05:09 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.