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

📄 myclient.cs

📁 一个不错的网络编程例子
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Text;
namespace SocketClient
{
	/// <summary>
	/// myClient 的摘要说明。
	/// </summary>
	public delegate void DisDelegate(string val);
	public class myClient : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Button button2;
		private TcpClient client;
		string userName,ID;
		private bool first=true;
		private StringBuilder strMsg;
		private byte[] recByte=new byte[1024];
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public myClient()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();
			strMsg=new StringBuilder();
			//
			// 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.textBox1 = new System.Windows.Forms.TextBox();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.BackColor = System.Drawing.SystemColors.InactiveBorder;
			this.textBox1.Location = new System.Drawing.Point(8, 8);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(512, 216);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// listBox1
			// 
			this.listBox1.ItemHeight = 12;
			this.listBox1.Location = new System.Drawing.Point(544, 0);
			this.listBox1.Name = "listBox1";
			this.listBox1.Size = new System.Drawing.Size(176, 220);
			this.listBox1.TabIndex = 1;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 240);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(48, 23);
			this.label1.TabIndex = 2;
			this.label1.Text = "信息:";
			// 
			// textBox2
			// 
			this.textBox2.Enabled = false;
			this.textBox2.Location = new System.Drawing.Point(64, 240);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(368, 21);
			this.textBox2.TabIndex = 3;
			this.textBox2.Text = "textBox2";
			// 
			// button1
			// 
			this.button1.Enabled = false;
			this.button1.Location = new System.Drawing.Point(440, 240);
			this.button1.Name = "button1";
			this.button1.TabIndex = 4;
			this.button1.Text = "发送";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(536, 240);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 16);
			this.label2.TabIndex = 5;
			this.label2.Text = "用户名:";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(584, 240);
			this.textBox3.Name = "textBox3";
			this.textBox3.TabIndex = 6;
			this.textBox3.Text = "";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(688, 240);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(56, 23);
			this.button2.TabIndex = 7;
			this.button2.Text = "登陆";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// myClient
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(744, 277);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.listBox1);
			this.Controls.Add(this.textBox1);
			this.MinimizeBox = false;
			this.Name = "myClient";
			this.Text = "myClient";
			this.ResumeLayout(false);

		}
		#endregion
		private void DisplayInfo(string msg)
		{
			this.textBox1.AppendText(msg);
		}
		public void AddUser(string user)
		{
			if(!this.listBox1.Items.Contains(user))
				listBox1.Items.Add(user);
			if(!first)
				DisplayInfo(user+"已经加入!");
		}
		public void RemoveUser(string user)
		{
			if(listBox1.Items.Contains(user))
				listBox1.Items.Remove(user);
			DisplayInfo(user+"已经退出!");
		}
		private void Send(string msg)
		{
			StreamWriter sw=new StreamWriter(client.GetStream());
			sw.WriteLine(msg);
			sw.Flush();
		}
		private void button1_Click(object sender, System.EventArgs e)
		{
			if(this.textBox2.Text!="")
			{
				Send(textBox2.Text);
				textBox2.Text="";
			}
		}
		public void GetMsg(IAsyncResult ar)
		{
		   int intcount=0;
			try
			{
				intcount=client.GetStream().EndRead(ar);
			
				if(intcount<1)
				{
					Disconnect();
					return;
				}
				BuildText(recByte,0,intcount);
				if(!first)
				{
					AsyncCallback msg=new AsyncCallback(GetMsg);
					client.GetStream().BeginRead(recByte,0,1024,msg,null);
				}
			}
			catch
			{
				Disconnect();
			}

		}
		private void BuildText(byte[] data,int offset,int len)
		{
			for(int i=offset;i<len;i++)
			{
				if(data[i]==10)
					continue;   //意思是当data[i]=10时 继续循环  不执行下面的语句!!!
				strMsg.Append(Convert.ToChar(data[i]));
			}
			char[] split={'@'};
			if(first)
			{
				string[] tmpStr=strMsg.ToString().Split(split);
				if(tmpStr[0]=="sorry")
				{
					object[] msg={tmpStr[1]};
					this.Invoke(new DisDelegate(DisplayInfo),msg);
					Disconnect();
				}
				else
				{
					this.ID=tmpStr[0];
					for(int i=1;i<tmpStr.Length;i++)
					{
						object[] msg={tmpStr[i]};
						this.Invoke(new DisDelegate(AddUser),msg);
					}
					first=false;
					AsyncCallback ac=new AsyncCallback(GetMsg);
					client.GetStream().BeginRead(recByte,0,1024,ac,null);

				}
			}
			else
			{
				if(strMsg.ToString().IndexOf(this.ID)>=0)
				{
					string[] tmpStr=strMsg.ToString().Split(split);
					if(tmpStr[1]=="Connect")
					{
						object[] user={tmpStr[2]};
						this.Invoke(new DisDelegate(AddUser),user);
					}
					if(tmpStr[1]=="Disconnect")
					{
						object[] user={tmpStr[2]};
						this.Invoke(new DisDelegate(RemoveUser),user);
					}
				}
				else
				{
					strMsg.Append("\r\n");
					object[] tmp={strMsg.ToString()};
					this.Invoke(new DisDelegate(DisplayInfo),tmp);
				}
			}
			strMsg=new StringBuilder();
		}
		private void button2_Click(object sender, System.EventArgs e)
		{
			if(this.button2.Text=="登陆"&&this.textBox3.Text!="")
			{
				try
				{
					client=new TcpClient("localhost",5151);
					DisplayInfo("连接到服务器....\r\n");
					AsyncCallback recMsg=new AsyncCallback(GetMsg);
					(client.GetStream()).BeginRead(recByte,0,1024,recMsg,null);
					Send(this.textBox3.Text);
					this.userName=this.textBox3.Text;
					this.Text="客户端:"+this.textBox3.Text;
					this.textBox3.Enabled=false;
					this.AcceptButton=this.button1;
					this.button2.Text="断开";
					this.textBox3.Text="";
					this.textBox2.Enabled=true;
					this.button1.Enabled=true;
				}
				catch
				{
			
					Disconnect();
				}
			}
			else
			{
				textBox2.Enabled=false;
				button1.Enabled=false;
				button2.Text="登陆";
				this.AcceptButton=this.button2;
				Disconnect();
			}
		}
		private void Disconnect()
		{
			if(client!=null)
			{
				client.Close();
				client=null;
			}
		}
	}
}

⌨️ 快捷键说明

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