结合windows服务的Socket聊天室

windows service是一种window服务,与计算机一起启动,然后保持一直运行的状态。比较适合于服务器的监听,例如基于socket的聊天室程序,基于remoting的程序,都要有服务器端的支持,如果把它们做到windows service,那将是个完美的组合。

下面就介绍一个socket聊天室与windows结合的例子。。。

1. 首先建立一个空白的解决方案,命名为ChatSocket.

2. 添加一个注册表项,用来设置客户端和服务器通讯的端口号。

运行->输入regedit->此例子中在HKEY_LOCAL_MACHINE\SOFTWARE\Socket\Server中设置Port字符串键值。

2. 添加一个Class Library解决方案,命名为ClientEntity。

在类Client.cs中添加如下代码。

public class Client
{
    private Thread clthread;
    private EndPoint endpoint;
    private string name;
    private Socket sock;

    public Client(string _name, EndPoint _endpoint, Thread _thread, Socket _sock)
    {
        // TODO: 在此处添加构造函数逻辑 
        clthread = _thread;
        endpoint = _endpoint;
        name = _name;
        sock = _sock;
    }

    public override string ToString()
    {
        return endpoint.ToString() + " : " + name;
    }

    public Thread CLThread
    {
        get { return clthread; }
        set { clthread = value; }
    }

    public EndPoint Host
    {
        get { return endpoint; }
        set { endpoint = value; }
    }

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    public Socket Sock
    {
        get { return sock; }
        set { sock = value; }
    }
}

因为客户实体类要能被windows服务所访问,所以暂且注册到GAC中。

首先,生成一个强命文件,添加到配置信息中。

Microsoft Visual Studio 2005->Visual Studio Tools->启用Visual Studio 2005命令提示。输入:sn –k ClientEntity.snk生成强名文件。

在配置文件中添加,[assembly: AssemblyKeyFile("ClientEntity.snk")].

 

注册客户实体类:

在visual studio command命令行中进入ClientEntity.dll所在目录,gacutil -i ClientEntity.dll—安装程序集。默认情况下,gacutil.exe文件放在C:"Program Files"Microsoft Visual Studio 8"SDK"v2.0"Bin目录中。

客户实体类已经成功注册到GAC中,可以通过运行->assembly来查看,这个时候本机的所有程序都可以访问他了。

 

2. 添加一个新的windows service的解决方案,命名为ChatServer,并且引用ClientEntity解决方案。

首先新建一个读取注册表的类,用来从注册表中读取端口号和服务器地址等信息。

public class ReadRegistry
{
    public static string getValue(RegistryHive hive, string subKey, string value)
    {
        RegistryKey reg = null;

        switch (hive)
        {
            case RegistryHive.CurrentUser:
                reg = Registry.CurrentUser;
                break;
            case RegistryHive.LocalMachine:
                reg = Registry.LocalMachine;
                break;
            default:
                break;
        }

        RegistryKey regSubKey = reg.OpenSubKey(subKey);

        string str = regSubKey.GetValue(value).ToString();
        return str;
    }
}

 

然后新建一个ServerStart类用来控制socket服务器的监听工作,windows服务调用它从而始终处于监听状态。

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net.Sockets;
using System.Threading;

using ClientEntity;
using System.Net;

namespace ChatServer
{
    public class ServerStart
    {
        private static readonly int listenport = Convert.ToInt32(ReadRegistry.getValue(Microsoft.Win32.RegistryHive.LocalMachine, @"SOFTWARE\Socket\Server", "Port"));
        ArrayList clientsArray;
        Socket clientSocket;
        Thread clientThread;
        Thread serverThread;

        public ServerStart()
        {
            clientsArray = new ArrayList();
        }

