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

📄 chatserver.cs

📁 基于SOCKET的聊天室
💻 CS
📖 第 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;


namespace ChatServer
{
	public class ClientSeverForm : System.Windows.Forms.Form
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		
		private System.Windows.Forms.GroupBox grpSocket;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox tbSocketPort;
		private System.Windows.Forms.TextBox tbSocketClientsNum;
		private System.Windows.Forms.RichTextBox rtbSocketMsg;
		private System.Windows.Forms.Button btnSocketStart;
		private System.Windows.Forms.Button btnSocketStop;
		private System.Windows.Forms.ListBox lbSocketClients;

		//clients数组保存当前在线用户的Client对象
		internal static Hashtable clients=new Hashtable();
		
		//该服务器默认的监听的端口号
		private TcpListener listener;
		
		//服务器可以支持的最多的客户端的连接数
		static int MAX_NUM=100;
		
		//开始服务的标志
		internal static bool SocketServiceFlag = false;

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

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

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ClientSeverForm));
			this.grpSocket = new System.Windows.Forms.GroupBox();
			this.lbSocketClients = new System.Windows.Forms.ListBox();
			this.btnSocketStop = new System.Windows.Forms.Button();
			this.rtbSocketMsg = new System.Windows.Forms.RichTextBox();
			this.btnSocketStart = new System.Windows.Forms.Button();
			this.tbSocketPort = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.tbSocketClientsNum = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.grpSocket.SuspendLayout();
			this.SuspendLayout();
			// 
			// grpSocket
			// 
			this.grpSocket.Controls.Add(this.lbSocketClients);
			this.grpSocket.Controls.Add(this.btnSocketStop);
			this.grpSocket.Controls.Add(this.rtbSocketMsg);
			this.grpSocket.Controls.Add(this.btnSocketStart);
			this.grpSocket.Controls.Add(this.tbSocketPort);
			this.grpSocket.Controls.Add(this.label1);
			this.grpSocket.Controls.Add(this.tbSocketClientsNum);
			this.grpSocket.Controls.Add(this.label3);
			this.grpSocket.Location = new System.Drawing.Point(16, 16);
			this.grpSocket.Name = "grpSocket";
			this.grpSocket.Size = new System.Drawing.Size(784, 368);
			this.grpSocket.TabIndex = 13;
			this.grpSocket.TabStop = false;
			this.grpSocket.Text = "Socket连接监听";
			// 
			// lbSocketClients
			// 
			this.lbSocketClients.Location = new System.Drawing.Point(8, 64);
			this.lbSocketClients.Name = "lbSocketClients";
			this.lbSocketClients.ScrollAlwaysVisible = true;
			this.lbSocketClients.Size = new System.Drawing.Size(200, 290);
			this.lbSocketClients.TabIndex = 21;
			// 
			// btnSocketStop
			// 
			this.btnSocketStop.Enabled = false;
			this.btnSocketStop.Location = new System.Drawing.Point(528, 32);
			this.btnSocketStop.Name = "btnSocketStop";
			this.btnSocketStop.Size = new System.Drawing.Size(88, 24);
			this.btnSocketStop.TabIndex = 20;
			this.btnSocketStop.Text = "Socket停止";
			this.btnSocketStop.Click += new System.EventHandler(this.btnSocketStop_Click);
			// 
			// rtbSocketMsg
			// 
			this.rtbSocketMsg.Location = new System.Drawing.Point(216, 64);
			this.rtbSocketMsg.Name = "rtbSocketMsg";
			this.rtbSocketMsg.Size = new System.Drawing.Size(552, 296);
			this.rtbSocketMsg.TabIndex = 19;
			this.rtbSocketMsg.Text = "";
			// 
			// btnSocketStart
			// 
			this.btnSocketStart.Location = new System.Drawing.Point(424, 32);
			this.btnSocketStart.Name = "btnSocketStart";
			this.btnSocketStart.Size = new System.Drawing.Size(88, 24);
			this.btnSocketStart.TabIndex = 18;
			this.btnSocketStart.Text = "Socket启动";
			this.btnSocketStart.Click += new System.EventHandler(this.btnSocketStart_Click);
			// 
			// tbSocketPort
			// 
			this.tbSocketPort.Location = new System.Drawing.Point(328, 32);
			this.tbSocketPort.Name = "tbSocketPort";
			this.tbSocketPort.Size = new System.Drawing.Size(83, 20);
			this.tbSocketPort.TabIndex = 17;
			this.tbSocketPort.Text = "1234";
			this.tbSocketPort.TextChanged += new System.EventHandler(this.tbSocketPort_TextChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(232, 31);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 21);
			this.label1.TabIndex = 16;
			this.label1.Text = "Socket端口号:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// tbSocketClientsNum
			// 
			this.tbSocketClientsNum.Location = new System.Drawing.Point(112, 32);
			this.tbSocketClientsNum.Name = "tbSocketClientsNum";
			this.tbSocketClientsNum.ReadOnly = true;
			this.tbSocketClientsNum.TabIndex = 15;
			this.tbSocketClientsNum.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 36);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(97, 16);
			this.label3.TabIndex = 13;
			this.label3.Text = "当前在线用户:";
			// 
			// ClientSeverForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(818, 407);
			this.Controls.Add(this.grpSocket);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MinimizeBox = false;
			this.Name = "ClientSeverForm";
			this.Text = "聊天室服务器";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.ClientSeverForm_Closing);
			this.grpSocket.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

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

		private string getIPAddress()
		{
			// 获得本机局域网IP地址
			IPAddress[] AddressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
			if (AddressList.Length<1)
			{
				return "";
			}
			
			return AddressList[0].ToString();
		} 

		private static string getDynamicIPAddress( )
		{
			// 获得拨号动态分配IP地址
			IPAddress[] AddressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
			if (AddressList.Length<2)
			{
				return "";
			}
			
			return AddressList[1].ToString();
		}

		private int getValidPort(string port)
		{
			int lport;

			//测试端口号是否有效
			try
			{
				//是否为空
				if (port=="") 
				{
					throw new ArgumentException(
						"端口号为空,不能启动服务器");
				}
				lport = System.Convert.ToInt32(port);
			}
			catch (Exception e)
			{
				//ArgumentException, 
				//FormatException, 
				//OverflowException
				Console.WriteLine("无效的端口号:" + e.ToString());
				this.rtbSocketMsg.AppendText("无效的端口号:" + e.ToString()+"\n");
				return -1;
			}
			return lport;
		}

		//当单击“Socket启动”按钮时,便开始监听指定的Socket端口
		private void btnSocketStart_Click(object sender, System.EventArgs e)
		{
			int port = getValidPort(tbSocketPort.Text);
			if (port<0)
			{
				return;
			}

			string ip = this.getIPAddress();
			try
			{
				IPAddress ipAdd=IPAddress.Parse(ip);
				//创建服务器套接字
				listener=new TcpListener(ipAdd, port);
				//开始监听服务器端口
				listener.Start();
				this.rtbSocketMsg.AppendText("Socket服务器已经启动,正在监听" + 
					ip + " 端口号:" + this.tbSocketPort.Text + "\n");

				//启动一个新的线程,执行方法this.StartSocketListen,
				//以便在一个独立的进程中执行确认与客户端Socket连接的操作
				ClientSeverForm.SocketServiceFlag = true;
				Thread thread=new Thread(new ThreadStart(this.StartSocketListen));
				thread.Start();
				this.btnSocketStart.Enabled = false;
				this.btnSocketStop.Enabled = true;
			}
			catch(Exception ex)
			{
				this.rtbSocketMsg.AppendText(ex.Message.ToString()+"\n");
			}		
		}

		//在新的线程中的操作,它主要用于当接收到一个客户端请求时,确认与客户端的连接,
		//并且立刻启动一个新的线程来处理和该客户端的信息交互。
		private void StartSocketListen()
		{
			while(ClientSeverForm.SocketServiceFlag)
			{
				try
				{
					//当接收到一个客户端请求时,确认与客户端的连接
					if (listener.Pending())
					{
						Socket socket=listener.AcceptSocket();
						if(clients.Count>=MAX_NUM)
						{
							this.rtbSocketMsg.AppendText("已经达到了最大连接数:" + 
								MAX_NUM + ",拒绝新的连接\n");
							socket.Close();
						}
						else
						{
							//启动一个新的线程,
							//执行方法this.ServiceClient,处理用户相应的请求
							Client client = new Client(this, socket);
							Thread clientService=new Thread(
								new ThreadStart(client.ServiceClient));
							clientService.Start();
						}
					}
					Thread.Sleep(200);

⌨️ 快捷键说明

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