{"id":728,"date":"2007-04-14T04:00:00","date_gmt":"2007-04-13T19:00:00","guid":{"rendered":"https:\/\/ecoop.net\/memo\/2007-04-14-4.html"},"modified":"2017-02-20T11:39:16","modified_gmt":"2017-02-20T02:39:16","slug":"2007-04-14-4","status":"publish","type":"post","link":"https:\/\/www.ecoop.net\/memo\/archives\/2007-04-14-4.html","title":{"rendered":"C#.NET \u3067 UDP \u901a\u4fe1\u3092\u7c21\u5358\u306b\u884c\u3046\u306b\u306f"},"content":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u306fC#\u3067\u306e\u5b9f\u88c5\u4f8b\u3067\u3059\u3002<br \/>\n\u3082\u3057PC\u304b\u3089\u306e\u63a5\u7d9a\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u306e\u63a5\u7d9a\u65b9\u6cd5\u3092\u77e5\u308a\u305f\u3044\u65b9\u306f\u3001<a href=\" https:\/\/www.ecoop.net\/memo\/archives\/udp-connection-on-command-line.html\">\u3053\u3061\u3089\u306e\u8a18\u4e8b<\/a>\u3092\u3069\u3046\u305e\u3002<\/p>\n<blockquote><p>Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);<br \/>\nsocket.SendTo(&#8230;);<br \/>\nsocket.ReceiveFrom(&#8230;);\n<\/p><\/blockquote>\n<p>\u306e\u3088\u3046\u306b System.Net.Sockets.Socket \u3092\u4f7f\u3046\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u304c\u3001System.Net.Sockets.UdpClient \u3092\u4f7f\u3046\u3068\u7c21\u5358\u306b UDP \u901a\u4fe1\u3092\u884c\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<blockquote><p>\/\/ \u5fc5\u8981\u306a\u5ba3\u8a00<br \/>\nusing System.Net;<br \/>\nusing System.Net.Sockets;\n<\/p><\/blockquote>\n<blockquote><p>private void DoSend(){<br \/>\n\/\/ \u9001\u4fe1\u5143\u3068\u3059\u308b\u30dd\u30fc\u30c8<br \/>\nint localPort = 1234;<br \/>\n&nbsp;<br \/>\nUdpClient client = new UdpClient(localPort);<br \/>\n&nbsp;<br \/>\n\/\/ UDP \u30d1\u30b1\u30c3\u30c8\u306e\u9001\u4fe1\u5148<br \/>\nIPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse(&#8220;192.168.2.1&#8221;), 5678);<br \/>\n\/\/ \u9001\u4fe1\u3059\u308b\u5185\u5bb9(\u30d0\u30a4\u30c8\u914d\u5217\u3067\u6307\u5b9a)<br \/>\nbyte[] msg = Encoding.ASCII.GetBytes(&#8220;Hello, world\\n&#8221;);<br \/>\n&nbsp;<br \/>\n\/\/ \u9001\u4fe1\u3059\u308b<br \/>\nclient.Send(msg, msg.Length, remoteEP);<br \/>\n\/\/ \/\/ \u540c\u3058\u5834\u6240\u306b\u9001\u4fe1\u3057\u7d9a\u3051\u308b\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u63a5\u7d9a\u3068\u9001\u4fe1\u3092\u308f\u3051\u3066\u3082\u3088\u3044<br \/>\n\/\/ client.Connect(remoteEP);<br \/>\n\/\/ client.Send(msg, msg.Length);<br \/>\n}\n<\/p><\/blockquote>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308c\u3070\u3001\u53d7\u4fe1\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<blockquote><p>private void DoReceive(){<br \/>\nint localPort = 5678;<br \/>\nUdpClient client = new UdpClient(localPort);<br \/>\n&nbsp;<br \/>\n\/\/ \u9001\u4fe1\u5143\u3002\u4efb\u610f\u306eIP\u30a2\u30c9\u30ec\u30b9\u3001\u4efb\u610f\u306e\u30dd\u30fc\u30c8\u304b\u3089\u8a31\u53ef<br \/>\nIPEndPoint remoteEP = IPEndPoint(IPAddress.Any, 0);<br \/>\n&nbsp;<br \/>\n\/\/ \u53d7\u4fe1\u3059\u308b\u307e\u3067\u5f85\u3061\u7d9a\u3051\u308b<br \/>\nbyte[] res = client.Receive(ref remoteEP);<br \/>\n&nbsp;<br \/>\n\/\/ \u30d0\u30a4\u30c8\u914d\u5217\u304b\u3089 ASCII \u6587\u5b57\u5217\u306b\u5909\u63db\u3057\u3066\u8868\u793a<br \/>\nSystem.Console.Write(<br \/>\n&nbsp;&nbsp;&nbsp;&#8220;\u9001\u4fe1\u5143:&#8221; + remoteEP + &#8220;\\n&#8221; +  \/\/ \u5b9f\u969b\u306b\u9001\u4fe1\u304c\u884c\u308f\u308c\u305f IP\u30a2\u30c9\u30ec\u30b9, \u30dd\u30fc\u30c8\u304c\u683c\u7d0d\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u4e00\u7dd2\u306b\u8868\u793a<br \/>\n&nbsp;&nbsp;&nbsp;&#8220;\u53d7\u4fe1\u5185\u5bb9:&#8221;+Encoding.ASCII.GetString(res)<br \/>\n&nbsp;&nbsp;);<br \/>\n}\n<\/p><\/blockquote>\n<p>TCP \u7248\u306e TcpClient \u3082\u3042\u308a\u307e\u3059\u3002<br \/>\n&nbsp;<br \/>\n\u53c2\u8003:<br \/>\n&#8211; <a href=\"http:\/\/msdn2.microsoft.com\/ja-jp\/library\/system.net.sockets.udpclient(VS.80).aspx\">UdpClient \u30af\u30e9\u30b9 (System.Net.Sockets)<\/a><br \/>\n&#8211; <a href=\"http:\/\/msdn2.microsoft.com\/ja-jp\/library\/system.net.sockets.tcpclient(VS.80).aspx\">TcpClient \u30af\u30e9\u30b9<\/a><br \/>\n&#8211; <a href=\"http:\/\/msdn2.microsoft.com\/ja-jp\/library\/system.net.sockets.socket(VS.80).aspx\">Socket \u30af\u30e9\u30b9(System.Net.Sockets)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3053\u306e\u8a18\u4e8b\u306fC#\u3067\u306e\u5b9f\u88c5\u4f8b\u3067\u3059\u3002 \u3082\u3057PC\u304b\u3089\u306e\u63a5\u7d9a\u3001\u30b3\u30de\u30f3\u30c9\u30e9\u30a4\u30f3\u304b\u3089\u306e\u63a5\u7d9a\u65b9\u6cd5\u3092\u77e5\u308a\u305f\u3044\u65b9\u306f\u3001\u3053\u3061\u3089\u306e\u8a18\u4e8b\u3092\u3069\u3046\u305e\u3002 Socket socket = new Socket(AddressFamily.InterNetw &hellip; <a href=\"https:\/\/www.ecoop.net\/memo\/archives\/2007-04-14-4.html\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">C#.NET \u3067 UDP \u901a\u4fe1\u3092\u7c21\u5358\u306b\u884c\u3046\u306b\u306f<\/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":[],"tags":[76],"class_list":["post-728","post","type-post","status-publish","format-standard","hentry","tag-c"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/728","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/comments?post=728"}],"version-history":[{"count":1,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/728\/revisions"}],"predecessor-version":[{"id":1206,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/posts\/728\/revisions\/1206"}],"wp:attachment":[{"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/media?parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/categories?post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ecoop.net\/memo\/wp-json\/wp\/v2\/tags?post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}