Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C reading from web server

Options
  • 24-03-2008 6:12pm
    #1
    Registered Users Posts: 23


    hi everybody,
    I am trying to write a simple c program that just opens a txt file on a webserver and writes the contents to a string. I don't how to do this or if its even possible, if anyone can help me out i would appreciate it greatly. Also, I may need to write to the file as well.
    thanks in advance,
    geistmorder


Comments

  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    I wrote a C Socket tutorial there a year ago for my class mates, dunno if it will make sense to you or not but I uploaded it here:

    http://killarneyonline.eu/mystuff/socketprogramming.htm

    Must be viewed in IE to see it properly since it was exported from DOC to HTML. Forgive me for this, its not like me but I don't have time at moment. The Doc version is
    http://killarneyonline.eu/mystuff/socketprogramming.doc

    Hope this helps.

    Oh yes, you will have to send a HTTP get command connecting to port 80 on the webserver if you want to read contents. I've never done it but that might be a for you anyways.

    By the way writing to the file will be more difficult. You will have to open a FTP connection to the webserver and overwrite the file with the new version you have written locally.


  • Closed Accounts Posts: 413 ✭✭sobriquet


    Beejs' guide to network programming is a decent free tutorial and it uses C. It uses the unix socket library which may differ from the winsock.h definitions, I'm not sure.

    Sending a HTTP GET request is pretty straightforward. You'll call send() with a string like "GET /path/path/file.txt HTTP/1.0" and the web server will give you back the file, wrapped in response statuses, like "HTTP/1.0 200 OK" or "HTTP/1.0 404 Not Found" etc which you'll need to parse out (AFAIR, the header is delimited by two carriage return characters).

    Writing to the file could also be done with the HTTP PUT or POST command if the server supports it, or like webmonkey says with FTP. Haven't done either so can't say much on it.


  • Closed Accounts Posts: 448 ✭✭ve


    You can use the Curl library (HTTP) to do this: http://curl.haxx.se/libcurl/c/example.html

    Just make sure before you go ahead and use this that your examiner isn't looking for you to implement this using simple TCP socket programming yourself, where you'd manually process the HTTP Request/Response strings instead of using some higher level of abstraction such as the above library.

    Best of luck ;)


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    there is api on windows, assuming you're writing it for that OS:

    URLDownloadToFile()


Advertisement