zoukankan      html  css  js  c++  java
  • Telnet Chat Daemon

    Latest Snippet Version: 0.1

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Threading;
    /*
    Copyright (c) 2002, Stefan Muenzel (smuenzel@users.sourceforge.net)
    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    -Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    -Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    -Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


    */
    namespace telnetchatd
    {    
        public class Utility
        {
            public static readonly byte[] Backspace = {0x08};
            public static readonly byte[] CrLf = {0x0D, 0x0A};
            private Utility(){}


            public static byte[] StrToByte(string str)
            {
                char[] car= str.ToCharArray();
                byte[] bar= new byte[car.Length];
                int i = 0;
                foreach (char c in car)
                {
                    bar[i] = (byte)c;
                    i++;
                }
                return bar;
            }
            public static void SendString(Socket sock, string s)
            {
                if(sock == null)
                    throw new System.NullReferenceException("(SendString) No Socket to Send");
                sock.Send(StrToByte(s));
            }
            
            //write to local console
            public static void prLocal(string str)
            {
                prLog("<Daemon> " + str);
                Console.WriteLine("<Daemon> " + str);
            }

            //Write to log with timestamp
            public static void prLog(string str)
            {
                System.DateTime mt = System.DateTime.Now;
                Stream stream = File.Open("log.log",FileMode.Append,FileAccess.Write,FileShare.ReadWrite);
                StreamWriter fstream = new StreamWriter(stream);
                fstream.WriteLine ("$" +mt.ToLongTimeString() + "$ " + str);
                fstream.Close();
                stream.Close();
            }
        }

        public class ChatD
        {
            static ChatD mInstance;
            private IPEndPoint mPort = new IPEndPoint(IPAddress.Any,12345);
        
            private System.Collections.ArrayList mConnected;
            private Socket mSockListen;
            
            /* Class used to represent one client, all clients are saved in mConnected, and delete themselves
             * when the remote side disconnects
             */
            public class  cClient
            {
                
                string WELCOME = ".NET Telnet Chat Service\r\n"+
                    "To talk just type and press <ENTER>.\r\n" +
                    "If you would like to change your client name, type '$' followed by the new name.\r\n"+
                    "Please turn local echo off.\r\n";
                //private string Send;
                private Thread mThread; //The thread this client runs in

                internal string UserName;
                internal string Current;
                internal Socket Sock;
                internal int BackSpaceCount; //how many backspace the user has accumulated

                ~cClient() //Never called, since RequestFinalize() doesn't exist
                {
                    Utility.prLog("~" + this.UserName + " destroyed.");
                }
                public cClient()
                {
                    this.Sock = null;
                    this.Current = "";
                    this.BackSpaceCount = 0;
                    System.GC.ReRegisterForFinalize(this); //does nothing at all
                }

                public void DoInNewThread()
                {
                    ThreadStart ts = new ThreadStart(this.ThreadMethod);
                    mThread = new Thread(ts);
                    mThread.Start();
                }

                private void ThreadMethod()
                {
                    byte[] buffer = new byte[24];
                    Utility.SendString(this.Sock,WELCOME);
                    int crec = 0; //length of incoming data
                    while(true)
                    {
                        //Check if i'm supposed to be dead:
                        if(!ChatD.mInstance.mConnected.Contains(this))
                        {
                            break;
                        }
                        //Incoming Loop
                        try
                        {
                            if(this.Sock.Poll(100,SelectMode.SelectRead)) //Poll the socket for incoming data
                            {
                                if((crec =this.Sock.Receive(buffer)) == 0) //if received data = 0, the remote side closed
                                {
                                    Sock.Shutdown(SocketShutdown.Both);
                                    Sock.Close();
                                    break;
                                }
                                else
                                {
                                    if(((System.Collections.IList)buffer).Contains((byte)'\n') ||
                                        ((System.Collections.IList)buffer).Contains((byte)'\r'))
                                    {
                                    //When a newline is contained, emit the string
                                        if(Current.Length > 0)
                                        {
                                            Sock.Send(Utility.CrLf);
                                            if(Current[0] == '$') //name change
                                            {
                                                ChatD.mInstance.BroadCast(null,">>"+this.UserName +
                                                    "<< changed name to " + Current.Substring(1),true);
                                                this.UserName = Current.Substring(1);
                                            }
                                            else
                                            {
                                                //Emit the string to all other  clients
                                                ChatD.mInstance.BroadCast(this,Current);
                                            }
                                            Current = "";
                                        }
                                    }
                                    else
                                    {
                                        if(!char.IsControl((char)buffer[0]) && !(buffer[0] == 0x08))
                                        {
                                        //add the incoming characters to the buffer
                                            this.Sock.Send(buffer);
                                            for(int i = 0; i < crec; i++)
                                            {
                                                this.Current += (char)buffer[i];
                                                //this.Current = this.Current.Remove(this.Current.Length -(24-crec+1),(24-crec+1));
                                                if( this.BackSpaceCount > 0)this.BackSpaceCount --;
                                            }
                                        }
                                        else if(buffer[0] == 0x08 && Current.Length > 0)
                                        {
                                        //Process backspaces
                                            try
                                            {
                                                this.Current = this.Current.Remove(this.Current.Length -1,1);
                                                this.BackSpaceCount ++;
                                                this.Sock.Send(buffer);
                                            }
                                            catch(System.ArgumentOutOfRangeException ex)
                                            {
                                            }
                                        }
                                    }
                                    //make new buffer
                                    buffer = new byte[24];
                                }
                            }
                        }
                        catch (System.Net.Sockets.SocketException sx)
                        {
                            Utility.prLocal("Error receiving from client " + this.Sock.RemoteEndPoint.ToString() + " ("
                                + this.UserName + ")");
                            Utility.prLocal("\tError:" + sex.ErrorCode.ToString()  + ", Native:"
                                + sx.NativeErrorCode.ToString () + ", " + sx.Message);
                            break;
                        }
                    }
                    //Destruction:
                    ChatD.mInstance.mConnected.Remove(this);
                    ChatD.mInstance.BroadCast(null,this.UserName + " has left the server.");
                }
            };

            [STAThread]
            static void Main(string[] args)
            {
                Utility.prLocal("Initializing...");
                ChatD baseDaemon = new ChatD();
            }

            public ChatD()
            {
                ChatD.mInstance = this;
                mConnected = new System.Collections.ArrayList(21);
                mSockListen = new Socket(AddressFamily.InterNetwork,
                    SocketType.Stream,ProtocolType.Tcp); //create the listening socket
                Utility.prLocal("Initializing completed.");
                this.Commence();
            }

            ~ChatD()
            {
                Utility.prLocal("Shuting down...");
            }

            public void Commence()
            {
                mSockListen.Bind(mPort); //Bind and listen
                DoListen();
            }

            public void DoListen()
            {
                cClient tempclient = new cClient();
                int tempn;
                mSockListen.Listen(2048);
                while(true){
                    if(mSockListen.Poll(10000,SelectMode.SelectRead)) //Poll the socket and create clients on incoming connections
                    {
                        Utility.prLocal("Incoming connection...");
                        tempclient.Sock = mSockListen.Accept();
                        tempclient.UserName = "New User";
                        tempclient.Current = "";
                        Utility.prLocal("\t...from " + tempclient.Sock.RemoteEndPoint.ToString());
                        this.BroadCast(null,">>" +tempclient.UserName + "<< joined the server.");
                        tempn = mConnected.Add(tempclient);
                        ((cClient)mConnected[tempn]).DoInNewThread();
                        tempclient = new cClient();
                    }                
                }
            }
            public void BroadCast(cClient broadcaster, string s)
            {
                this.BroadCast(broadcaster, s,false);
            }
            public void BroadCast(cClient broadcaster, string s, bool ne)
            {
                string tstr;

                if(broadcaster == null) //Daemon
                {
                    tstr = s +"\r\n";
                }
                else
                {
                    tstr = "<"+broadcaster.UserName+"> "+s +"\r\n";
                }
                byte[] bar = Utility.StrToByte(tstr);
                //Send to every client:
                foreach(object ob in this.mConnected)
                {
                    try
                    {
                        //send "\r\n" to clients that have typed something
                        if((((cClient)ob).Current.Length > 2) && ( ob != broadcaster) ) ((cClient)ob).Sock.Send(Utility.CrLf);
                        ((cClient)ob).Sock.Send(bar); //Send String
                        //Echo their current string and backspaces back:
                        if(!ne) if( ob != broadcaster) ((cClient)ob).Sock.Send(Utility.StrToByte(((cClient)ob).Current));
                        for(int i = 0; i< ((cClient)ob).BackSpaceCount; i++)
                            ((cClient)ob).Sock.Send(Utility.Backspace);
                    }
                    catch(System.Net.Sockets.SocketException sx)
                    {
                        Utility.prLocal("Error sending to client " + ((cClient)ob).Sock.RemoteEndPoint.ToString() + " ("
                            + ((cClient)ob).UserName + ")");
                        Utility.prLocal("\tError:" + sx.ErrorCode.ToString()  + ", Native:" + sx.NativeErrorCode.ToString ()
                            + ", " + sex.Message);
                        this.mConnected.Remove (ob);
                    }
                }
                tstr = tstr.Substring(0,tstr.Length -2) + '\n'; //make fit for writing to file
                Utility.prLog(tstr);
                Console.Write(tstr);
            }

        }
    }
  • 相关阅读:
    CDN缓存服务器现状,squid、nginx、trafficserver、ATS性能测试
    [钉钉通知系列]Jenkins发布后自动通知
    查税号,税号查询,纳税人识别号查询
    体验万象优图鉴黄服务
    红象云腾
    我眼中的领域驱动设计
    手工DIY:手机变身扫描仪_极客迷
    【CZURET-16】成者科技(CZUR)ET16智能扫描仪OCR文字识别书籍文档票据零边距高速A3A4高拍仪高清1600万像素【行情 报价 价格 评测】-京东
    技术漫谈 | 使用docker-compose进行python开发
    饿了么的 PWA 升级实践
  • 原文地址:https://www.cnblogs.com/zhuor/p/308944.html
Copyright © 2011-2022 走看看