F# Async problem.

Posted by chrisdew on Stack Overflow See other posts from Stack Overflow or by chrisdew
Published on 2010-06-13T06:39:33Z Indexed on 2010/06/13 6:42 UTC
Read the original article Hit count: 357

Filed under:
|
|

Hi, I've written a dummy http server as an exercise in F#.

I'm using Mono 2.4.4 on Ubuntu 10.04 x86_64, with MonoDevelop.

The following code fails to compile with the error:

Error FS0039: The field, constructor or member 'Spawn' is not defined (FS0039)

Could someone try this in VisualStudio please, I don't know whether this is a Mono problem, or my problem.

I have tried several Async examples from the F# book, and they also all produce similar messages about Async.* methods.

Thanks,

Chris.

#light

open System
open System.IO
open System.Threading
open System.Net
open System.Net.Sockets
open Microsoft.FSharp.Control.CommonExtensions

printfn "%s" "Hello World!"

let headers = System.Text.Encoding.ASCII.GetBytes("HTTP/1.0 200 OK\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: 37\r\nDate: Sun, 13 Jun 2010 05:30:00 GMT\r\nServer: FSC/0.0.1\r\n\r\n")
let content = System.Text.Encoding.ASCII.GetBytes("<html><body>Hello World</body></html>")

let serveAsync (client : TcpClient) =
    async { let out = client.GetStream()
            do! out.AsyncWrite(headers)
            do! Async.Sleep 3000
            do! out.AsyncWrite(content)
            do out.Close()
            }

let http_server (ip, port) = 
    let server = new TcpListener(IPAddress.Parse(ip),port)
    server.Start()
    while true do 
        let client = server.AcceptTcpClient()
        printfn "new client"
        Async.Spawn (serveAsync client)

http_server ("0.0.0.0", 1234)

© Stack Overflow or respective owner

Related posts about F#

Related posts about asynchronous