{"id":306,"date":"2022-07-01T11:11:59","date_gmt":"2022-07-01T03:11:59","guid":{"rendered":"https:\/\/www.fengjijiao.cn\/?p=306"},"modified":"2022-07-01T11:36:11","modified_gmt":"2022-07-01T03:36:11","slug":"c%e5%90%8c%e6%ad%a5socket-client%ef%bc%88windows-form%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.fengjijiao.cn\/?p=306","title":{"rendered":"C#\u540c\u6b65Socket Client\uff08Windows Form\uff09"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5e93\u6587\u4ef6\uff08SocketLib.cs\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Text;\nusing System.Net;\nusing System.Net.Sockets;\n\nnamespace WindowsFormsApp3\n{\n    public class SocketLib\n    {\n        private static byte&#91;] buffer = new byte&#91;1024*100];\n        public static string Get()\n        {\n            string message = \"\";\n            try\n            {\n                \/\/\u83b7\u53d6hostname\u7684ip\u5730\u5740\u96c6\uff0c\u672c\u673a\u5730\u5740\u53ef\u4f7f\u7528Dns.GetHostName()\n                IPHostEntry ipHostInfo = Dns.GetHostEntry(\"www.baidu.com\");\n                \/\/\u4f7f\u7528\u83b7\u53d6\u5230\u7684\u7b2c\u4e00\u4e2a\u5730\u5740\n                IPAddress ipAddress = ipHostInfo.AddressList&#91;0];\n                \/\/\u8bbe\u7f6e\u7ec8\u7ed3\u70b9\u7684ip\u5730\u5740\u548c\u7aef\u53e3\n                IPEndPoint remoteEP = new IPEndPoint(ipAddress, 80);\/\/ip port\n                Socket st = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);\n                try\n                {\n                    \/\/\u8fde\u63a5\n                    st.Connect(remoteEP);\n                    message += \"Socket connected to \" + st.RemoteEndPoint.ToString()+\"\\r\\n\";\n                    \/\/\u53d1\u9001\n                    byte&#91;] msg = Encoding.UTF8.GetBytes(\"GET \/ HTTP\/1.1\\r\\n\\r\\nHost: www.baidu.com\\r\\nAccept: *\/*\\r\\n\\r\\n\");\n                    int bytesSent = st.Send(msg);\n                    int bytesRecv;\n                    \/\/\u63a5\u6536\uff0c\u8fd9\u91cc\u53ea\u662f\u7b80\u5355\u7684\u5224\u65ad\uff0c\u5e76\u6ca1\u6709\u4eceHTTP\u5934\u90e8\u4e2d\u83b7\u53d6\u6b63\u6587\u957f\u5ea6\uff0c\u4ec5\u4f9b\u6f14\u793a\n                    do\n                    {\n                        bytesRecv = st.Receive(buffer);\n                        message += Encoding.UTF8.GetString(buffer, 0, bytesRecv) + \"\\r\\n\";\n                    } while (bytesRecv &gt; 0);\n                    \/\/\u5173\u95ed\n                    st.Shutdown(SocketShutdown.Both);\n                    st.Close();\n                }\n                catch (ArgumentNullException ane)\n                {\n                    message += \"ArgumentNullException: \" + ane.ToString() + \"\\r\\n\";\n                }\n                catch (SocketException se)\n                {\n                    message += \"SocketException: \" + se.ToString() + \"\\r\\n\";\n                }\n                catch (Exception e1)\n                {\n                    message += \"Exception: \" + e1.ToString() + \"\\r\\n\";\n                }\n            }\n            catch (Exception e2)\n            {\n                message += e2.ToString() + \"\\r\\n\";\n            }\n            return message;\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e3b\u7a0b\u5e8f\uff08Form1.cs\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.ComponentModel;\nusing System.Data;\nusing System.Drawing;\nusing System.Linq;\nusing System.Text;\nusing System.Threading.Tasks;\nusing System.Windows.Forms;\nusing System.Net;\nusing System.Net.Sockets;\nusing System.Threading;\n\nnamespace WindowsFormsApp3\n{\n    public partial class Form1 : Form\n    {\n        \/\/\u4e3b\u7ebf\u7a0b\u4e0a\u4e0b\u6587\n        public SynchronizationContext mainThreadContext;\n        public Form1()\n        {\n            mainThreadContext = SynchronizationContext.Current;\n            InitializeComponent();\n        }\n\n        private void button1_Click(object sender, EventArgs e)\n        {\n            this.textBox1.Text = \"Start!\\r\\n\";\n            \/\/\u5c06Socket\u64cd\u4f5c\u653e\u5728\u5b50\u7ebf\u7a0b\n            ThreadStart ts = new ThreadStart(ThreadGet);\n            Thread th = new Thread(ts);\n            th.Start();\n        }\n\n        void showText(object text)\n        {\n            var str = text as string;\n            this.textBox1.Text += str;\n        }\n\n        void ThreadGet()\n        {\n            string message = So.Get();\n            \/\/\u5c06\u4fe1\u606f\u9001\u56de\u4e3b\u7ebf\u7a0b\n            mainThreadContext.Post(new SendOrPostCallback(showText), message);\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u9605<\/h2>\n\n\n\n<p><a href=\"https:\/\/docs.microsoft.com\/zh-cn\/dotnet\/framework\/network-programming\/synchronous-client-socket-example\">https:\/\/docs.microsoft.com\/zh-cn\/dotnet\/framework\/network-programming\/synchronous-client-socket-example<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e93\u6587\u4ef6\uff08SocketLib.cs\uff09 \u4e3b\u7a0b\u5e8f\uff08Form1.cs\uff09 \u53c2\u9605 https:\/\/docs.microso [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[19],"tags":[25],"_links":{"self":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/306"}],"collection":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=306"}],"version-history":[{"count":7,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions"}],"predecessor-version":[{"id":317,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=\/wp\/v2\/posts\/306\/revisions\/317"}],"wp:attachment":[{"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fengjijiao.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}