⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 java基础方面的一些实例代码
💻 CS
字号:
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.Text;

namespace AsyncTcpClient
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.ListBox listBox1;

		private Socket client;
		private byte[] data =new byte[1024];
		private int size = 1024;
		int s=0;
		int t;
		DateTime t1;
		DateTime t2;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.TextBox textBox6;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.ComponentModel.IContainer components;

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

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

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.button3 = new System.Windows.Forms.Button();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button4 = new System.Windows.Forms.Button();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.label1 = new System.Windows.Forms.Label();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.textBox6 = new System.Windows.Forms.TextBox();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(160, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 23);
			this.button1.TabIndex = 0;
			this.button1.Text = "连接";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(232, 56);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(64, 23);
			this.button2.TabIndex = 1;
			this.button2.Text = "发送";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(232, 16);
			this.button3.Name = "button3";
			this.button3.Size = new System.Drawing.Size(64, 23);
			this.button3.TabIndex = 2;
			this.button3.Text = "断开";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(16, 56);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(208, 21);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "输入信息";
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(16, 88);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(280, 100);
			this.listBox1.TabIndex = 5;
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(176, 88);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(88, 24);
			this.button4.TabIndex = 6;
			this.button4.Text = "开始测试发送";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(128, 16);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(136, 21);
			this.textBox3.TabIndex = 7;
			this.textBox3.Text = "";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(128, 40);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(136, 21);
			this.textBox4.TabIndex = 8;
			this.textBox4.Text = "";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(128, 64);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(136, 21);
			this.textBox5.TabIndex = 9;
			this.textBox5.Text = "";
			// 
			// timer1
			// 
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.label1.Location = new System.Drawing.Point(128, 120);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(136, 16);
			this.label1.TabIndex = 10;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 352);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(312, 22);
			this.statusBar1.TabIndex = 11;
			this.statusBar1.Text = "确保接收端在监听状态,输入IP并连接";
			// 
			// textBox6
			// 
			this.textBox6.Location = new System.Drawing.Point(16, 16);
			this.textBox6.Name = "textBox6";
			this.textBox6.Size = new System.Drawing.Size(136, 21);
			this.textBox6.TabIndex = 12;
			this.textBox6.Text = "输入IP地址";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.textBox3);
			this.groupBox1.Controls.Add(this.textBox4);
			this.groupBox1.Controls.Add(this.textBox5);
			this.groupBox1.Controls.Add(this.button4);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Location = new System.Drawing.Point(16, 200);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(280, 144);
			this.groupBox1.TabIndex = 13;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "异步socket测试(连续发送n组数据)";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 16);
			this.label2.TabIndex = 10;
			this.label2.Text = "发送间隔(ms)";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 48);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 11;
			this.label3.Text = "发送次数(n)";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 72);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 16);
			this.label4.TabIndex = 12;
			this.label4.Text = "所要发送的信息";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 120);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(112, 16);
			this.label5.TabIndex = 13;
			this.label5.Text = "发送所用时间(ms):";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(312, 374);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.textBox6);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.button3);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Name = "Form1";
			this.Text = "异步套接字发送端";
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				this.statusBar1.Text = "连接中...";
				Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
				IPEndPoint iep = new IPEndPoint(IPAddress.Parse(this.textBox6.Text),8000);
				newsock.BeginConnect(iep,new AsyncCallback(Connected),newsock);
				this.button1.Enabled=false;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			byte[] message = Encoding.Default.GetBytes(this.textBox2.Text);
			client.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),client);
		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			client.Close();
			this.statusBar1.Text = "断开";
			this.button1.Enabled=true;
		}

		void Connected(IAsyncResult iar)
		{
			client = (Socket)iar.AsyncState;
			try
			{
				client.EndConnect(iar);
				this.statusBar1.Text = "连接到:"+client.RemoteEndPoint.ToString();
				client.BeginReceive(data,0,size,SocketFlags.None,new AsyncCallback(ReceiveData),client);
			}
			catch(SocketException)
			{
				this.statusBar1.Text = "连接失败!";
			}
		}

		void ReceiveData(IAsyncResult iar)
		{
			Socket remote = (Socket)iar.AsyncState;
			int recv = remote.EndReceive(iar);
			string stringData = Encoding.Default.GetString(data,0,recv);
			this.listBox1.Items.Add(stringData);
		}

		void SendData(IAsyncResult iar)
		{
			Socket remote = (Socket)iar.AsyncState;
			int sent = remote.EndSend(iar);
			remote.BeginReceive(data,0,size,SocketFlags.None,new AsyncCallback(ReceiveData),remote);
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			byte[] message = Encoding.Default.GetBytes("#"+this.textBox4.Text);
			client.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),client);
			try
			{
				t1 = DateTime.Now;			
				s=0;
				int temp1=Int32.Parse(this.textBox3.Text.ToString());//循环间隔
				int temp2=Int32.Parse(this.textBox4.Text.ToString());//循环次数
				this.timer1.Interval=temp1;
				t=temp2;
				this.timer1.Start();
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			byte[] message = Encoding.Default.GetBytes(this.textBox5.Text);
			client.BeginSend(message,0,message.Length,SocketFlags.None,new AsyncCallback(SendData),client);
			s++;
			if(s==t)
			{
				this.timer1.Stop();
				t2 = DateTime.Now;
				TimeSpan dt = t2 - t1;
				this.label1.Text=(dt.TotalMilliseconds.ToString()+"毫秒" );								
			};
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -