Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A little web-server in OCaml (submission and question)
Index -> Programming, General Programming -> Functional Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
PaulButler




PostPosted: Tue Aug 07, 2007 1:05 pm   Post subject: A little web-server in OCaml (submission and question)

I have been playing with sockets in OCaml. Eventually, I would like to build a very lightweight web server just to play around with.

Anyway, here is my code:

Ocaml:

(* Simple OCaml Web Server *)

open Unix

(* Port to listen on *)
let port = 9090

let listen_sock = socket PF_INET SOCK_STREAM 0

(* Sends the gives string to the given socket *)
(* Unix.file_descr -> string -> unit *)
let send_string sock str =
        let len = String.length str in
        let _ = send sock str 0 len [] in
        ()

(* Main loop *)
let rec do_listen () =
        let (client_sock, _) = accept listen_sock in
        send_string client_sock "HTTP/1.1 200/OK\nContent-type: text/html\n\n";
        send_string client_sock "<h1>Hello World</h1>";
        close client_sock;
        do_listen ()

(* Listen on port *)
let _ =
        bind listen_sock (ADDR_INET (inet_addr_of_string "0.0.0.0", port));
        listen listen_sock 8;
        do_listen ()


For some reason, it is not very reliable - I think close might be the wrong way to end the connection. When I try it in FireFox, the result is usually "Connection reset by server", but if I refresh enough I do see the "Hello World" once in a while. If you remove the close command, the page works every time, but the connection isn't closed so the browser thinks the page is still loading. Maybe I need to send an EOF? Using the content-length header is not desirable, since it doesn't work for dynamic content (unless you buffer the whole thing...).

Even if it did work, it would be pretty useless without threads, but I am trying to take it one step at a time.

The send_string function looks a bit hackish, but it seemed like the best way to get around a warning caused by using the semicolon after a function that doesn't return the unit value. In retrospect, I probably should have just lived with the warning, but I wanted to see how easily I could get around it.
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sat Aug 11, 2007 4:56 pm   Post subject: RE:A little web-server in OCaml (submission and question)

Here's a tip that will solve a lot of ugly issues you will end up with once you have multi-threaded your application: Use Scala instead of Ocaml. Scala implements Erlang's fancy Actor-messaging framework. It guarantees no deadlocks and it makes managing multi-threaded applications a lot simpler.

As for the HTTP protocol, telnet into an existing server and see what kind of response they return upon retrieving a web page successfully. Or you could simply read up on the HTTP RFC's: here
PaulButler




PostPosted: Sun Aug 12, 2007 3:45 pm   Post subject: RE:A little web-server in OCaml (submission and question)

Scala is interesting, if I run into problems with threads I will consider using it. I've been meaning to give the Lift web framework (which uses Scala) a try, but I want to play with ocaml some more first.

Thanks for the telnet suggestion, I didn't think of that. I think I found the problem - I am closing the connection without first reading the input, so it could be that the browser did not like having the connection close before it is done sending all the headers. Interestingly, the server seems to work fine under Linux. It turns out that HTTP just signals the end of a request by closing the connection, so I had that part right.

Ocaml has an establish_server command that handles all the threading and low-level stuff, but it isn't supported under Windows. Now that I am running linux on my main PC, I can forget about the listen_sock.
Display posts from previous:   
   Index -> Programming, General Programming -> Functional Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: