{"id":545,"date":"2016-11-15T21:57:36","date_gmt":"2016-11-15T21:57:36","guid":{"rendered":"647bba344396e7c8170902bcf2e15551"},"modified":"2016-11-15T21:57:36","modified_gmt":"2016-11-15T21:57:36","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/545","title":{"rendered":"C# UDP\u7ec4\u64ad"},"content":{"rendered":"<p><b><span style=\"font-size:14px;\">\/\/\u7ec4\u64ad\u4ee3\u7801<\/span><\/b><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Collections.Generic;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Linq;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Text;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Net;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Net.Sockets;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Threading;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">namespace<\/span><span style=\"font-size:14px;\"> <\/span><span class=\"hljs-title\" style=\"font-size:14px;\">Test<\/span><span style=\"font-size:14px;\"> {<\/span><\/p>\n<pre class=\"prettyprint hljs cs\"><span style=\"font-size:14px;\">    <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">class<\/span><span style=\"font-size:14px;\"> <\/span><span class=\"hljs-title\" style=\"font-size:14px;\">Program<\/span><span style=\"font-size:14px;\">     {\r\n        <\/span><span class=\"hljs-function\" style=\"font-size:14px;\"><span class=\"hljs-keyword\" style=\"font-size:14px;\">static<\/span> <span class=\"hljs-keyword\" style=\"font-size:14px;\">void<\/span> <span class=\"hljs-title\" style=\"font-size:14px;\">Main<\/span>(<span class=\"hljs-params\" style=\"font-size:14px;\"><span class=\"hljs-keyword\" style=\"font-size:14px;\">string<\/span>[] args<\/span>)\r\n        <\/span><span style=\"font-size:14px;\">{\r\n            UdpClient client = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> UdpClient(<\/span><span class=\"hljs-number\" style=\"font-size:14px;\">5566<\/span><span style=\"font-size:14px;\">);\r\n            client.JoinMulticastGroup(IPAddress.Parse(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"234.5.6.7\"<\/span><span style=\"font-size:14px;\">));\r\n            IPEndPoint multicast = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> IPEndPoint(IPAddress.Parse(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"234.5.6.7\"<\/span><span style=\"font-size:14px;\">), <\/span><span class=\"hljs-number\" style=\"font-size:14px;\">7788<\/span><span style=\"font-size:14px;\">);\r\n            <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">byte<\/span><span style=\"font-size:14px;\">[] buf = Encoding.Default.GetBytes(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"Hello from multicast\"<\/span><span style=\"font-size:14px;\">);\r\n            Thread t = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> Thread(<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> ThreadStart(RecvThread));\r\n            t.IsBackground = <\/span><span class=\"hljs-literal\" style=\"font-size:14px;\">true<\/span><span style=\"font-size:14px;\">;\r\n            t.Start();\r\n            <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">while<\/span><span style=\"font-size:14px;\"> (<\/span><span class=\"hljs-literal\" style=\"font-size:14px;\">true<\/span><span style=\"font-size:14px;\">)\r\n            {\r\n                client.Send(buf, buf.Length, multicast);\r\n                Thread.Sleep(<\/span><span class=\"hljs-number\" style=\"font-size:14px;\">1000<\/span><span style=\"font-size:14px;\">);\r\n            }\r\n        }\r\n\r\n        <\/span><span class=\"hljs-function\" style=\"font-size:14px;\"><span class=\"hljs-keyword\" style=\"font-size:14px;\">static<\/span> <span class=\"hljs-keyword\" style=\"font-size:14px;\">void<\/span> <span class=\"hljs-title\" style=\"font-size:14px;\">RecvThread<\/span>()\r\n        <\/span><span style=\"font-size:14px;\">{\r\n            UdpClient client = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> UdpClient(<\/span><span class=\"hljs-number\" style=\"font-size:14px;\">7788<\/span><span style=\"font-size:14px;\">);\r\n            client.JoinMulticastGroup(IPAddress.Parse(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"234.5.6.7\"<\/span><span style=\"font-size:14px;\">));\r\n            IPEndPoint multicast = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> IPEndPoint(IPAddress.Parse(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"234.5.6.7\"<\/span><span style=\"font-size:14px;\">), <\/span><span class=\"hljs-number\" style=\"font-size:14px;\">5566<\/span><span style=\"font-size:14px;\">);\r\n            <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">while<\/span><span style=\"font-size:14px;\"> (<\/span><span class=\"hljs-literal\" style=\"font-size:14px;\">true<\/span><span style=\"font-size:14px;\">)\r\n            {\r\n                <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">byte<\/span><span style=\"font-size:14px;\">[] buf = client.Receive(<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">ref<\/span><span style=\"font-size:14px;\"> multicast);\r\n                <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">string<\/span><span style=\"font-size:14px;\"> msg = Encoding.Default.GetString(buf);\r\n                Console.WriteLine(msg);\r\n            }\r\n        }\r\n    }\r\n}<\/span><\/pre>\n<p><span style=\"font-size:14px;\">\u7ec4\u64ad\u5730\u5740\u4e3a 224.0.0.0 ~ 239.255.255.255\uff0c\u5176\u4e2d 224.0.0.0~224.255.255.255 \u4e0d\u5efa\u8bae\u5728\u7528\u6237\u7a0b\u5e8f\u4e2d\u4f7f\u7528\uff0c\u56e0\u4e3a\u5b83\u4eec\u4e00\u822c\u90fd\u6709\u7279\u6b8a\u7528\u9014\u3002 <\/span><\/p>\n<p>&nbsp;<\/p>\n<p><b><span style=\"font-size:14px;\">\/\/\u5e7f\u64ad\u4ee3\u7801<\/span><\/b><\/p>\n<p><span style=\"font-size:14px;\"> <\/span><\/p>\n<p><span style=\"font-size:14px;\">using System;<\/span><br \/>\n<span style=\"font-size:14px;\">using System.Collections.Generic;<\/span><br \/>\n<span style=\"font-size:14px;\">using System.Linq;<\/span><br \/>\n<span style=\"font-size:14px;\">using System.Text;<\/span><br \/>\n<span style=\"font-size:14px;\">using System.Net;<\/span><br \/>\n<span style=\"font-size:14px;\">using System.Net.Sockets;<\/span><br \/>\n<span style=\"font-size:14px;\">using System.Threading;<\/span><br \/>\n<span style=\"font-size:14px;\">namespace ConsoleApplication1<\/span><br \/>\n<span style=\"font-size:14px;\">{<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp; class Program<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp; {<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static void Main(string[] args)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n Socket socket = new Socket(AddressFamily.InterNetwork,<br \/>\nSocketType.Dgram, ProtocolType.IP);\/\/\u53ea\u80fd\u7528 P\u534f\u8bae\u53d1\u9001\u5e7f\u64ad\uff0c\u6240\u4ee5ProtocolType\u8bbe\u7f6e\u4e3a P<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IPEndPoint iep = new IPEndPoint(IPAddress.Broadcast, 10881);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8ba9\u5176\u81ea\u52a8\u63d0\u4f9b\u5b50\u7f51\u4e2d\u7684IP\u5730\u5740<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n socket.SetSocketOption(SocketOptionLevel.Socket,<br \/>\nSocketOptionName.Broadcast, 1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8bbe\u7f6ebroadcast\u503c\u4e3a1\uff0c\u5141\u8bb8\u5957\u63a5\u5b57\u53d1\u9001\u5e7f\u64ad\u4fe1\u606f<\/span><\/p>\n<p><span style=\"font-size:14px;\"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Int32&nbsp; i = 0;<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (true)<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Thread.Sleep(1000);<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; string info = \"Hello\";<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] bInfo = Encoding.UTF8.GetBytes(info);<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; socket.SendTo(bInfo, iep);<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<\/span><br \/>\n<span style=\"font-size:14px;\">&nbsp;&nbsp;&nbsp; }<\/span><\/p>\n<p><span style=\"font-size:14px;\">}<\/span><\/p>\n<p><span style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n<p><span style=\"font-size:14px;\"><br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p><b><span style=\"font-size:14px;\">\/\/\u7ec4\u64ad\u4ee3\u7801<\/span><\/b><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/><\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Collections.Generic;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/><\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Linq;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Text;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/><\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Net;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Net.Sockets;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/><\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">using<\/span><span style=\"font-size:14px;\"> System.Threading;<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\"><br \/><\/span><\/p>\n<p><span class=\"hljs-keyword\" style=\"font-size:14px;\">namespace<\/span><span class=\"hljs-title\" style=\"font-size:14px;\">Test<\/span><span style=\"font-size:14px;\"> {<\/span><\/p>\n<pre class=\"prettyprint hljs cs\"><span class=\"hljs-keyword\" style=\"font-size:14px;\">class<\/span><span class=\"hljs-title\" style=\"font-size:14px;\">Program<\/span><span style=\"font-size:14px;\">     {\r\n        <\/span><span class=\"hljs-function\" style=\"font-size:14px;\"><span class=\"hljs-keyword\" style=\"font-size:14px;\">static<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">void<\/span><span class=\"hljs-title\" style=\"font-size:14px;\">Main<\/span>(<span class=\"hljs-params\" style=\"font-size:14px;\"><span class=\"hljs-keyword\" style=\"font-size:14px;\">string<\/span>[] args<\/span>)\r\n        <\/span><span style=\"font-size:14px;\">{\r\n            UdpClient client = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> UdpClient(<\/span><span class=\"hljs-number\" style=\"font-size:14px;\">5566<\/span><span style=\"font-size:14px;\">);\r\n            client.JoinMulticastGroup(IPAddress.Parse(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"234.5.6.7\"<\/span><span style=\"font-size:14px;\">));\r\n            IPEndPoint multicast = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> IPEndPoint(IPAddress.Parse(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"234.5.6.7\"<\/span><span style=\"font-size:14px;\">), <\/span><span class=\"hljs-number\" style=\"font-size:14px;\">7788<\/span><span style=\"font-size:14px;\">);\r\n            <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">byte<\/span><span style=\"font-size:14px;\">[] buf = Encoding.Default.GetBytes(<\/span><span class=\"hljs-string\" style=\"font-size:14px;\">\"Hello from multicast\"<\/span><span style=\"font-size:14px;\">);\r\n            Thread t = <\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> Thread(<\/span><span class=\"hljs-keyword\" style=\"font-size:14px;\">new<\/span><span style=\"font-size:14px;\"> ThreadStart(RecvThread));\r\n   ...<\/span><\/pre>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/545\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[291],"tags":[260,259],"class_list":["post-545","post","type-post","status-publish","format-standard","hentry","category-network","tag-broadcast","tag-upd"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=545"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/545\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}