        private void ListenClient()
        {
            TcpListener listener = new TcpListener(listenport);
            listener.Start();

            while (true)
            {
                try
                {
                    Socket s = listener.AcceptSocket();
                    clientSocket = s;
                    clientThread = new Thread(new ThreadStart(serviceClient));
                    clientThread.Start();
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void serviceClient()
        {
            Socket client = clientSocket;
            bool keepAlive = true;

            while (keepAlive)
            {
                Byte[] buffer = new Byte[1024];
                int bufLen = 0;

                try
                {
                    bufLen = client.Available;
                    client.Receive(buffer, 0, bufLen, SocketFlags.None);

                    if (bufLen == 0)
                    {
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    return;
                }

                string clientcommand = System.Text.Encoding.ASCII.GetString(buffer).Substring(0, bufLen);

                string[] tokens = clientcommand.Split(new Char[] { '|' });
                Console.WriteLine(clientcommand);

                if (tokens[0] == "CONN")
                {
                    for (int n = 0; n < clientsArray.Count; n++)
                    {
                        Client cl = (Client)clientsArray[n];
                        SendToClient(cl, "JOIN|" + tokens[1]);
                    }
                    EndPoint ep = client.RemoteEndPoint;
                    Client c = new Client(tokens[1], ep, clientThread, client);

                    string message = "LIST|" + GetChatterList() + "\r\n";
                    SendToClient(c, message);

                    clientsArray.Add(c);

                    //lbClients.Items.Add(c);
                }
                if (tokens[0] == "CHAT")
                {
                    for (int n = 0; n < clientsArray.Count; n++)
                    {
                        Client cl = (Client)clientsArray[n];
                        SendToClient(cl, clientcommand);
                    }
                }
                if (tokens[0] == "PRIV")
                {
                    string destclient = tokens[3];
                    for (int n = 0; n < clientsArray.Count; n++)
                    {
                        Client cl = (Client)clientsArray[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 = clientsArray.Count;
                    for (int n = 0; n < clientsArray.Count; n++)
                    {
                        Client cl = (Client)clientsArray[n];
                        SendToClient(cl, clientcommand);
                        if (cl.Name.CompareTo(tokens[1]) == 0)
                        {
                            remove = n;
                            found = true;
                            //lbClients.Items.Remove(cl);
                        }
                    }
                    if (found)
                    {
                        clientsArray.RemoveAt(remove);
                    }
                    client.Close();
                    keepAlive = false;
                }
            }
        }

        private void SendToClient(Client client, string clientCommand)
        {
            Byte[] message = System.Text.Encoding.ASCII.GetBytes(clientCommand);
            Socket s = client.Sock;
            if (s.Connected)
            {
                s.Send(message, message.Length, 0);
            }
        }

        private string GetChatterList()
        {
            string result = "";

            for (int i = 0; i < clientsArray.Count; i++)
            {
                result += ((Client)clientsArray[i]).Name + "|";
            }
            return result;
        }
    }
}

在windows service解决方案的service.cs的Design界面中,右击Add Installer将添加一个安装类。可以修改安装服务的相关属性。。。

 

3. 到此处可以注册windows服务了。首先要编译一下解决方案。。。

注册windows服务:

还是在visual studio command命令行中。输入installutil ChatServer.exe来注册window service程序到windows服务中。

这个时候你可以通过运行->services.msc来查看是否安装成功。

 

部署完服务器以后,算是完成大部分工作了,最后还要有一个客户端程序来访问,当多个人同时加入时,可以互相群聊。。。

 

4. 客户端程序。 

namespace Client
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnConnect = new System.Windows.Forms.Button();
            this.clientName = new System.Windows.Forms.TextBox();
            this.btnDisconnect = new System.Windows.Forms.Button();
            this.statusBar1 = new System.Windows.Forms.StatusBar();
            this.btnSend = new System.Windows.Forms.Button();
            this.ChatOut = new System.Windows.Forms.TextBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.rtbChatIn = new System.Windows.Forms.RichTextBox();
            this.lbChatters = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            //
            // btnConnect
            //
            this.btnConnect.Location = new System.Drawing.Point(324, 57);
            this.btnConnect.Name = "btnConnect";
            this.btnConnect.Size = new System.Drawing.Size(53, 30);
            this.btnConnect.TabIndex = 18;
            this.btnConnect.Text = "连接";
            this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click);
            //
            // clientName
            //
            this.clientName.Location = new System.Drawing.Point(77, 12);
            this.clientName.Name = "clientName";
            this.clientName.Size = new System.Drawing.Size(83, 20);
            this.clientName.TabIndex = 17;
            this.clientName.Text = "name";
            //
            // btnDisconnect
            //
            this.btnDisconnect.Enabled = false;
            this.btnDisconnect.Location = new System.Drawing.Point(324, 109);
            this.btnDisconnect.Name = "btnDisconnect";
            this.btnDisconnect.Size = new System.Drawing.Size(53, 30);
            this.btnDisconnect.TabIndex = 16;
            this.btnDisconnect.Text = "断开";
            this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click);
            //
            // statusBar1
            //
            this.statusBar1.Location = new System.Drawing.Point(0, 272);
            this.statusBar1.Name = "statusBar1";
            this.statusBar1.Size = new System.Drawing.Size(394, 21);
            this.statusBar1.TabIndex = 15;
            this.statusBar1.Text = "statusBar1";
            //
            // btnSend
            //
            this.btnSend.Location = new System.Drawing.Point(277, 235);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(62, 22);
            this.btnSend.TabIndex = 14;
            this.btnSend.Text = "send";
            this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
            //
            // ChatOut
            //
            this.ChatOut.Location = new System.Drawing.Point(110, 235);
            this.ChatOut.Name = "ChatOut";
            this.ChatOut.Size = new System.Drawing.Size(114, 20);
            this.ChatOut.TabIndex = 13;
            this.ChatOut.Text = "message";
            //
            // checkBox1
            //
            this.checkBox1.Location = new System.Drawing.Point(10, 235);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(87, 23);
            this.checkBox1.TabIndex = 12;
            this.checkBox1.Text = "checkBox1";
            //
            // rtbChatIn
            //
            this.rtbChatIn.Location = new System.Drawing.Point(130, 42);
            this.rtbChatIn.Name = "rtbChatIn";
            this.rtbChatIn.Size = new System.Drawing.Size(174, 164);
            this.rtbChatIn.TabIndex = 11;
            this.rtbChatIn.Text = "";
            //
            // lbChatters
            //
            this.lbChatters.Location = new System.Drawing.Point(24, 42);
            this.lbChatters.Name = "lbChatters";
            this.lbChatters.Size = new System.Drawing.Size(93, 147);
            this.lbChatters.TabIndex = 10;
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(394, 293);
            this.Controls.Add(this.btnConnect);
            this.Controls.Add(this.clientName);
            this.Controls.Add(this.btnDisconnect);
            this.Controls.Add(this.statusBar1);
            this.Controls.Add(this.btnSend);
            this.Controls.Add(this.ChatOut);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.rtbChatIn);
            this.Controls.Add(this.lbChatters);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button btnConnect;
        private System.Windows.Forms.TextBox clientName;
        private System.Windows.Forms.Button btnDisconnect;
        private System.Windows.Forms.StatusBar statusBar1;
        private System.Windows.Forms.Button btnSend;
        private System.Windows.Forms.TextBox ChatOut;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.RichTextBox rtbChatIn;
        private System.Windows.Forms.ListBox lbChatters;
    }
}

 

 

类文件,用来与服务器通讯。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Threading;
using System.IO;

namespace Client
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        NetworkStream nws;
        StreamReader sr;
        TcpClient tcpClient;
        Thread clientThread;

