当前位置:编程学习 > C#/ASP.NET >>

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
            {
      &n

上一个:一个用C#实现的简单http server(转自中华技术网 www.driverdevelop.com),给楼下的朋友
下一个:在WinNT及Win2000中实现读取网卡物理地址的一段C程序,熟悉C的人员可作参考改进。

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,