C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類實(shí)例
本文實(shí)例講述了C#實(shí)現(xiàn)的UDP收發(fā)請(qǐng)求工具類。分享給大家供大家參考,具體如下:
初始化:
ListeningPort = int.Parse(ConfigurationManager.AppSettings["ListeningPort"]); SendingPort = int.Parse(ConfigurationManager.AppSettings["SendingPort"]); SendingIp = ConfigurationManager.AppSettings["SendingIp"];
監(jiān)聽:
public static void Listen() { Task.Run(() => { var done = false; var listener = new UdpClient(ListeningPort); var groupEP = new IPEndPoint(IPAddress.Any, ListeningPort); string received_data; byte[] receive_byte_array; try { _log.Error("############Service started###########"); while (true) { receive_byte_array = listener.Receive(ref groupEP); Console.WriteLine("Received a broadcast from {0}", groupEP.ToString()); received_data = Encoding.UTF8.GetString(receive_byte_array, 0, receive_byte_array.Length); ParseCommand(received_data); } } catch (Exception e) { _log.Error(e); Console.WriteLine(e.ToString()); } _log.Error("############Service stopped###########"); }); }
發(fā)送:
public static void SendCommand(string xmlCmd) { try { var sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); var sending_end_point = new IPEndPoint(IPAddress.Parse(SendingIp), SendingPort); var send_buffer = Encoding.UTF8.GetBytes(xmlCmd); sending_socket.SendTo(send_buffer, sending_end_point); _log.Info("[COMMAND SENT] : " + xmlCmd); } catch (Exception ex) { _log.Error(ex); } }
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#窗體操作技巧匯總》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》、《C#數(shù)組操作技巧總結(jié)》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)給DevExpress中GridView表格指定列添加進(jìn)度條顯示效果,感興趣的小伙伴可以嘗試一下2022-06-06C#中通過API實(shí)現(xiàn)的打印類 實(shí)例代碼
這篇文章介紹了,C#中通過API實(shí)現(xiàn)的打印類 實(shí)例代碼,有需要的朋友可以參考一下2013-08-08C#操作圖片讀取和存儲(chǔ)SQLserver實(shí)現(xiàn)代碼
用C#將Image轉(zhuǎn)換成byte[]并插入數(shù)據(jù)庫/將圖片數(shù)據(jù)從SQLserver中取出來并顯示到pictureBox控件上,接下來將為你詳細(xì)介紹下實(shí)現(xiàn)步驟,感興趣的你可以參考下2013-03-03C#設(shè)置程序開機(jī)啟動(dòng)的實(shí)現(xiàn)示例
本文主要介紹了C#設(shè)置程序開機(jī)啟動(dòng)的實(shí)現(xiàn)示例,可以通過修改注冊(cè)表將啟動(dòng)信息寫入注冊(cè)表來實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01C#和JavaScript實(shí)現(xiàn)交互的方法
最近做一個(gè)小項(xiàng)目不可避免的需要前端腳本與后臺(tái)進(jìn)行交互。由于是在asp.net中實(shí)現(xiàn),故問題演化成asp.net中jiavascript與后臺(tái)c#如何進(jìn)行交互。2015-05-05WPF利用ValueConverter實(shí)現(xiàn)值轉(zhuǎn)換器
值轉(zhuǎn)換器在WPF開發(fā)中是非常常見的,值轉(zhuǎn)換器可以幫助我們很輕松地實(shí)現(xiàn),界面數(shù)據(jù)展示的問題。本文將通過WPF?ValueConverter實(shí)現(xiàn)簡(jiǎn)單的值轉(zhuǎn)換器,希望對(duì)大家有所幫助2023-03-03