        bool connected;
        string server = "127.0.0.1";
        int port = 6666;

        string clientname;

        private void btnConnect_Click(object sender, EventArgs e)
        {
            EstablishConnection();
            RegisterWithServer();
            if (connected)
            {
                clientThread = new Thread(new ThreadStart(ReceiveChat));
                clientThread.Start();
            }
        }

        private void EstablishConnection()
        {
            statusBar1.Text = "正在连接到服务器";

            try
            {
                tcpClient = new TcpClient(server, port);
                nws = tcpClient.GetStream();
                sr = new StreamReader(nws);
                connected = true;

            }
            catch (Exception)
            {
                MessageBox.Show("不能连接到服务器!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                statusBar1.Text = "已断开连接";
            }
        }

        private void RegisterWithServer()
        {
            lbChatters.Items.Clear();

            clientname = clientName.Text;
            try
            {
                string command = "CONN|" + clientname; //+"\r\n";
                Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
                nws.Write(outbytes, 0, outbytes.Length);


                string serverresponse = sr.ReadLine();
                serverresponse.Trim();
                string[] tokens = serverresponse.Split('|');
                if (tokens[0] == "LIST")
                {
                    statusBar1.Text = "已连接";
                    btnDisconnect.Enabled = true;
                }
                if (tokens[1] != "")
                {
                    for (int n = 1; n < tokens.Length; n++)
                    {
                        lbChatters.Items.Add(tokens[n].Trim(new char[] { '\r', '\n' }));
                    }
                }
                this.Text = clientname + ":已连接到服务器";

            }
            catch (Exception ex)
            {
                MessageBox.Show("注册时发生错误!" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                connected = false;
            }
        }

        private void ReceiveChat()
        {
            bool keepalive = true;
            while (keepalive)
            {
                try
                {
                    Byte[] buffer = new Byte[1024];
                    nws.Read(buffer, 0, buffer.Length);
                    string chatter = System.Text.Encoding.ASCII.GetString(buffer);
                    string[] tokens = chatter.Split(new Char[] { '|' });

                    if (tokens[0] == "CHAT")
                    {
                        rtbChatIn.AppendText(tokens[1]);
                    }
                    if (tokens[0] == "PRIV")
                    {
                        rtbChatIn.AppendText("Private from ");
                        rtbChatIn.AppendText(tokens[1].Trim());
                        rtbChatIn.AppendText(tokens[2] + "\r\n");
                    }
                    if (tokens[0] == "JOIN")
                    {
                        rtbChatIn.AppendText(tokens[1].Trim());
                        rtbChatIn.AppendText(" has joined the Chat\r\n");

                        string newguy = tokens[1].Trim(new char[] { '\r', '\n' });
                        lbChatters.Items.Add(newguy);
                    }
                    if (tokens[0] == "GONE")
                    {
                        rtbChatIn.AppendText(tokens[1].Trim());
                        rtbChatIn.AppendText(" has left the Chat\r\n");

                        lbChatters.Items.Remove(tokens[1].Trim(new char[] { '\r', '\n' }));
                    }
                    if (tokens[0] == "QUIT")
                    {
                        nws.Close();
                        tcpClient.Close();
                        keepalive = false;
                        statusBar1.Text = "服务器端已停止";
                        connected = false;
                        btnSend.Enabled = false;
                        btnDisconnect.Enabled = false;
                    }
                }
                catch (Exception) { }
            }
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            QuitChat();
        }

        private void QuitChat()
        {
            if (connected)
            {
                try
                {
                    string command = "GONE|" + clientname;
                    Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
                    nws.Write(outbytes, 0, outbytes.Length);
                    tcpClient.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

            if (clientThread != null && clientThread.IsAlive)
            {
                clientThread.Abort();
            }
            this.Text = "客户端";

            connected = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (connected)
            {
                try
                {
                    string command = "CHAT|" + clientname + ": " + ChatOut.Text + "\r\n";
                    Byte[] outbytes = System.Text.Encoding.ASCII.GetBytes(command.ToCharArray());
                    nws.Write(outbytes, 0, outbytes.Length);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}

至此,所有工作全部完成,提供一个下载版本。ChatSocket示例代码下载

posted @ 2009-01-22 23:47  -Timothy-  阅读(2945)  评论(0编辑  收藏  举报