IPEndPoint iep = new IPEndPoint(IPAddress.Loopback, 8001);
UdpClient client = new UdpClient(iep);
Console.WriteLine("在{0}监听...", iep);
IPEndPoint romoteEp = null;
var t1 = Task.Run(() => {
while (true)
{
var result = client.Receive(ref romoteEp);
Console.WriteLine("来自{0}:{1}", romoteEp, Encoding.Unicode.GetString(result));
}
});
var t2 = Task.Run(async () =>
{
while (true)
{
byte[] bytes = Encoding.Unicode.GetBytes(Console.ReadLine());
await client.SendAsync(bytes, bytes.Length, "127.0.0.1", 8002);
}
});
t1.Wait();
t1.Wait();
评论