国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#中使用UDP通信實例

2020-01-24 02:31:32
字體:
來源:轉載
供稿:網友

網絡通信協議中的UDP通信是無連接通信,客戶端在發送數據前無需與服務器端建立連接,即使服務器端不在線也可以發送,但是不能保證服務器端可以收到數據。本文實例即為基于C#實現的UDP通信。具體功能代碼如下:

服務器端代碼如下:

static void Main(string[] args) {   UdpClient client = null;   string receiveString = null;   byte[] receiveData = null;   //實例化一個遠程端點,IP和端口可以隨意指定,等調用client.Receive(ref remotePoint)時會將該端點改成真正發送端端點   IPEndPoint remotePoint = new IPEndPoint(IPAddress.Any, 0);   while (true)   {  client = new UdpClient(11000);  receiveData = client.Receive(ref remotePoint);//接收數據  receiveString = Encoding.Default.GetString(receiveData);  Console.WriteLine(receiveString);  client.Close();//關閉連接   } }

客戶端代碼如下:

static void Main(string[] args) {   string sendString = null;//要發送的字符串   byte[] sendData = null;//要發送的字節數組   UdpClient client = null;   IPAddress remoteIP = IPAddress.Parse("127.0.0.1");   int remotePort = 11000;   IPEndPoint remotePoint = new IPEndPoint(remoteIP, remotePort);//實例化一個遠程端點   while (true)   {  sendString = Console.ReadLine();  sendData = Encoding.Default.GetBytes(sendString);  client = new UdpClient();  client.Send(sendData, sendData.Length, remotePoint);//將數據發送到遠程端點  client.Close();//關閉連接   } }

程序最終運行效果如下:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰顺县| 应城市| 曲麻莱县| 兴山县| 嘉兴市| 雷州市| 买车| 奇台县| 中卫市| 浑源县| 富民县| 琼中| 兴安县| 铁力市| 若羌县| 漳平市| 那曲县| 松原市| 景德镇市| 德格县| 双江| 瓦房店市| 通山县| 甘洛县| 开封县| 临武县| 麻栗坡县| 石渠县| 巴彦淖尔市| 民县| 太和县| 江川县| 桓台县| 仁布县| 满洲里市| 德钦县| 康定县| 威宁| 叶城县| 甘孜| 灌阳县|