对不起,很长的帖子。我想TcpListener
用来侦听端口,处理不同(后台)线程中传入连接请求的繁重工作,然后在准备好时将响应发送回客户端。我在 MSDN 上阅读了很多代码和示例,并为服务器提出了以下实现。
对于以下所有实现,请假设以下变量:
let sva = "127.0.0.1"
let dspt = 32000
let respondToQuery (ns_ : NetworkStream) (bta_ : byte array) : unit =
// DO HEAVY LIFTING
()
实现1(普通的同步服务器;我从这个 MSDN 页面翻译的代码)
let runSync () : unit =
printfn "Entering runSync ()"
let (laddr : IPAddress) = IPAddress.Parse sva
let (svr : TcpListener) = new TcpListener (laddr, dspt)
try
svr.Start ()
let (bta : byte array) = Array.zeroCreate<byte> imbs
while true do
printfn "Listening on port %d at %s" dspt sva
let (cl : TcpClient) = svr.AcceptTcpClient ()
let (ns : NetworkStream) = cl.GetStream ()
respondToQuery ns bta
cl.Close ()
svr.Stop ()
printfn "Exiting runSync () normally"
with
| excp ->
printfn "Error: %s" excp.Message
printfn "Exiting runSync () with error"
实现2(我在这个 MSDN 页面上的代码翻译)
let runAsyncBE () : unit =
printfn "Entering runAsyncBE ()"
let (tcc : ManualResetEvent) = new ManualResetEvent (false)
let (bta : byte array) = Array.zeroCreate<byte> imbs
let datcc (ar2_ : IAsyncResult) : unit =
let tcpl2 = ar2_.AsyncState :?> TcpListener
let tcpc2 = tcpl2.EndAcceptTcpClient ar2_
let (ns2 : NetworkStream) = tcpc2.GetStream ()
respondToQuery ns2 bta
tcpc2.Close ()
tcc.Set () |> ignore
let rec dbatc (tcpl2_ : TcpListener) : unit =
tcc.Reset () |> ignore
printfn "Listening on port %d at %s" dspt sva
tcpl2_.BeginAcceptTcpClient (new AsyncCallback (datcc), tcpl2_) |> ignore
tcc.WaitOne () |> ignore
dbatc tcpl2_
let (laddr : IPAddress) = IPAddress.Parse sva
let (tcpl : TcpListener) = new TcpListener (laddr, dspt)
try
tcpl.Start ()
dbatc tcpl
printfn "Exiting try block"
printfn "Exiting runAsyncBE () normally"
with
| excp ->
printfn "Error: %s" excp.Message
printfn "Exiting runAsyncBE () with error"
实现3(我的实现基于异步工作流的 MSDN 页面)
let runAsyncA () : unit =
printfn "Entering runAsyncA ()"
let (laddr : IPAddress) = IPAddress.Parse sva
let (svr : TcpListener) = new TcpListener (laddr, dspt)
try
svr.Start ()
let (bta : byte array) = Array.zeroCreate<byte> imbs
while true do
printfn "Listening on port %d at %s" dspt sva
let (cl : TcpClient) = svr.AcceptTcpClient ()
let (ns : NetworkStream) = cl.GetStream ()
async {respondToQuery ns bta} |> Async.RunSynchronously
cl.Close ()
svr.Stop ()
printfn "Exiting runAsyncA () normally"
with
| excp ->
printfn "Error: %s" excp.Message
printfn "Exiting runAsyncA () with error"
现在,根据我对 MSDN 文档的阅读,我认为那Implementation 3
会是最快的。但是当我用来自多台机器的多个查询访问服务器时,它们都以大致相同的速度运行。这让我相信我一定做错了什么。
在后台执行繁重的工作,并在完成后将响应返回给客户端,同时允许另一个客户端可能也连接并在另一个后台线程中启动另一个任务,是不是Implementation 2
或者Implementation 3
是“正确”的方法?TcpListener
如果没有,您能告诉我应该阅读哪些课程(或教程)吗?