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

📄 form1.cs

📁 C#网络实例.里面有很多很实用的C#范例
💻 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.Threading;//新加的

namespace Sock_Conn
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox2;
		private IPAddress myIP=IPAddress.Parse("127.0.0.1");//新加的
		private IPEndPoint  MyServer;//新加的 
		private Socket sock;//新加的 
		  
		private int i=0;//新加的 
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.Button button2;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.richTextBox1 = new System.Windows.Forms.RichTextBox();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.button2 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(24, 32);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "服务器IP:";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(24, 64);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 23);
			this.label2.TabIndex = 2;
			this.label2.Text = "请求端口:";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(24, 96);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(80, 23);
			this.label3.TabIndex = 10;
			this.label3.Text = "服务信息:";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(120, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(152, 21);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(72, 168);
			this.button1.Name = "button1";
			this.button1.TabIndex = 3;
			this.button1.Text = "请求连接";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// statusBarPanel1
			// 
			this.statusBarPanel1.Width = 300;
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(120, 64);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(152, 21);
			this.textBox2.TabIndex = 4;
			this.textBox2.Text = "6688";
			// 
			// richTextBox1
			// 
			this.richTextBox1.Location = new System.Drawing.Point(120, 96);
			this.richTextBox1.Name = "richTextBox1";
			this.richTextBox1.Size = new System.Drawing.Size(256, 48);
			this.richTextBox1.TabIndex = 12;
			this.richTextBox1.Text = "";
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 209);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanel1});
			this.statusBar1.ShowPanels = true;
			this.statusBar1.Size = new System.Drawing.Size(376, 20);
			this.statusBar1.TabIndex = 6;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(224, 168);
			this.button2.Name = "button2";
			this.button2.TabIndex = 13;
			this.button2.Text = "断开连接";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(376, 229);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button2,
																		  this.richTextBox1,
																		  this.label3,
																		  this.statusBar1,
																		  this.textBox2,
																		  this.button1,
																		  this.label2,
																		  this.textBox1,
																		  this.label1});
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "Form1";
			this.Text = "按时收费";
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			try
			{
				myIP =IPAddress.Parse(textBox1.Text);
			
			}
			catch{MessageBox.Show("您输入的IP地址格式不正确,请重新输入!");}
			try
			{
				MyServer=new IPEndPoint(myIP,Int32.Parse(textBox2.Text));
				sock =new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			
				sock.Connect(MyServer);
				
					statusBarPanel1.Text="与主机"+textBox1.Text+"端口"+textBox2.Text+"连接成功!";
				timer1.Start();
				timer1.Tick+= new  EventHandler(aa);

				
			}
			catch(Exception ee){MessageBox.Show(ee.Message);}
		}

	
	
		private void aa(Object myObject,EventArgs myEventArgs)
		{
			timer1.Interval=10000;
			i=i+10;
			
			richTextBox1.Text="为主机"+textBox1.Text+"服务时间已达"+i.ToString()+"秒"+"请收费"+i.ToString()+"分钱!";
			string str="为您服务时间已达"+i.ToString()+"秒"+"请缴费"+i.ToString()+"分钱!";
			Byte[]  bytee=System.Text.Encoding.BigEndianUnicode.GetBytes(str.ToCharArray());
            sock.Send(bytee,bytee.Length,0);
			
				
		
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			sock.Close();
		}
	}
}

⌨️ 快捷键说明

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