异步通信实例的详细介绍
来源:www.45fan.com 2016-08-31 16:47:25
异步通信实例的详细介绍
usingSystem;
usingSystem.Drawing;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Windows.Forms;
usingSystem.Data;
usingSystem.Text;
usingSystem.Net;
usingSystem.Net.Sockets;
usingSystem.Threading;

namespaceTServer

...{

/**////<summary>
///Form1的摘要说明。
///</summary>
publicclassForm1:System.Windows.Forms.Form

...{
privateSystem.Windows.Forms.TextBoxtxt;
privateSystem.Net.Sockets.Socketserver;
privateSystem.Windows.Forms.ComboBoxcmbIP;
privateSystem.Windows.Forms.ButtonbtnStart;
privateSystem.Windows.Forms.ButtonbtnStop;
privateSystem.Windows.Forms.StatusBarstatBar;
privateSystem.Threading.ManualResetEventallDone;
privatebyte[]buffer;

/**////<summary>
///必需的设计器变量。
///</summary>
privateSystem.ComponentModel.Containercomponents=null;

publicForm1()

...{
//
//Windows窗体设计器支持所必需的
//
InitializeComponent();

//
//TODO:在InitializeComponent调用后添加任何构造函数代码
//
}


/**////<summary>
///清理所有正在使用的资源。
///</summary>
protectedoverridevoidDispose(booldisposing)

...{
if(disposing)

...{
if(components!=null)

...{
components.Dispose();
}
}
base.Dispose(disposing);
}


Windows窗体设计器生成的代码#regionWindows窗体设计器生成的代码

/**////<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()

...{
this.txt=newSystem.Windows.Forms.TextBox();
this.cmbIP=newSystem.Windows.Forms.ComboBox();
this.btnStart=newSystem.Windows.Forms.Button();
this.btnStop=newSystem.Windows.Forms.Button();
this.statBar=newSystem.Windows.Forms.StatusBar();
this.SuspendLayout();
//
//txt
//
this.txt.Anchor=((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom|System.Windows.Forms.AnchorStyles.Left)
|System.Windows.Forms.AnchorStyles.Right)));
this.txt.Location=newSystem.Drawing.Point(0,40);
this.txt.Multiline=true;
this.txt.Name="txt";
this.txt.Size=newSystem.Drawing.Size(472,232);
this.txt.TabIndex=0;
this.txt.Text="";
//
//cmbIP
//
this.cmbIP.DropDownStyle=System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbIP.Location=newSystem.Drawing.Point(8,8);
this.cmbIP.Name="cmbIP";
this.cmbIP.Size=newSystem.Drawing.Size(144,20);
this.cmbIP.TabIndex=1;
//
//btnStart
//
this.btnStart.FlatStyle=System.Windows.Forms.FlatStyle.Popup;
this.btnStart.Location=newSystem.Drawing.Point(168,8);
this.btnStart.Name="btnStart";
this.btnStart.TabIndex=2;
this.btnStart.Text="开始";
this.btnStart.Click+=newSystem.EventHandler(this.btnStart_Click);
//
//btnStop
//
this.btnStop.FlatStyle=System.Windows.Forms.FlatStyle.Popup;
this.btnStop.Location=newSystem.Drawing.Point(272,8);
this.btnStop.Name="btnStop";
this.btnStop.TabIndex=2;
this.btnStop.Text="停止";
//
//statBar
//
this.statBar.Location=newSystem.Drawing.Point(0,272);
this.statBar.Name="statBar";
this.statBar.Size=newSystem.Drawing.Size(472,22);
this.statBar.TabIndex=3;
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(6,14);
this.ClientSize=newSystem.Drawing.Size(472,294);
this.Controls.Add(this.statBar);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.cmbIP);
this.Controls.Add(this.txt);
this.Controls.Add(this.btnStop);
this.Name="Form1";
this.Text="服务器端";
this.Load+=newSystem.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion


/**////<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain()

...{
Application.Run(newForm1());
}

privatevoidForm1_Load(objectsender,System.EventArgse)

...{
this.server=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
this.allDone=newManualResetEvent(true);
this.cmbIP.Items.Clear();
foreach(IPAddressipinDns.GetHostByName(Dns.GetHostName()).AddressList)

...{
this.cmbIP.Items.Add(ip.ToString());
}
if(this.cmbIP.Items.Count>0)
this.cmbIP.SelectedIndex=0;
this.statBar.Text="初始