Csharp/C#教程:如何在C#中创建UDP服务器?分享


如何在C#中创建UDP服务器?

可能重复:
C#如何制作简单的UDP服务器

我想在C#中创建一个UDP服务器。 我怎么做? 如何自定义它侦听的端口(即1212)?

以下是C#中的示例 :

上述就是C#学习教程:如何在C#中创建UDP服务器?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

/* C# Network Programming by Richard Blum Publisher: Sybex ISBN: 0782141765 */ using System; using System.Net; using System.Net.Sockets; using System.Text; public class UdpSrvrSample { public static void Main() { byte[] data = new byte[1024]; IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050); UdpClient newsock = new UdpClient(ipep); Console.WriteLine("Waiting for a client..."); IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); data = newsock.Receive(ref sender); Console.WriteLine("Message received from {0}:", sender.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); string welcome = "Welcome to my test server"; data = Encoding.ASCII.GetBytes(welcome); newsock.Send(data, data.Length, sender); while(true) { data = newsock.Receive(ref sender); Console.WriteLine(Encoding.ASCII.GetString(data, 0, data.Length)); newsock.Send(data, data.Length, sender); } } } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/989296.html

(0)
上一篇 2021年12月23日
下一篇 2021年12月23日

精彩推荐