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

📄 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 AsyncTcpServer
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListBox listBox1;

		private Socket server;
		private byte[] data =new byte[1024];
		private int size = 1024;
		private int count = 0;
		private double num;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.Button button3;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		/// <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 );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.button1 = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button2 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.button3 = new System.Windows.Forms.Button();
			this.button4 = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(176, 16);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 24);
			this.button1.TabIndex = 0;
			this.button1.Text = "关闭";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(16, 56);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(264, 112);
			this.listBox1.TabIndex = 2;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(48, 16);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 24);
			this.button2.TabIndex = 3;
			this.button2.Text = "监听";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.label1.Location = new System.Drawing.Point(136, 88);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(120, 16);
			this.label1.TabIndex = 5;
			// 
			// label2
			// 
			this.label2.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.label2.Location = new System.Drawing.Point(136, 112);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(120, 16);
			this.label2.TabIndex = 6;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 344);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(296, 22);
			this.statusBar1.TabIndex = 7;
			this.statusBar1.Text = "请按\"监听\"按钮开始监听";
			// 
			// button3
			// 
			this.button3.Location = new System.Drawing.Point(152, 24);
			this.button3.Name = "button3";
			this.button3.TabIndex = 8;
			this.button3.Text = "计算准确率";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(40, 24);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(80, 24);
			this.button4.TabIndex = 9;
			this.button4.Text = "计数器置零";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label6);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.label4);
			this.groupBox1.Controls.Add(this.label3);
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.label2);
			this.groupBox1.Controls.Add(this.button4);
			this.groupBox1.Controls.Add(this.button3);
			this.groupBox1.Location = new System.Drawing.Point(16, 192);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(264, 144);
			this.groupBox1.TabIndex = 10;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "异步socket测试(传输准确性)";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(112, 16);
			this.label3.TabIndex = 10;
			this.label3.Text = "实际接收的数据(m)";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 112);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(100, 16);
			this.label4.TabIndex = 11;
			this.label4.Text = "准确率(m/n)";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(16, 64);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(112, 16);
			this.label5.TabIndex = 12;
			this.label5.Text = "实际发送的数据(n)";
			this.label5.Click += new System.EventHandler(this.label5_Click);
			// 
			// label6
			// 
			this.label6.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.label6.Location = new System.Drawing.Point(136, 64);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(120, 16);
			this.label6.TabIndex = 13;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(296, 366);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.listBox1);
			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)
		{
			Close();
		}

		void AcceptConn(IAsyncResult iar)
		{
			Socket oldserver = (Socket)iar.AsyncState;
			Socket client = oldserver.EndAccept(iar);
			this.statusBar1.Text = "连接到:"+client.RemoteEndPoint.ToString();
			string stringData = "连接成功!";
			byte[] message1 = Encoding.Default.GetBytes(stringData);
			client.BeginSend(message1,0,message1.Length,SocketFlags.None,new AsyncCallback(SendData),client);
		}

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

		void ReceiveData(IAsyncResult iar)
		{
			Socket client = (Socket)iar.AsyncState;
			int recv = client.EndReceive(iar);
			if(recv==0)
			{
				client.Close();
				this.statusBar1.Text = "等待连接...";
				server.BeginAccept(new AsyncCallback(AcceptConn),server);
				return;
			}
			string receiveData = Encoding.Default.GetString(data,0,recv);

			if  ( receiveData.StartsWith("#") )
			{
				string abc=receiveData;
				abc = abc.Replace("#", "");
				num = Convert.ToDouble(abc); 
				this.label6.Text = num.ToString()+"个";
                count--;
			}
            count++;
			this.listBox1.Items.Add(receiveData +" 收到的第"+count+"个数据" )  ;
			this.label1.Text=(count+"个");
			byte[] message2 = Encoding.Default.GetBytes(receiveData);
			client.BeginSend(message2,0,message2.Length,SocketFlags.None,new AsyncCallback(SendData),client);
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			server = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			IPEndPoint iep = new IPEndPoint(IPAddress.Any,8000);
			server.Bind(iep);
			server.Listen(5);
			server.BeginAccept(new AsyncCallback(AcceptConn),server);
			this.statusBar1.Text = "开始监听...";
			this.button2.Enabled=false;

		}

		private void button3_Click(object sender, System.EventArgs e)
		{
			try
			{
				
				double temp=count/num*100;
				this.label2.Text=(temp.ToString()+"%");
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			count=0;
			num = 0;
			this.label6.Text=(num+"个");
			this.label1.Text=(count+"个");
			this.label2.Text=("");
		}

		private void label5_Click(object sender, System.EventArgs e)
		{
		
		}

	}
}

⌨️ 快捷键说明

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