一个不错的网络编程例子

源代码在线查看: serverform.cs

软件大小: 29 K
上传用户: nihao464667180
关键词: 网络编程
下载地址: 免注册下载 普通下载 VIP

相关代码

				/************************************************************
				
				 ServerForm.cs
				 CopyRight 2000-2001
				 This is a sample program made by Saurabh Nandu.
				 E-mail: saurabh@mastercsharp.com
				 Website: http://www.mastercsharp.com
				 Socket Chat
				Compilation:
				csc /t:winexe /out:..\ChatServer.exe ServerForm.cs Client.cs
				
				11/September/2001
				************************************************************/
				using System;
				using System.Drawing;
				using System.Collections;
				using System.ComponentModel;
				using System.Windows.Forms;
				using System.Data;
				using System.Threading;
				using System.Net;
				using System.Net.Sockets;
				
				namespace SocketServer
				{
					//Public delegate
					public delegate void LogUpdater(string msg);
					
					public class Form1 : System.Windows.Forms.Form
					{
						//Private Variables
						private System.Windows.Forms.ListBox logBox;
						private Thread serverThread;
						private TcpListener serverListener;
						private Hashtable clientTable;
						
						private System.ComponentModel.Container components = null;
				
						public Form1()
						{
							InitializeComponent();
							clientTable = new Hashtable();
							//Start a thread on the startListen method
							serverThread = new Thread(new ThreadStart(startListen));
							serverThread.Start();
							AddLog("Socket Server Started");
						}
				
						public void startListen()
						{
							try
							{
								//Start the tcpListner
								serverListener = new TcpListener(5151);
								serverListener.Start();
								do
								{
									//Create a new class when a new Chat Client connects
									Client newClient = new Client(serverListener.AcceptTcpClient());
									//Attach the Delegates
									newClient.Disconnected+= new DisconnectDelegate(OnDisconnected);
									newClient.Connected+=new ConnectDelegate(this.OnConnected);
									newClient.MessageReceived+=new MessageDelegate(OnMessageReceived);
									//Connect to the clients
									newClient.Connect();
								}
								while(true);
							}
							catch
							{
								serverListener.Stop();
							}
						}
				
						//EvntsHandler fo the Connected event
						public void OnConnected(object sender, EventArgs e)
						{
							//Get the client that raised the vent
							Client temp = (Client)sender;
							//Add the client to the Hashtable
							clientTable.Add(temp.ID,temp);
							Client tempClient;
							AddLog("Client Connected:"+temp.UserName);
							//loop through each client and announce the 
							//client connected
							foreach(DictionaryEntry d in clientTable)
							{
								tempClient =(Client)d.Value;
								tempClient.Send(tempClient.ID+"@Connected@"+temp.UserName);
							}
						}
				
						public void OnDisconnected(object sender, EventArgs e)
						{
							//Get the Client that raised the Event
							Client temp =(Client)sender;
							//If the Client exists in the Hashtable
							if(clientTable.ContainsKey(temp.ID))
							{
								AddLog("Client Disconnected:"+temp.UserName);
								//Remove the client from the hashtable
								clientTable.Remove(temp.ID);
								//Remove the client from the ClientList class
								ClientList.RemoveClient(temp.UserName,temp.ID);
								Client tempClient;
								//Announce to all the existing clients
								foreach(DictionaryEntry d in clientTable)
								{
									tempClient =(Client)d.Value;
									tempClient.Send(tempClient.ID+"@Disconnected@"+temp.UserName);
								}
							}
						}
				
						public void OnMessageReceived(object sender, MessageEventArgs e)
						{
							//Message sender client
							Client temp = (Client)sender;
							AddLog(temp.UserName+" :"+e.Message);
							Client tempClient;
							//Send the message to each client
							foreach(DictionaryEntry d in clientTable)
							{
								tempClient =(Client)d.Value;
								tempClient.Send(temp.UserName+" :"+e.Message);
							}
						}
						//Method to add the string to the server log
						public void AddLog(string msg)
						{
							logBox.Items.Add(msg);
						}
						/// 
						/// Clean up any resources being used.
						/// 
						protected override void Dispose( bool disposing )
						{
							if( disposing )
							{
								if (components != null) 
								{
									if(serverListener!=null)
										serverListener.Stop();
									components.Dispose();
								}
							}
							base.Dispose( disposing );
						}
				
						#region Windows Form Designer generated code
						/// 
						/// Required method for Designer support - do not modify
						/// the contents of this method with the code editor.
						/// 
						private void InitializeComponent()
						{
							this.logBox = new System.Windows.Forms.ListBox();
							this.SuspendLayout();
							// 
							// logBox
							// 
							this.logBox.Dock = System.Windows.Forms.DockStyle.Fill;
							this.logBox.Name = "logBox";
							this.logBox.Size = new System.Drawing.Size(368, 368);
							this.logBox.TabIndex = 0;
							// 
							// Form1
							// 
							this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
							this.ClientSize = new System.Drawing.Size(368, 373);
							this.Controls.AddRange(new System.Windows.Forms.Control[] {
																						  this.logBox});
							this.Name = "Form1";
							this.Text = "Socket Server";
							this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
							this.ResumeLayout(false);
				
						}
						#endregion
				
						/// 
						/// The main entry point for the application.
						/// 
						[STAThread]
						static void Main() 
						{
							Application.Run(new myServer());
						}
				
						private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
						{
							serverListener.Stop();
						}
					}
				}
							

相关资源