我在F#中编写了一个虚拟的http服务器作为练习.
我在Ubuntu 10.04 x86_64和MonoDevelop上使用Mono 2.4.4.
以下代码无法使用错误进行编译:
Error FS0039: The field, constructor or member 'Spawn' is not defined (FS0039)
Run Code Online (Sandbox Code Playgroud)
有人可以在VisualStudio中尝试这个,我不知道这是单声道问题,还是我的问题.
我已经尝试了F#书中的几个Async示例,它们也都生成了关于Async.*方法的类似消息.
谢谢,
克里斯.
#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)
Run Code Online (Sandbox Code Playgroud)
Spawn现在被称为Start(自几年前出版了几本书以来,库API已经发生了一些变化).
查看文档
http://msdn.microsoft.com/en-us/library/ee370232.aspx
| 归档时间: |
|
| 查看次数: |
506 次 |
| 最近记录: |