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

📄 cweituo.txt

📁 了解委托与事件的基本概念
💻 TXT
📖 第 1 页 / 共 2 页
字号:
}
//客户端
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;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
namespace TestSyncClient
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{

		private Socket socket;
		private Thread thread;
         public static int fs=0;

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.TextBox textBoxIP;
		private System.Windows.Forms.TextBox textBoxPort;
		private System.Windows.Forms.Button buttonRequest;
		private System.Windows.Forms.Button buttonSend;
		private System.Windows.Forms.Button buttonClose;
		private System.Windows.Forms.ListBox listBoxState;
		private System.Windows.Forms.RichTextBox richTextBoxSend;
		private System.Windows.Forms.RichTextBox richTextBoxReceive;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

			this.richTextBoxSend.Text="";
			this.richTextBoxReceive.Text="";
			this.listBoxState.Items.Clear();
			this.textBox1.Text=fs.ToString();
		}

		/// <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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.listBoxState = new System.Windows.Forms.ListBox();
			this.textBoxIP = new System.Windows.Forms.TextBox();
			this.textBoxPort = new System.Windows.Forms.TextBox();
			this.buttonRequest = new System.Windows.Forms.Button();
			this.buttonSend = new System.Windows.Forms.Button();
			this.buttonClose = new System.Windows.Forms.Button();
			this.richTextBoxSend = new System.Windows.Forms.RichTextBox();
			this.richTextBoxReceive = new System.Windows.Forms.RichTextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(11, 31);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(85, 29);
			this.label1.TabIndex = 0;
			this.label1.Text = "服务器IP";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(11, 93);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(85, 29);
			this.label2.TabIndex = 0;
			this.label2.Text = "请求端口";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(11, 154);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(85, 30);
			this.label3.TabIndex = 0;
			this.label3.Text = "接收信息";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(11, 267);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(85, 30);
			this.label4.TabIndex = 0;
			this.label4.Text = "发送信息";
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.listBoxState);
			this.groupBox1.Location = new System.Drawing.Point(267, 10);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(266, 129);
			this.groupBox1.TabIndex = 1;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "程序状态";
			// 
			// listBoxState
			// 
			this.listBoxState.ItemHeight = 15;
			this.listBoxState.Location = new System.Drawing.Point(11, 21);
			this.listBoxState.Name = "listBoxState";
			this.listBoxState.Size = new System.Drawing.Size(245, 79);
			this.listBoxState.TabIndex = 0;
			// 
			// textBoxIP
			// 
			this.textBoxIP.Location = new System.Drawing.Point(107, 31);
			this.textBoxIP.Name = "textBoxIP";
			this.textBoxIP.ReadOnly = true;
			this.textBoxIP.Size = new System.Drawing.Size(133, 25);
			this.textBoxIP.TabIndex = 2;
			this.textBoxIP.Text = "127.0.0.1";
			// 
			// textBoxPort
			// 
			this.textBoxPort.Location = new System.Drawing.Point(107, 93);
			this.textBoxPort.Name = "textBoxPort";
			this.textBoxPort.ReadOnly = true;
			this.textBoxPort.Size = new System.Drawing.Size(133, 25);
			this.textBoxPort.TabIndex = 2;
			this.textBoxPort.Text = "6788";
			// 
			// buttonRequest
			// 
			this.buttonRequest.Location = new System.Drawing.Point(64, 381);
			this.buttonRequest.Name = "buttonRequest";
			this.buttonRequest.Size = new System.Drawing.Size(100, 29);
			this.buttonRequest.TabIndex = 3;
			this.buttonRequest.Text = "请求连接";
this.buttonRequest.Click += new System.EventHandler(this.buttonRequest_Click);
			// 
			// buttonSend
			// 
			this.buttonSend.Location = new System.Drawing.Point(213, 381);
			this.buttonSend.Name = "buttonSend";
			this.buttonSend.Size = new System.Drawing.Size(100, 29);
			this.buttonSend.TabIndex = 3;
			this.buttonSend.Text = "发送信息";
this.buttonSend.Click += new System.EventHandler(this.buttonSend_Click);
			// 
			// buttonClose
			// 
			this.buttonClose.Location = new System.Drawing.Point(363, 381);
			this.buttonClose.Name = "buttonClose";
			this.buttonClose.Size = new System.Drawing.Size(100, 29);
			this.buttonClose.TabIndex = 3;
			this.buttonClose.Text = "关闭连接";
	this.buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
			// 
			// richTextBoxSend
			// 
			this.richTextBoxSend.Location = new System.Drawing.Point(107, 267);
			this.richTextBoxSend.Name = "richTextBoxSend";
			this.richTextBoxSend.Size = new System.Drawing.Size(426, 103);
			this.richTextBoxSend.TabIndex = 5;
			this.richTextBoxSend.Text = "richTextBoxSend";
			// 
			// richTextBoxReceive
			// 
		this.richTextBoxReceive.Location = new System.Drawing.Point(107, 154);
			this.richTextBoxReceive.Name = "richTextBoxReceive";
			this.richTextBoxReceive.Size = new System.Drawing.Size(426, 103);
			this.richTextBoxReceive.TabIndex = 5;
			this.richTextBoxReceive.Text = "richTextBoxReceive";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(480, 384);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 24);
			this.button1.TabIndex = 6;
			this.button1.Text = "button1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(32, 64);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 7;
			this.textBox1.Text = "textBox1";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
			this.ClientSize = new System.Drawing.Size(554, 417);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.richTextBoxSend);
			this.Controls.Add(this.buttonRequest);
			this.Controls.Add(this.textBoxIP);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBoxPort);
			this.Controls.Add(this.buttonSend);
			this.Controls.Add(this.buttonClose);
			this.Controls.Add(this.richTextBoxReceive);
			this.Name = "Form1";
			this.Text = "客户端";
