首先我们了解下关于异步编程的一些知识可以参考我在网上收集的一些资料C#异步Socket编程 我实现了基于异步通信的Socket本文来源gao@!dai!ma.com搞$$代^@码!网服务器和客户端程序 下面是我的代码 第一步:首先新建一个服务端项目YBServerWindowsForms 然后在项目里添加一个自定义的类名为StateObje
首先我们了解下关于异步编程的一些知识可以参考我在网上收集的一些资料——C#异步Socket编程
我实现了基于异步通信的Socket服务器和客户端程序
下面是我的代码
第一步:首先新建一个服务端项目YBServerWindowsForms
然后在项目里添加一个自定义的类名为StateObject:
接着是界面设计代码ServerForm.Designer.cs
private System.ComponentModel.IContainer components = null; ///
/// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 ///
private void InitializeComponent() { this.IPTextBox = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.PortTextBox = new System.Windows.Forms.TextBox(); this.StartListenButton = new System.Windows.Forms.Button(); this.ReciveMessageRichTextBox = new System.Windows.Forms.RichTextBox(); this.HostStateRichTextBox = new System.Windows.Forms.RichTextBox(); this.label3 = new System.Windows.Forms.Label(); this.StopListenButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // IPTextBox // this.IPTextBox.Location = new System.Drawing.Point(81, 41); this.IPTextBox.Name = “IPTextBox”; this.IPTextBox.Size = new System.Drawing.Size(149, 21); this.IPTextBox.TabIndex = 0; this.IPTextBox.Text = “127.0.0.1”; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(22, 44); this.label1.Name = “label1”; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 1; this.label1.Text = “主机IP”; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(22, 80); this.label2.Name = “label2”; this.label2.Size = new System.Drawing.Size(53, 12); this.label2.TabIndex = 3; this.label2.Text = “主机端口”; // // PortTextBox // this.PortTextBox.Location = new System.Drawing.Point(81, 77); this.PortTextBox.Name = “PortTextBox”; this.PortTextBox.Size = new System.Drawing.Size(149, 21); this.PortTextBox.TabIndex = 2; this.PortTextBox.Text = “8080”; // // StartListenButton // this.StartListenButton.Location = new System.Drawing.Point(130, 357); this.StartListenButton.Name = “StartListenButton”; this.StartListenButton.Size = new System.Drawing.Size(100, 36); this.StartListenButton.TabIndex = 4; this.StartListenButton.Text = “开始监听”; this.StartListenButton.UseVisualStyleBackColor = true; this.StartListenButton.Click += new System.EventHandler(this.StartListenButton_Click); // // ReciveMessageRichTextBox // this.ReciveMessageRichTextBox.Location = new System.Drawing.Point(24, 131); this.ReciveMessageRichTextBox.Name = “ReciveMessageRichTextBox”; this.ReciveMessageRichTextBox.Size = new System.Drawing.Size(297, 178); this.ReciveMessageRichTextBox.TabIndex = 5; this.ReciveMessageRichTextBox.Text = “”; // // HostStateRichTextBox // this.HostStateRichTextBox.Location = new System.Drawing.Point(335, 56); this.HostStateRichTextBox.Name = “HostStateRichTextBox”; this.HostStateRichTextBox.Size = new System.Drawing.Size(261, 253); this.HostStateRichTextBox.TabIndex = 6; this.HostStateRichTextBox.Text = “”; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(333, 41); this.label3.Name = “label3”; this.label3.Size = new System.Drawing.Size(53, 12); this.label3.TabIndex = 7; this.label3.Text = “主机状态”; // // StopListenButton // this.StopListenButton.Location = new System.Drawing.Point(335, 357); this.StopListenButton.Name = “StopListenButton”; this.StopListenButton.Size = new System.Drawing.Size(100, 36); this.StopListenButton.TabIndex = 8; this.StopListenButton.Text = “停止监听”; this.StopListenButton.UseVisualStyleBackColor = true; this.StopListenButton.Click += new System.EventHandler(this.StopListenButton_Click); // // ServerForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(631, 430); this.Controls.Add(this.StopListenButton); this.Controls.Add(this.label3); this.Controls.Add(this.HostStateRichTextBox); this.Controls.Add(this.ReciveMessageRichTextBox); this.Controls.Add(this.StartListenButton); this.Controls.Add(this.label2); this.Controls.Add(this.PortTextBox); this.Controls.Add(this.label1); this.Controls.Add(this.IPTextBox); this.Name = “ServerForm”; this.Text = “YBServerForm”; this.Load += new System.EventHandler(this.ServerForm_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox IPTextBox; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox PortTextBox; private System.Windows.Forms.Button StartListenButton; private System.Windows.Forms.RichTextBox ReciveMessageRichTextBox; private System.Windows.Forms.RichTextBox HostStateRichTextBox; private System.Windows.Forms.Label label3; private System.Windows.Forms.Button StopListenButton; }}