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

📄 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.Sockets; 
  //使用到TcpListen类 
using System.Net;
using System.Threading;
using System.Timers;

namespace cs
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		int s=0;
		int t;
		int port = 8000 ; 
		//定义侦听端口号 
		private TcpClient tcpc ; 
		//对服务器端创建TCP连接 
		private Socket stSend ; 
		//创建发送数据套接字 
		private bool tcpConnect = false ;
		DateTime t1;
		DateTime t2;
		
		
		
		private System.Timers.Timer timerClock = new System.Timers.Timer();
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Button button4;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.Label label10;
		private System.Windows.Forms.Label label11;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Label label12;
		private System.ComponentModel.IContainer components;

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

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

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if ( tcpConnect ) 
			{ 
				Byte [ ] bySend = new byte [ 4 ] ; 
				//根据字符串“STOP”长度来定义Byte数组 
				bySend = System.Text.Encoding. 
					Default.GetBytes ( "STOP" ) ; 
				int i = stSend.Send ( bySend ) ; 
				//发送控制码 
				stSend.Close ( ) ; 
				//关闭套接字 
			} 
			if ( disposing ) 
			{ 
				if ( components != null ) 
				{ 
					components.Dispose ( ) ; 
				} 
			} 
			base.Dispose ( disposing ) ; 

		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.label3 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.label7 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label10 = new System.Windows.Forms.Label();
			this.label11 = new System.Windows.Forms.Label();
			this.label12 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.button4 = new System.Windows.Forms.Button();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 40);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(74, 30);
			this.label1.TabIndex = 0;
			this.label1.Text = "IP地址:";
			// 
			// textBox1
			// 
			this.textBox1.ForeColor = System.Drawing.SystemColors.WindowText;
			this.textBox1.Location = new System.Drawing.Point(96, 40);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(166, 21);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(280, 40);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(62, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "连接";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 72);
			this.label2.Name = "label2";
			this.label2.TabIndex = 3;
			this.label2.Text = "发送信息:";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(96, 72);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(166, 21);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "";
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(24, 128);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(336, 124);
			this.listBox1.TabIndex = 6;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 442);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Size = new System.Drawing.Size(378, 22);
			this.statusBar1.TabIndex = 7;
			this.statusBar1.Text = "无连接";
			this.statusBar1.PanelClick += new System.Windows.Forms.StatusBarPanelClickEventHandler(this.statusBar1_PanelClick);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 104);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(128, 23);
			this.label3.TabIndex = 8;
			this.label3.Text = "已经发送的信息:";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(280, 72);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(62, 24);
			this.button2.TabIndex = 9;
			this.button2.Text = "发送";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(16, 8);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(360, 23);
			this.label4.TabIndex = 10;
			this.label4.Text = "确保接收端在监听状态,然后输入IP地址,连接成功后发送信息";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label7);
			this.groupBox1.Controls.Add(this.label5);
			this.groupBox1.Controls.Add(this.label10);
			this.groupBox1.Controls.Add(this.label11);
			this.groupBox1.Controls.Add(this.label12);
			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.Location = new System.Drawing.Point(24, 264);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(336, 168);
			this.groupBox1.TabIndex = 12;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "同步TCP测试(连续发送n组数据)";
			// 
			// label7
			// 
			this.label7.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
			this.label7.Location = new System.Drawing.Point(128, 136);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(192, 16);
			this.label7.TabIndex = 0;
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(24, 136);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(100, 16);
			this.label5.TabIndex = 1;
			this.label5.Text = "传输时间(ms):";
			// 
			// label10
			// 
			this.label10.Location = new System.Drawing.Point(24, 32);
			this.label10.Name = "label10";
			this.label10.Size = new System.Drawing.Size(100, 16);
			this.label10.TabIndex = 16;
			this.label10.Text = "发送间隔(ms)";
			// 
			// label11
			// 
			this.label11.Location = new System.Drawing.Point(24, 56);
			this.label11.Name = "label11";
			this.label11.Size = new System.Drawing.Size(100, 16);
			this.label11.TabIndex = 17;
			this.label11.Text = "发送次数(n)";
			// 
			// label12
			// 
			this.label12.Location = new System.Drawing.Point(24, 80);
			this.label12.Name = "label12";
			this.label12.Size = new System.Drawing.Size(100, 16);
			this.label12.TabIndex = 19;
			this.label12.Text = "要发送的信息";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(128, 24);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(192, 21);
			this.textBox3.TabIndex = 14;
			this.textBox3.Text = "";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(128, 48);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(192, 21);
			this.textBox4.TabIndex = 15;
			this.textBox4.Text = "";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(128, 72);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(192, 21);
			this.textBox5.TabIndex = 18;
			this.textBox5.Text = "";
			// 
			// button4
			// 
			this.button4.Location = new System.Drawing.Point(216, 104);
			this.button4.Name = "button4";
			this.button4.Size = new System.Drawing.Size(104, 23);
			this.button4.TabIndex = 13;
			this.button4.Text = "开始测试传输";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			// 
			// timer1
			// 
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(378, 464);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.statusBar1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label3);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.MaximizeBox = false;
			this.Name = "Form1";
			this.Text = "TCP发送端";
			this.Load += new System.EventHandler(this.Form1_Load_1);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

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

		private void button1_Click(object sender, System.EventArgs e)
		{
			//以下代码是判断是否和远程终结点成功连接 
			try 
			{ 
				stSend = new Socket ( AddressFamily.InterNetwork , 
					SocketType.Stream , ProtocolType.Tcp ) ; 
				//初始化一个Socket实例 
				IPEndPoint tempRemoteIP = new IPEndPoint 
					( IPAddress.Parse ( textBox1.Text ) , port ) ; 
				//根据IP地址和端口号创建远程终结点 
				EndPoint epTemp = ( EndPoint ) tempRemoteIP ; 
				stSend.Connect ( epTemp ) ; 
				//连接远程主机的8000端口号 
				statusBar1.Text = "成功连接远程计算机!" ; 
				tcpConnect = true ; 
				button1.Enabled = false ; 
				button2.Enabled = true ; 
			} 
			catch ( Exception ) 
			{ 
				statusBar1.Text = "目标计算机拒绝连接请求!" ; 
			} 

		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			int iLength = textBox2.Text.Length ; 
			//获取要发送的数据的长度 
			Byte [ ] bySend = new byte [ iLength ] ; 
			//根据获取的长度定义一个Byte类型数组 
			bySend = System.Text.Encoding.Default.GetBytes 
				( textBox2.Text ) ; 
			//按照指定编码类型把字符串指定到指定的Byte数组 
			int i = stSend.Send ( bySend ) ; 
			//发送数据 
			listBox1.Items.Add ( textBox2.Text ) ; 

		}

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


		private void timer1_Tick(object sender, System.EventArgs e)
		{
			int iLength = textBox5.Text.Length ; 
			Byte [ ] bySend = new byte [ iLength ] ; 
			bySend = System.Text.Encoding.Default.GetBytes ( textBox5.Text ) ;  
			int i = stSend.Send ( bySend ) ; 
			listBox1.Items.Add ( textBox5.Text ) ; 
			s++;
			if(s==t)
			{
				this.timer1.Stop();
				t2 = DateTime.Now;
				TimeSpan dt = t2 - t1;
				this.label7.Text=(dt.TotalMilliseconds.ToString()+"毫秒" );								
			};
			
		
		}

		private void button4_Click(object sender, System.EventArgs e)
		{
			int iLength = textBox4.Text.Length +"#".Length; 
			Byte [ ] bySend = new byte [ iLength ] ; 
			bySend = System.Text.Encoding.Default.GetBytes ( "#"+textBox4.Text ) ;  
			int i = stSend.Send ( bySend ) ; 
			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 statusBar1_PanelClick(object sender, System.Windows.Forms.StatusBarPanelClickEventArgs e)
		{
		
		}	

	}
}

⌨️ 快捷键说明

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