this.Closing+=new system.ComponentModel.CancelEventHandler(this.Form1_Closing);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private void buttonRequest_Click(object sender, System.EventArgs e)
		{
			IPEndPoint server=new IPEndPoint(IPAddress.Parse("127.0.0.1"),6788);
socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
			try
			{
				socket.Connect(server);
			}
			catch
			{
				MessageBox.Show("与服务器连接失败!");
				return;
			}
			this.buttonRequest.Enabled=false;
			this.listBoxState.Items.Add("与服务器连接成功");
            NetworkStream netStream=new NetworkStream(socket);
					
			Thread thread=new Thread(new ThreadStart(AcceptMessage));
			thread.Start();
			//
             
			///

		}

		private void AcceptMessage()
		{
			while(true)
			{
				try
				{
					NetworkStream netStream=new NetworkStream(socket);
					byte[] datasize=new byte[4];
					netStream.Read(datasize,0,4);
					int size=System.BitConverter.ToInt32(datasize,0);
					Byte[] message=new byte[size];
					int dataleft=size;
					int start=0;
					while(dataleft>0)
					{
						int recv=netStream.Read(message,start,dataleft);
						start+=recv;
						dataleft-=recv;
					}
					this.richTextBoxReceive.Rtf=System.Text.Encoding.Unicode.GetString(message);
				}
				catch
				{
					this.listBoxState.Items.Add("服务器断开连接。");
					break;
				}
			}
		}

		private void buttonClose_Click(object sender, System.EventArgs e)
		{
			try
			{
				socket.Shutdown(SocketShutdown.Both);
				socket.Close();
				this.listBoxState.Items.Add("与主机断开连接");
				thread.Abort();
			}
			catch
			{
				MessageBox.Show("尚未与主机连接,断开无效!");
			}
			this.buttonRequest.Enabled=true;
		}

		private void buttonSend_Click(object sender, System.EventArgs e)
		{
			string str=this.richTextBoxSend.Rtf;
			int i=str.Length;
			if(i==0)
			{
				return;
			}
			else
			{
		 //因为str为Unicode编码,每个字符占2字节,所以实际字节数应*2
				i*=2;
			}
			byte[] datasize=new byte[4];
			//将32位整数值转换为字节数组
			datasize=System.BitConverter.GetBytes(i); 
			byte[] sendbytes=System.Text.Encoding.Unicode.GetBytes(str);
			try
			{
				if(fs==0){
                 //datasize=System.BitConverter.GetBytes(i); 
					NetworkStream netStream=new NetworkStream(socket);
					netStream.Write(fdatasize,0,4);
					netStream.Write(fsendbytes,0,fsendbytes.Length);
					netStream.Flush();
					this.richTextBoxSend.Text=fsendbytes.ToString();
				 fs=1;
					this.textBox1.Text=fs.ToString();
				}
				else
				{
					fs=2;
					NetworkStream netStream=new NetworkStream(socket);
					netStream.Write(datasize,0,4);
					netStream.Write(sendbytes,0,sendbytes.Length);
					netStream.Flush();
					this.richTextBoxSend.Text="";
					this.textBox1.Text=fs.ToString();
				}
			}
			catch
			{
				MessageBox.Show("无法发送!");
			}
		}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			try
			{
				socket.Shutdown(SocketShutdown.Both);
				socket.Close();
			}
			catch
			{}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			string str=this.richTextBoxSend.Rtf;
			int i=str.Length;
			if(i==0)
			{
				return;
			}
			else
			{
			//因为str为Unicode编码,每个字符占2字节,所以实际字节数应*2
				i*=2;
			}
			byte[] datasize=new byte[4];
			//将32位整数值转换为字节数组
			datasize=System.BitConverter.GetBytes(i); 
			byte[] sendbytes=System.Text.Encoding.Unicode.GetBytes(str);
			try
			{
				NetworkStream netStream=new NetworkStream(socket);
				netStream.Write(datasize,0,4);
				netStream.Write(sendbytes,0,sendbytes.Length);
				netStream.Flush();
				this.richTextBoxSend.Text="";
			}
			catch
			{
				MessageBox.Show("无法发送!");
			}
		}

	}
}
        2、开发UDP点对点、组播程序
       //UDP点对点
       using System;
using System.Net;
using System.Net.Sockets;
class TestSimpleUdp
{
	public static void Main()
	{
		int length;
		byte[] bytes=new byte[1024];
		Socket socket=

⌨️ 快捷键说明

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