{"id":9417,"date":"2018-10-02T17:10:28","date_gmt":"2018-10-02T08:10:28","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=9417"},"modified":"2018-10-03T12:05:29","modified_gmt":"2018-10-03T03:05:29","slug":"system-net-httplistener-%e3%81%8c-net-core-%e3%81%ae%e6%99%82%e3%81%a0%e3%81%91%e3%83%a1%e3%83%a2%e3%83%aa%e3%83%aa%e3%83%bc%e3%82%af%e3%81%a3%e3%81%bd%e3%81%84%e7%8f%be%e8%b1%a1%e3%81%ab%e3%81%aa","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/9417","title":{"rendered":"System.Net.HttpListener \u304c .NET Core \u306e\u6642\u3060\u3051\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u3063\u307d\u3044\u73fe\u8c61\u306b\u306a\u308b\u4ef6\u306e\u89e3\u6c7a\u7b56"},"content":{"rendered":"<p><a href=\"https:\/\/github.com\/moonmile\/FireScratch\">FireScratch<\/a> \u3092 C# \u3067\u4f5c\u3063\u3066\u3044\u305f\u6642\u306b\u767a\u899a\u3057\u305f\u30e1\u30e2\u30ea\u30fc\u30ea\u30c3\u30af\u3063\u307d\u3044\u73fe\u8c61\u306b\u5f53\u305f\u3063\u305f\u306e\u3067\u3001\u73cd\u3057\u304f issue <a title=\"https:\/\/github.com\/dotnet\/corefx\/issues\/32454\" href=\"https:\/\/github.com\/dotnet\/corefx\/issues\/32454\">https:\/\/github.com\/dotnet\/corefx\/issues\/32454<\/a> \u3092\u7acb\u3066\u307e\u3057\u305f\u3002<\/p>\n<p>\u30b5\u30fc\u30d0\u30fc\u306e\u307b\u3046\u3067\u3001.NET Core \u304b .NET Framework \u3092\u4f7f\u3063\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u7c21\u6613 HTTP \u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nnamespace CheckHttpListener\r\n {\r\n     class Program\r\n     {\r\n         static void Main(string&#x5B;] args)\r\n         {\r\n             Console.WriteLine(&quot;test server .net framework&quot;);\r\n             var listener = new System.Net.HttpListener();\r\n             listener.Prefixes.Add(&quot;http:\/\/127.0.0.1:5411\/&quot;);\r\n             listener.Start();\r\n             while (true)\r\n             {\r\n                 var context = listener.GetContext();\r\n                 var res = context.Response;\r\n                 res.StatusCode = 200;\r\n                 var sw = new System.IO.StreamWriter(res.OutputStream);\r\n                 sw.Write(string.Format(&quot;text {0}&quot;, DateTime.Now.ToString()));\r\n                 sw.Close();\r\n             }\r\n         }\r\n     }\r\n }\r\n<\/pre>\n<p>\u3053\u308c\u306b\u5bfe\u3057\u3066\u3001\u30c6\u30b9\u30c8\u7528\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092\u4f5c\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nnamespace CheckClient\r\n{\r\n    class Program\r\n     {\r\n         static void Main(string&#x5B;] args)\r\n         {\r\n             HttpClient client = new HttpClient();\r\n             int cnt = 0;\r\n             while ( true )\r\n             {\r\n                 var res = client.GetAsync(&quot;http:\/\/localhost:5411&quot;).Result;\r\n                 var text = res.Content.ReadAsStringAsync().Result;\r\n                 Console.WriteLine($&quot;{cnt} {text}&quot;);\r\n                 cnt++;\r\n                 System.Threading.Thread.Sleep(20);\r\n             }\r\n         }\r\n     }\r\n }\r\n<\/pre>\n<p>\u3053\u308c\u3092\u52d5\u304b\u3057\u3066\u304a\u304f\u3068\u3001.NET Framework \u306e\u6642\u306b\u306f\u30b5\u30fc\u30d0\u30fc\u306e\u30e1\u30e2\u30ea\u304c\u7a0b\u3088\u3044\u5927\u304d\u3055\u3067\u6b62\u307e\u308b\u306e\u3067\u3059\u304c\u3001.NET Core \u306e\u307b\u3046\u306f\u3001\u304c\u3093\u304c\u3093\u3068\u30e1\u30e2\u30ea\u3092\u98df\u3044\u6f70\u3057\u3066\u3057\u307e\u3063\u3066\u6700\u5f8c\u306b\u30b5\u30fc\u30d0\u30fc\u304c\u5012\u308c\u307e\u3059\u3002\u3060\u3044\u305f\u30441\u6642\u9593\u3050\u3089\u3044\u653e\u7f6e\u3057\u3066\u304a\u304f\u3068\u843d\u3061\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2018\/10\/image-12.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2018\/10\/image_thumb-12.png\" border=\"0\" alt=\"image\" title=\"image\" width=\"470\" height=\"480\" style=\"margin: 0px; border: 0px currentcolor; display: inline; background-image: none;\" \/><\/a><\/p>\n<p>\u540c\u3058\u30b3\u30fc\u30c9\u3067\u3001.NET Framework \u3068 .NET Core \u306e\u6319\u52d5\u304c\u7570\u306a\u308b\u306e\u3067\u3001.NET Core \u306e HttpListener \u306e\u30d0\u30b0\u304b\uff1f\u3068\u3082\u601d\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u3069\u3046\u3084\u3089 HttpListener.GetContext \u304c HttpListnerContonet \u304c\u6301\u3064 Response \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306e\u6319\u52d5\u304c\u7570\u306a\u308b\u306e\u3067\u3001\u4e00\u6982\u306b\u30d0\u30b0\u3068\u306f\u8a00\u3048\u306a\u3044\u3088\u3046\u306a\u611f\u3058\u3067\u3059\u3002<\/p>\n<h2>Response \u3092\u660e\u793a\u7684\u306b Close \u3059\u308b\u304b using \u3092\u4f7f\u3046<\/h2>\n<p>\u89e3\u6c7a\u7b56\u3068\u3057\u3066\u306f\u3001Response \u306b\u5bfe\u3057\u3066\u660e\u793a\u7684\u306b Close \u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u304b\u3001using \u3092\u4f7f\u3063\u3066\u6697\u9ed9\u306b Response \u304c\u9589\u3058\u3089\u308c\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar context = listener.GetContext();\r\nvar res = context.Response;\r\n res.StatusCode = 200;\r\nvar sw = new System.IO.StreamWriter(res.OutputStream);\r\n sw.Write(string.Format(&quot;text {0}&quot;, DateTime.Now.ToString()));\r\n sw.Close();\r\n res.Close(); \/\/ \u2605\r\n<\/pre>\n<p>\u3042\u308b\u3044\u306f\u3001<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar context = listener.GetContext();\r\nusing (var res = context.Response)  \/\/ \u2605\r\n {\r\n     res.StatusCode = 200;\r\n     using (var sw = new System.IO.StreamWriter(res.OutputStream))\r\n     {\r\n         sw.Write(string.Format(&quot;text {0}&quot;, DateTime.Now.ToString()));\r\n     }\r\n}\r\n<\/pre>\n<p>\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\u3002\u3053\u306e Close \u51e6\u7406\u306f .NET Framework \u3067\u3082\u6709\u52b9\u306a\u306e\u3067\u3001\u3053\u308c\u3067\u540c\u3058\u30b3\u30fc\u30c9\u3067 .NET Core \u3067\u3082 .NET Framework \u3067\u3082\u30e1\u30e2\u30ea\u30ea\u30fc\u30af\u304c\u51fa\u306a\u3044\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u5927\u62b5\u306e\u30b5\u30f3\u30d7\u30eb\u306f HttpListener \u3092\u305f\u304f\u3055\u3093\u56de\u3055\u306a\u3044\u306e\u3067\u3001\u3053\u306e\u554f\u984c\u306b\u3042\u305f\u308b\u3053\u3068\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u5b9f\u969b\u306b\u4f5c\u3063\u3066\u30b5\u30fc\u30d0\u30fc\u5316\u3059\u308b\u3068\u6570\u6642\u9593\u5f8c\u306b\u843d\u3061\u305f\u308a\u3059\u308b\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u306d\u3002\u3053\u308c\u3001Windows + .NET Core \u3060\u3051\u306e\u73fe\u8c61\u306a\u306e\u304b\u3001\u305d\u308c\u3068\u3082 Linux + .NET Core \u3067\u3082\u767a\u751f\u3059\u308b\u306e\u304b\u306f\u3042\u3068\u3067\u8abf\u3079\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>FireScratch \u3092 C# \u3067\u4f5c\u3063\u3066\u3044\u305f\u6642\u306b\u767a\u899a\u3057\u305f\u30e1\u30e2\u30ea\u30fc\u30ea\u30c3\u30af\u3063\u307d\u3044\u73fe\u8c61\u306b\u5f53\u305f\u3063\u305f\u306e\u3067\u3001\u73cd\u3057\u304f issue https:\/\/github.com\/dotnet\/corefx\/issues\/32454 \u3092\u7acb\u3066\u307e\u3057 &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/9417\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[23],"tags":[],"class_list":["post-9417","post","type-post","status-publish","format-standard","hentry","category-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9417","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=9417"}],"version-history":[{"count":4,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9417\/revisions"}],"predecessor-version":[{"id":9421,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9417\/revisions\/9421"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=9417"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=9417"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=9417"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}