F# -- TCP/IP小测试
生活随笔
收集整理的這篇文章主要介紹了
F# -- TCP/IP小测试
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
下面是服務(wù)器端代碼:
// Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. open System.Net.Sockets open System.Net open System.IO open System open System.Collections.Generictype System.Net.Sockets.TcpListener withmember this.AsyncAcceptTcpClient() =Async.FromBeginEnd(this.BeginAcceptTcpClient,this.EndAcceptTcpClient)type Server() =classmember this.Start() = let tcpListener = new TcpListener(System.Net.IPAddress.Loopback, 4242)tcpListener.Start()while(true) doif(tcpListener.Pending()) then let proc =async{trylet! client = tcpListener.AsyncAcceptTcpClient()let strPipe = client.GetStream()let strReader = new StreamReader(strPipe)let strWriter = new StreamWriter(strPipe)while(true) do let buffer = Array.create 216 0uylet read = strPipe.Read(buffer,0,216)let allText = System.Text.Encoding.UTF8.GetString(buffer,0,read)printfn "Recieve from client: %s" (allText)strWriter.Write("Got From Server:")allText |> strWriter.WriteLinestrWriter.Flush()with _ -> printfn "丟失連接,重新連接吧"}proc |> Async.Startend [<EntryPoint>] let main argv = (new Server()).Start()0 // return an integer exit code客戶端代碼:
// Learn more about F# at http://fsharp.net // See the 'F# Tutorial' project for more help. open System.Windows.Forms open System open System.Drawing open System.Net.Sockets open System.IO let form = new Form()form.Text <- "Test" let input = new System.Windows.Forms.TextBox() input.Multiline <- true input.Dock <- DockStyle.Top input.Height <- form.Height / 10 * 2 form.Controls.Add(input)let output = new System.Windows.Forms.TextBox() output.Multiline <- true output.ReadOnly <- true output.Dock <- DockStyle.Bottom output.Height <- form.Height / 10 * 6 output.ScrollBars <- ScrollBars.Both output.ScrollToCaret()let client = new TcpClient() client.Connect("172.0.0.1",4242)let sr = new StreamReader(client.GetStream()) let sw = new StreamWriter(client.GetStream()) form.Controls.Add(output) //發(fā)送消息 let keyUp() = //多于 一行 ,回車 if(input.Lines.Length > 1) thenlet text = input.Textif (text <> null && text <> "") thentrysw.WriteLine(text)sw.Flush() with err ->printfn "Server error" input.Text <- ""input.KeyUp.Add(fun _ -> keyUp())let getBackStr() =let getStr() = while(true) dolet text = sr.ReadLine()if(text<>"" && text<>null)thenprintfn "%s"textoutput.Text <- output.Text + textoutput.AppendText(System.Environment.NewLine)//另起一個(gè)專門負(fù)責(zé)接受服務(wù)器回復(fù)的Threadlet thread = new Threading.Thread(new Threading.ThreadStart(fun _ -> getStr()))thread.Start()form.Load.Add(fun _ -> getBackStr()) form.SizeChanged.Add(fun _ -> input.Height <- form.Height / 10 * 2output.Height <- form.Height / 10 * 6) form.Closing.Add(fun _ -> Application.Exit()) [<EntryPoint>] let main argv = do Application.Run(form)0 // return an integer exit code
?更新一下, 支持異步運(yùn)行。 多個(gè)Form同時(shí)運(yùn)行。 下個(gè)版本實(shí)現(xiàn)多個(gè)之間交流。。
轉(zhuǎn)載于:https://www.cnblogs.com/FsharpZack/archive/2012/12/28/2837418.html
總結(jié)
以上是生活随笔為你收集整理的F# -- TCP/IP小测试的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GridControl详解(三)列数据的
- 下一篇: MySQL学习笔记7:基本查询