using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net;using System.Net.Sockets;using System.Threading;namespace TCLient...{ /**//// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form ...{ private System.Windows.Forms.TextBox txt; private System.Windows.Forms.StatusBar statBar; private System.Windows.Forms.TextBox txtIP; private System.Windows.Forms.TextBox sendText; private System.Windows.Forms.Button btnSend; private System.Windows.Forms.Button btnStart; private System.Net.Sockets.Socket client; private byte[] buffer; /**//// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() ...{ // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /**//// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) ...{ if( disposing ) ...{ if (components != null) ...{ components.Dispose(); } } base.Dispose( disposing ); } Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 /**//// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() ...{ this.txt = new System.Windows.Forms.TextBox(); this.statBar = new System.Windows.Forms.StatusBar(); this.txtIP = new System.Windows.Forms.TextBox(); this.sendText = new System.Windows.Forms.TextBox(); this.btnSend = new System.Windows.Forms.Button(); this.btnStart = new System.Windows.Forms.Button(); this.SuspendLayout(); // // txt // this.txt.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.txt.Location = new System.Drawing.Point(0, 32); this.txt.Multiline = true; this.txt.Name = "txt"; this.txt.Size = new System.Drawing.Size(584, 208); this.txt.TabIndex = 0; this.txt.Text = ""; // // statBar // this.statBar.Location = new System.Drawing.Point(0, 244); this.statBar.Name = "statBar"; this.statBar.Size = new System.Drawing.Size(584, 22); this.statBar.TabIndex = 1; // // txtIP // this.txtIP.Location = new System.Drawing.Point(8, 8); this.txtIP.Name = "txtIP"; this.txtIP.Size = new System.Drawing.Size(96, 21); this.txtIP.TabIndex = 2; this.txtIP.Text = "192.168.0.14"; // // sendText // this.sendText.Location = new System.Drawing.Point(104, 8); this.sendText.Name = "sendText"; this.sendText.Size = new System.Drawing.Size(272, 21); this.sendText.TabIndex = 3; this.sendText.Text = ""; // // btnSend // this.btnSend.Location = new System.Drawing.Point(392, 8); this.btnSend.Name = "btnSend"; this.btnSend.TabIndex = 4; this.btnSend.Text = "发送"; this.btnSend.Click += new System.EventHandler(this.btnSend_Click); // // btnStart // this.btnStart.Location = new System.Drawing.Point(488, 8); this.btnStart.Name = "btnStart"; this.btnStart.TabIndex = 4; this.btnStart.Text = "开始"; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(584, 266); this.Controls.Add(this.btnSend); this.Controls.Add(this.sendText); this.Controls.Add(this.txtIP); this.Controls.Add(this.statBar); this.Controls.Add(this.txt); this.Controls.Add(this.btnStart); this.Name = "Form1"; this.Text = "客户端"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /**//// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() ...{ Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) ...{ this.client = new Socket( AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp ); this.statBar.Text = "初始化完毕..."; this.buffer = new byte[ 1024 ]; } private void btnStart_Click(object sender, System.EventArgs e) ...{ IPEndPoint ipep = new IPEndPoint( IPAddress.Parse( this.txtIP.Text ),9000 ); this.client.BeginConnect( (EndPoint)ipep,new AsyncCallback(this.ConnectCallback ),this.client ); } private void ConnectCallback( System.IAsyncResult iar ) ...{ try ...{ Socket server = (Socket)iar.AsyncState; this.client.EndConnect( iar ); this.txt.Text += " 已连接至服务器。"; this.statBar.Text = "连接到服务器:" + server.RemoteEndPoint.ToString(); this.client.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback( this.ReceiveCallback ),this.client ); } catch( Exception ex ) ...{ this.txt.Text += " " + ex.ToString(); } } private void ReceiveCallback( System.IAsyncResult iar ) ...{ try ...{ Socket remote = (Socket)iar.AsyncState; int recv = remote.EndReceive( iar ); string tmp = System.Text.Encoding.Default.GetString( this.buffer,0,recv ); this.txt.Text += " 从服务器端接收到:" + tmp; } catch( Exception ex ) ...{ } } private void Send() ...{ byte[] tmp = System.Text.Encoding.Default.GetBytes( this.sendText.Text ); this.client.BeginSend( tmp,0,tmp.Length,SocketFlags.None,new AsyncCallback(this.SendCallback),this.client ); } private void SendCallback( System.IAsyncResult iar ) ...{ try ...{ Socket remote = (Socket)iar.AsyncState; int s = remote.EndSend( iar ); remote.BeginReceive( this.buffer,0,this.buffer.Length,SocketFlags.None,new AsyncCallback(this.ReceiveCallback),remote ); } catch( Exception ex ) ...{ this.txt.Text += " " + ex.ToString(); } } private void btnSend_Click(object sender, System.EventArgs e) ...{ this.Send(); } }}