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

📄 myclient.cs

📁 一个不错的网络编程例子
💻 CS
字号:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Collections;
using System.Text;
namespace SocketServer
{
	
	public class MessageArg:EventArgs
	{
		string msg;
		public string Message
		{
			get{return msg;}
			set{msg=value;}
		}
	}
	/// <summary>
	/// myclient 的摘要说明。
	/// </summary>
	public class myclient
	{
		public event ConnectDelegate connected;
		public event DisconnectDelegate disconnect;
		public event MessageDelegate message;
		private bool first=true;
		private bool connect=false;
	    private	StringBuilder str=new StringBuilder();
		private byte[] recByte=new byte[1024];
		private TcpClient myClient;
		private string userName,clientId;
		public myclient(TcpClient _client)
		{
			myClient=_client;
		}
		public string UserName
		{
			get{return userName;}
		}
		public string ClientID
		{
			get{return clientId;}
		}
		public void Connect()
		{
			this.clientId=Guid.NewGuid().ToString();
			AsyncCallback getMsgStream=new AsyncCallback(getMsg);
			myClient.GetStream().BeginRead(recByte,0,1024,getMsgStream,null);
			
		}
		private void getMsg(IAsyncResult ar)
		{
			int intCount;
			try
			{
				lock(myClient.GetStream())
				{
					intCount=myClient.GetStream().EndRead(ar);
				}
				if(intCount<1)
				{
					myClient.Close();
					if(disconnect!=null)
						disconnect(this,new System.EventArgs());
				}
				BuilderText(recByte,0,intCount);
				if(!first)
				{
					lock(myClient.GetStream())
					{
						AsyncCallback getMsg=new AsyncCallback(this.getMsg);
						myClient.GetStream().BeginRead(this.recByte,0,1024,getMsg,null);
					}

				}
			}
			catch
			{
				myClient.Close();
				if(disconnect!=null)
					disconnect(this,new System.EventArgs());
			}
		}
		private void BuilderText(byte[] data,int offset,int length)
		{
			for(int i=offset;i<length;i++)
			{
				if(data[i]==13)
				{	
					if(first)
					{
						CheckUserName(str.ToString());
						first=false;
					}
					else if(message!=null&&connect)
						{
							MessageEventArgs e=new MessageEventArgs();
							e.Message=str.ToString();
						      message(this,e);
					}
					str=new StringBuilder();
				}
				else
				{
					str.Append(Convert.ToChar(data[i]));
				}
			}
		}
		private void CheckUserName(string name)
		{
			if(name.Length>20)
			{
				Send("sorry@您的名称不合法!");
				this.Disconnect();
				return;
			}
			else if(name.IndexOf('@')>0)
			{
				Send("sorry@您的名称不合法!");
				Disconnect();
				return;
			}
			else if(!myClientList.AddClient(name,clientId))
			{
				Send("sorry@您的名称重复!");
				Disconnect();
				return;
			}
			else
			{
				this.connect=true;
				this.userName=name;
				StringBuilder sb=new StringBuilder();
				sb.Append(this.clientId);
				Hashtable ht=myClientList.getList;
				foreach(DictionaryEntry d in ht)
				{
					sb.Append("@");
					sb.Append(d.Value.ToString());
				}
				lock(myClient.GetStream())
				{
					AsyncCallback recMsg=new AsyncCallback(this.getMsg);
					myClient.GetStream().BeginRead(this.recByte,0,1024,recMsg,null);
				}
				Send(sb.ToString());
				if(connected!=null)
					connected(this,new System.EventArgs());
			}

		}
		public void Send(string msg)
		{
			lock(myClient.GetStream())
			{
				System.IO.StreamWriter sw=new System.IO.StreamWriter(myClient.GetStream());
				sw.Write(msg);
				sw.Flush();
			}
		}
		public void Disconnect()
		{
			this.connect=false;
			myClient.Close();
		}
	}
	public class myClientList
	{
		public static Hashtable clientTable=new Hashtable();
		public static bool AddClient(string name,string id)
		{
			lock(clientTable)
			{
				if(clientTable.ContainsValue(name))
				{
					return false;
				}
				else
				{
					clientTable.Add(id,name);
					return true;
				}
			}
		}
		public static bool RemoveClient(string id,string name)
		{
			lock(clientTable)
			{
				if(clientTable.ContainsValue(name))
				{
					clientTable.Remove(id);
					return true;
				}
				else
					return false;
			}
		}
		public static Hashtable getList
		{
			get{return clientTable;}
		}
	}
}

⌨️ 快捷键说明

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