Form1.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:5k
源码类别:
C#编程
开发平台:
Others
- 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
- {
- /// <summary>
- /// Form1 的摘要说明。
- /// </summary>
- public class Form1 : System.Windows.Forms.Form
- {
- private int listenport = 5555;
- private TcpListener listener;
- private ArrayList clients;
- private Thread processor;
- private Socket clientsocket;
- private Thread clientservice;
- private System.Windows.Forms.ListBox lbClients;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public Form1()
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- clients = new ArrayList();
- processor = new Thread(new ThreadStart(StartListening));
- processor.Start();
- }
- /// <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>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.lbClients = new System.Windows.Forms.ListBox();
- this.SuspendLayout();
- //
- // lbClients
- //
- this.lbClients.ItemHeight = 12;
- this.lbClients.Location = new System.Drawing.Point(24, 24);
- this.lbClients.Name = "lbClients";
- this.lbClients.Size = new System.Drawing.Size(248, 232);
- this.lbClients.TabIndex = 0;
- //
- // Form1
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(292, 273);
- this.Controls.AddRange(new System.Windows.Forms.Control[] {
- this.lbClients});
- this.Name = "Form1";
- this.Text = "服务器端";
- this.ResumeLayout(false);
- }
- #endregion
- private void StartListening()
- {
- listener = new TcpListener(listenport);
- listener.Start();
- while (true)
- {
- try
- {
- Socket s = listener.AcceptSocket();
- clientsocket = s;
- clientservice = new Thread(new ThreadStart(ServiceClient));
- clientservice.Start();
- }
- catch(Exception e)
- {
- Console.WriteLine(e.ToString() );
- }
- }
- }
- private void ServiceClient()
- {
- Socket client = clientsocket;
- bool keepalive = true;
- while (keepalive)
- {
- Byte[] buffer = new Byte[1024];
- client.Receive(buffer);
- string clientcommand = System.Text.Encoding.ASCII.GetString(buffer);
- string[] tokens = clientcommand.Split(new Char[]{'|'});
- Console.WriteLine(clientcommand);
- if (tokens[0] == "CONN")
- {
- for(int n=0; n<clients.Count; n++)
- {
- Client cl = (Client)clients[n];
- SendToClient(cl, "JOIN|" + tokens[1]);
- }
- EndPoint ep = client.RemoteEndPoint;
- Client c = new Client(tokens[1], ep, clientservice, client);
- clients.Add(c);
- string message = "LIST|" + GetChatterList() +"rn";
- SendToClient(c, message);
- lbClients.Items.Add(c);
- }
- if (tokens[0] == "CHAT")
- {
- for(int n=0; n<clients.Count; n++)
- {
- Client cl = (Client)clients[n];
- SendToClient(cl, clientcommand);
- }
- }
- if (tokens[0] == "PRIV")
- {
- string destclient = tokens[3];
- for(int n=0; n<clients.Count; n++)
- {
- Client cl = (Client)clients[n];
- if(cl.Name.CompareTo(tokens[3]) == 0)
- SendToClient(cl, clientcommand);
- if(cl.Name.CompareTo(tokens[1]) == 0)
- SendToClient(cl, clientcommand);
- }
- }
- if (tokens[0] == "GONE")
- {
- int remove = 0;
- bool found = false;
- int c = clients.Count;
- for(int n=0; n<c; n++)
- {
- Client cl = (Client)clients[n];
- SendToClient(cl, clientcommand);
- if(cl.Name.CompareTo(tokens[1]) == 0)
- {
- remove = n;
- found = true;
- lbClients.Items.Remove(cl);
- }
- }
- if(found)
- clients.RemoveAt(remove);
- client.Close();
- keepalive = false;
- }
- }
- }
- private void SendToClient(Client cl, string message)
- {
- try
- {
- byte[] buffer = System.Text.Encoding.ASCII.GetBytes(message.ToCharArray());
- cl.Sock.Send(buffer,buffer.Length,0);
- }
- catch(Exception)
- {
- cl.Sock.Close();
- cl.CLThread.Abort();
- clients.Remove(cl);
- lbClients.Items.Remove(cl.Name + " : " + cl.Host.ToString());
- }
- }
- private string GetChatterList()
- {
- string chatters = "";
- for(int n=0; n<clients.Count; n++)
- {
- Client cl = (Client)clients[n];
- chatters += cl.Name;
- chatters += "|";
- }
- chatters.Trim(new char[]{'|'});
- return chatters;
- }
- /// <summary>
- /// 应用程序的主入口点。
- /// </summary>
- [STAThread]
- static void Main()
- {
- Application.Run(new Form1());
- }
- }
- }