首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用F#异步接收和发送UDP?

如何使用F#异步接收和发送UDP?
EN

Stack Overflow用户
提问于 2018-08-13 05:15:57
回答 1查看 322关注 0票数 3

我正在尝试制作一个基于UDP的消息传递应用程序,并使其能够同时连续发送和接收。我不知道如何做到这一点,我已经尝试了一些东西。下面是我到目前为止的代码,谁能指出我做错了什么,或者我需要添加什么?谢谢。

代码语言:javascript
运行
复制
open System
open System.Net
open System.Net.Sockets
open System.Text

printfn "Receive port: "
let receivePort = Console.ReadLine() |> int

let receivingClient = new UdpClient(receivePort)

let ReceivingIpEndPoint = new IPEndPoint(IPAddress.Any, 0)

printfn "Send address: "
let sendAddress = IPAddress.Parse(Console.ReadLine())

printfn "Send port: "
let sendPort = Console.ReadLine() |> int

let sendingClient = new UdpClient()

let sendingIpEndPoint = new IPEndPoint(sendAddress, sendPort)

let rec loop() =
    let receive = async {
        try
            let! receiveResult = receivingClient.ReceiveAsync() |> Async.AwaitTask
            let receiveBytes = receiveResult.Buffer
            let returnData = Encoding.ASCII.GetString(receiveBytes)
            printfn "%s" returnData
        with
            | error -> printfn "%s" error.Message
    }

    receive |> ignore

    printfn "Send message: "
    let (sendBytes: byte array) = Encoding.ASCII.GetBytes(Console.ReadLine())

    try
        sendingClient.Send(sendBytes, sendBytes.Length, sendingIpEndPoint) |> ignore
    with
        | error -> printfn "%s" error.Message

    loop()

loop()

Console.Read() |> ignore
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51812925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档