{"id":707,"date":"2017-01-11T13:34:30","date_gmt":"2017-01-11T04:34:30","guid":{"rendered":"http:\/\/okamurax.com\/?p=707"},"modified":"2017-01-13T00:52:41","modified_gmt":"2017-01-12T15:52:41","slug":"c-web%e3%82%b5%e3%83%bc%e3%83%90%e3%83%bc","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=707","title":{"rendered":"C# Web\u30b5\u30fc\u30d0\u30fc"},"content":{"rendered":"<h2>\u30b5\u30fc\u30d0\uff11<\/h2>\n<pre class=\"lang:c# decode:true\">using System;\r\nusing System.Windows.Forms;\r\n\r\nnamespace tcp_listen_client\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n            Control.CheckForIllegalCrossThreadCalls = false;\r\n            \/\/\u30b9\u30ec\u30c3\u30c9\u304b\u3089\u30c6\u30ad\u30b9\u30c8\u30dc\u30c3\u30af\u30b9\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3092\u8a31\u53ef\r\n\r\n            \/\/\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u3068\u306f\u30a2\u30d7\u30ea\u3092\u30de\u30eb\u30c1\u30b9\u30ec\u30c3\u30c9\u3067\u8d70\u3089\u305b\u3066\u3082\u554f\u984c\u306a\u3044\u3053\u3068\u3002\r\n            \/\/Form\u306f\u30b9\u30ec\u30c3\u30c9\u30bb\u30fc\u30d5\u3067\u306f\u306a\u3044\u3002\r\n            \/\/\u901a\u5e38\u3001Control \u306f\u30e1\u30f3\u30d0\u306e\u53c2\u7167\u6642\u306bUI\u30b9\u30ec\u30c3\u30c9(Application.Run \u3057\u305f\u30b9\u30ec\u30c3\u30c9)\u3067\u64cd\u4f5c\u3059\u308b\u3053\u3068\u3092\u5f37\u5236\u3002\r\n            \/\/CheckForIllegalCrossThreadCalls\u3092false\u306b\u3059\u308b\u3068\u3001\u3053\u308c\u3092\u56de\u907f\u3067\u304d\u308b\u3002\r\n        }\r\n\r\n        private System.Net.Sockets.TcpListener server;\/\/\u30ea\u30b9\u30ca\u30fc\u63a5\u7d9a\u5f85\u3061\u3084\u53d7\u4fe1\u7b49\u3092\u884c\u3046\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\r\n        private System.Threading.Thread ListeningCallbackThred;\/\/\u63a5\u7d9a\u5f85\u3061\u30b9\u30ec\u30c3\u30c9\r\n        private volatile bool SLTAlive;\/\/\u63a5\u7d9a\u5f85\u3061\u30b9\u30ec\u30c3\u30c9\u7d42\u4e86\u6307\u793a\u30d5\u30e9\u30b0\r\n        \r\n        \/\/volatile\r\n        \/\/\u8907\u6570\u306e\u30b9\u30ec\u30c3\u30c9\u306b\u3088\u3063\u3066\u5909\u66f4\u3055\u308c\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u5834\u5408\u6307\u5b9a\u3002\r\n        \/\/\u30b7\u30f3\u30b0\u30eb\u30b9\u30ec\u30c3\u30c8\u524d\u63d0\u306e\u6700\u9069\u5316\u3092\u3057\u306a\u3044\r\n\r\n        private void Form1_Load(object sender, EventArgs e)\r\n        {\r\n            this.Text = \"\u30b5\u30fc\u30d0\u30fc\";\r\n            button1.Text = \"\u30b5\u30fc\u30d0\u30fc\u958b\u59cb\";\r\n            button2.Text = \"\u30b5\u30fc\u30d0\u30fc\u7d42\u4e86\";\r\n            label1.Text = \"\";\r\n\r\n            SLTAlive = false;\r\n            \/\/\u30b9\u30ec\u30c3\u30c9\u7d42\u4e86\u6307\u793a\u30d5\u30e9\u30b0\u3092\u7d42\u4e86\u306b\u521d\u671f\u5316\r\n        }\r\n\r\n        private void Form1_FormClosed(object sender, FormClosedEventArgs e)\r\n        {\r\n            \/\/\u30b5\u30fc\u30d0\u30fc\u3092\u7d42\u4e86\u3059\u308b\u306e\u306b\u3001\u63a5\u7d9a\u5f85\u3061\u30b9\u30ec\u30c3\u30c9\u304c\u7d42\u4e86\u3057\u3066\u3044\u306a\u3044\u5834\u5408\r\n            if (SLTAlive)\r\n            {\r\n                SLTAlive = false;\r\n                server.Stop();\r\n                ListeningCallbackThred = null;\r\n                \/\/\u30b9\u30ec\u30c3\u30c9\u3092null\u8a2d\u5b9a\r\n            }\r\n        }\r\n\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            \/\/\u63a5\u7d9a\u5f85\u3061\u30b9\u30ec\u30c3\u30c9\u304c\u751f\u6210\u3055\u308c\u3066\u3044\u306a\u3044\u5834\u5408\r\n            if (!SLTAlive)\r\n            {\r\n                ListeningCallbackThred = new System.Threading.Thread(ListeningCallback);\r\n                \/\/ListeningCallback\u306f\u30e1\u30bd\u30c3\u30c9\r\n                ListeningCallbackThred.Start();\r\n                SLTAlive = true;\r\n                \/\/\u672a\u7d42\u4e86\u306b\u8a2d\u5b9a\r\n            }\r\n        }\r\n\r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n            if (SLTAlive)\r\n            {\r\n                if (server != null)\r\n                {\r\n                    server.Stop();\r\n                }\r\n            }\r\n            SLTAlive = false;\/\/\u7d42\u4e86\u306b\u8a2d\u5b9a\r\n            label1.Text = \"\u30b5\u30fc\u30d0\u30fc\u7d42\u4e86\";\r\n        }\r\n\r\n        private void ListeningCallback()\r\n        {\r\n            \/\/\u30ea\u30b9\u30ca\u30fc\u3092\u751f\u6210\r\n            server = new System.Net.Sockets.TcpListener(System.Net.IPAddress.Parse(\"192.168.102.9\"),9000);\r\n            server.Start();\r\n            label1.Text = \"\u30b5\u30fc\u30d0\u30fc\u958b\u59cb\";\r\n\r\n            try\r\n            {\r\n                \/\/\u53d7\u4fe1\u306f\u7121\u9650\u30eb\u30fc\u30d7\u3002\u7d42\u4e86\u6307\u793a\u304c\u3042\u308c\u3070\u30eb\u30fc\u30d7\u7d42\u4e86\r\n                while (SLTAlive)\r\n                {\r\n                    \/\/\u53d7\u4fe1\u63a5\u7d9a\u30ad\u30e5\u30fc\u306b\u63a5\u7d9a\u5f85\u3061\u304c\u3042\u308b\u304b\u3069\u3046\u304b\r\n                    if(server.Pending() == true)\r\n                    {\r\n                        \/\/\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u304b\u3089\u306e\u63a5\u7d9a\u3092\u53d7\u3051\u4ed8\u3051\u308b\u3002\r\n                        \/\/\u901a\u4fe1\u30b9\u30c8\u30ea\u30fc\u30e0\u306e\u53d6\u5f97\r\n                        System.Net.Sockets.TcpClient ClientSocket = server.AcceptTcpClient();\r\n                        System.Net.Sockets.NetworkStream stream = ClientSocket.GetStream();\r\n                        byte[] ReceiveData = new byte[2000];\r\n                        int DataLength = stream.Read(ReceiveData,0,ReceiveData.Length);\r\n\r\n                        \/\/ textBox1.Text = ReceiveData.ToString();\u3000\u3053\u3046\u3059\u308b\u3068\r\n                        \/\/ System.Byte[]\u3068\u8868\u793a\u3055\u308c\u308b\u3002\u3064\u307e\u308a\u30d0\u30a4\u30c8\u578b\u306e\u914d\u5217\u306b\u306a\u3063\u3066\u3044\u308b\u3068\u3044\u3046\u3053\u3068\u304b\u3002\r\n\r\n                        \/\/string str = System.Text.Encoding.ASCII.GetString(ReceiveData, 0, DataLength);\r\n                        string str = System.Text.Encoding.GetEncoding(\"shift_jis\").GetString(ReceiveData,0,DataLength);\r\n                        \r\n                        \/\/GET\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u306b\u65e5\u672c\u8a9e\u3092\u5165\u308c\u3066\u30ea\u30af\u30a8\u30b9\u30c8\u3057\u305f\u5834\u5408\u3001\r\n                        \/\/ASCII\u3060\u3068??\u3068\u306a\u308b\u3002\r\n                        \/\/UTF-8\u3060\u3068\u6587\u5b57\u5316\u3051\u3059\u308b\u3002\r\n                        \/\/shift_jis\u3067\u3061\u3083\u3093\u3068\u8868\u793a\u3055\u308c\u305f\u3002\r\n\r\n                        \/\/str = Uri.UnescapeDataString(str);\r\n                        \/\/ String &lt;- byte[] GetString\r\n                        \/\/ String -&gt; byte[] GetByte\r\n\r\n                        textBox1.Text = str;\r\n\r\n                        \/\/\u8fd4\u4fe1\r\n                        string html = \"&lt;a href=\\\"http:\/\/yahoo.co.jp\\\"&gt;hello&lt;\/a&gt;\";\r\n                        byte[] SendBuffer = System.Text.Encoding.ASCII.GetBytes(html);\r\n                        \/\/ASCI\u306e\u90e8\u5206\u3092Unicode\u306b\u3059\u308b\u3068\u30d6\u30e9\u30a6\u30b6\u3067\u30c0\u30a6\u30ed\u30fc\u30c9\u3057\u3066\u3057\u307e\u3046\u3002\r\n\r\n                        stream.Write(SendBuffer, 0, SendBuffer.Length);\r\n                        \/\/stream.Flush();\r\n\r\n                        ClientSocket.Close();\r\n                    }\r\n                    \/\/\u77ed\u6642\u9593\u3060\u3051\u5f85\u6a5f\r\n                    System.Threading.Thread.Sleep(100);\r\n                }\r\n            }\r\n            catch(Exception e)\r\n            {\r\n                label1.Text = \"\u30b5\u30fc\u30d0\u30fc\u7d42\u4e86\";\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h2>\u30b5\u30fc\u30d0\uff12<\/h2>\n<pre class=\"lang:c# decode:true  \">using System;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\nusing System.Net;\r\nusing System.Net.Sockets;\r\nusing System.IO;\r\nusing System.Text.RegularExpressions;\r\n\r\nnamespace SimpSrv\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        bool chk;\r\n        TcpListener listener = null;\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n        }\r\n\r\n        private async void button1_Click(object sender, EventArgs e)\r\n        {\r\n            if (listener != null) return;\r\n\r\n            MessageBox.Show(\"\u8d77\u52d5\u3057\u307e\u3057\u305f\u3002\");\r\n            this.Text = \"\u8d77\u52d5\u4e2d\";\r\n\r\n            IPAddress ip = IPAddress.Parse(\"192.168.102.9\");\r\n            listener = new TcpListener(ip, 80);\r\n\r\n            listener.Start();\r\n            TcpClient client = null;\r\n\r\n            chk = true;\r\n\r\n            while(true)\r\n            {\r\n                await Task.Run(() =&gt;\r\n                {\r\n                    try\r\n                    {\r\n                        client = listener.AcceptTcpClient();\r\n                    }\r\n                    catch (SocketException ex)\r\n                    {\r\n                        System.Diagnostics.Debug.Print(ex.Message.ToString());\r\n                    }\r\n\r\n                });\r\n\r\n                if (!chk) break;\r\n\r\n                Encoding enc = Encoding.GetEncoding(\"shift_jis\");\r\n                string resMsg = \"\";\r\n                string sendMsg = \"\";\r\n\r\n                using (NetworkStream ns = client.GetStream())\r\n                {\r\n                    ns.WriteTimeout = 10000;\r\n                    ns.ReadTimeout = 10000;\r\n\r\n                    using (MemoryStream ms = new MemoryStream())\r\n                    {\r\n                        byte[] resByte = new byte[1024];\r\n                        int resSize = 0;\r\n                        \r\n                        do\r\n                        {\r\n                            try\r\n                            {\r\n                                resSize = ns.Read(resByte, 0, resByte.Length);\r\n                            }\r\n                            catch (IOException ex)\r\n                            {\r\n                                System.Diagnostics.Debug.Print(ex.Message.ToString());\r\n                                break;\r\n                            }\r\n\r\n                            if (resSize == 0) break;\r\n                            ms.Write(resByte, 0, resSize);\r\n\r\n                        } while (ns.DataAvailable);\r\n\r\n                        resMsg = enc.GetString(ms.GetBuffer(), 0, (int)ms.Length);\r\n                        ms.Close();\r\n                    }\r\n\r\n                    Regex reg = new Regex(@\"\\\/\\?name=([a-z]+)&amp;id=([0-9]{6})&amp;p=([0-9]{2})\\s\");\r\n                    Match match = reg.Match(resMsg);\r\n\r\n                    if (match.Success)\r\n                    {\r\n                        sendMsg = \"\\n\\n\" + Html.MakeHtml(match.Groups[1].ToString(),match.Groups[2].ToString(),match.Groups[3].ToString());\r\n                    }\r\n                    else\r\n                    {\r\n                        sendMsg = \"\\n\\n\" + \"false\";\r\n                    }\r\n\r\n                    byte[] sendBytes = enc.GetBytes(sendMsg);\r\n\r\n                    try\r\n                    {\r\n                        ns.Write(sendBytes, 0, sendBytes.Length);\r\n                    }\r\n                    catch (IOException ex)\r\n                    {\r\n                        System.Diagnostics.Debug.Print(ex.Message.ToString());\r\n                    }\r\n                    ns.Close();\r\n                }\r\n                client.Close();\r\n            }\r\n        }\r\n\r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n            if (listener == null) return;\r\n\r\n            MessageBox.Show(\"\u7d42\u4e86\u3057\u307e\u3057\u305f\u3002\");\r\n            this.Text = \"\u505c\u6b62\u4e2d\";\r\n\r\n            listener.Stop();\r\n            listener = null;\r\n\r\n            chk = false;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u30b5\u30fc\u30d0\uff11 using System; using System.Windows.Forms; namespace tcp_listen_client { public partial class Form1 : Form &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=707\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Web\u30b5\u30fc\u30d0\u30fc&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=707"}],"version-history":[{"count":3,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions"}],"predecessor-version":[{"id":722,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/707\/revisions\/722"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}