我正在尝试制作一个基于UDP的消息传递应用程序,并使其能够同时连续发送和接收。我不知道如何做到这一点,我已经尝试了一些东西。下面是我到目前为止的代码,谁能指出我做错了什么,或者我需要添加什么?谢谢。
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
https://stackoverflow.com/questions/51812925
复制相似问题