⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operationcenter.cs

📁 .Net实现的局域网完整IM,含完整的设计远隔
💻 CS
📖 第 1 页 / 共 3 页
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.IO;



namespace JPMLab_LanTalker_Client
{
    public class icUser
    {
        public int _uID;
        public string _account;
        public string _password;
        public string _nickName;
        public int _headPic;
        public int _myShow;
        public int _sex;
        public string _birthDay;
        public string _bewrite;
        public string _openTime;
        public string _lastTime;
        public string _lastIP;
        public int _lastPort;
    }

    public class icMessage
    {
        public int _UID;
        public string _Message;
    }

    static class OperationCenter
    {
        /// <summary>
        /// Consts
        /// </summary>
        const int LOCATION_MESSAGEPORT = 1414;

        const int CMESSAGE_LENGTH_HEAD = 24;

        const char MESSAGE_TYPE_COMMAND = 'a';
        const char MESSAGE_TYPE_REGISTER = 'b';
        const char MESSAGE_TYPE_LOGIN = 'c';
        const char MESSAGE_TYPE_SEARCHUSERS = 'd';
        const char MESSAGE_TYPE_REQUESTFRIEND = 'e';
        const char MESSAGE_TYPE_AGEEBEFRIEND = 'f';
        const char MESSAGE_TYPE_DELETEFRIEND = 'g';
        const char MESSAGE_TYPE_NEWUSERINFO = 'h';
        const char MESSAGE_TYPE_MESSAGE = 'i';
        const char MESSAGE_TYPE_FILE = 'j';
        const char MESSAGE_TYPE_EXIT = 'k';

        const char MSGCMD_TYPE_MESSAGE = 'm';

        const int MESSAGE_UID_SERVER = -1;

        const char LOGIN_RESULT_COMLETE = 'l';
        const char LOGIN_RESULT_EACCOUNT = 'a';
        const char LOGIN_RESULT_EPASSWORD = 'p';
        const char LOGIN_RESULT_STOP = 's';
        const char LOGIN_RESULT_HOT = 'h';

        const char USER_STATE_NORMAL = 'n';
        const char USER_STATE_HOT = 'h';
        const char USER_STATE_STOP = 's';

        public const char SEARCHUSERS_TYPE_ALL = 'a';
        public const char SEARCHUSERS_TYPE_UID = 'i';
        public const char SEARCHUSERS_TYPE_NICKNAME = 'n';

        /// <summary>
        /// Some currency control
        /// </summary>
        public static System.Windows.Forms.ImageList _HPicList;
        public static System.Drawing.Icon[] _HIconList = new System.Drawing.Icon[18];
        public static frmMain _frmMain;
        public static System.Windows.Forms.NotifyIcon _MainNot;
        /// <summary>
        /// Current data
        /// </summary>
        public static icUser CurrentMe;
        public static System.Collections.ArrayList _FriendList = new System.Collections.ArrayList();
        public static System.Collections.ArrayList _MsgBuffer = new System.Collections.ArrayList();
        public static System.Collections.ArrayList _TalkFormList = new System.Collections.ArrayList();
        /// <summary>
        /// Operation currency fields
        /// </summary>
        static string _server_Address = "127.0.0.1";
        static int _server_Port = 2424;

        static string _my_Address = System.Net.Dns.GetHostAddresses(System.Net.Dns.GetHostName())[0].ToString();
        static int _my_Port = LOCATION_MESSAGEPORT;

        static System.Net.Sockets.TcpClient _socServerLink;
        static System.IO.BinaryWriter _bWriter;
        static System.IO.BinaryReader _bReader;

        static Thread _thdMsgListener;
        static System.Net.Sockets.TcpListener _socListener;
        static System.Net.Sockets.Socket _tempSession;
        static bool _keepListen = true;
        static bool _keepAnswer = true;
        static bool _keepAlarm = false;

        public static string Server_Address
        {
            set
            {
                _server_Address = value;
            }
            get
            {
                return _server_Address;
            }
        }

        public static int Server_Port
        {
            set
            {
                _server_Port = value;
            }
            get
            {
                return _server_Port;
            }
        }

        public static int Location_Port
        {
            set
            {
                _my_Port = value;
            }
            get
            {
                return _my_Port;
            }
        }

        /// <summary>
        /// Thread function
        /// </summary>
        /*
        static void thdfunMsgListener()
        {
            _socMsgListener = new TcpListener(_my_Port);

            _socMsgListener.Start();
            while (_keepListen)
            {
                System.Net.Sockets.Socket t_NSocket = _socMsgListener.AcceptSocket();
                System.Net.Sockets.NetworkStream t_NetStream = new NetworkStream(t_NSocket);
                System.IO.BinaryReader t_BReader = new BinaryReader(t_NetStream);
                System.IO.BinaryWriter t_BWriter = new BinaryWriter(t_NetStream);
                int t_SUID, t_MType;

                t_SUID = t_BReader.ReadInt32();
                t_MType = t_BReader.ReadChar();

                if (t_SUID == MESSAGE_UID_SERVER)
                {
                    if (t_MType == MESSAGE_TYPE_MESSAGE)
                    {

                        SureMessageEx(t_BReader.ReadString(), global::JPMLab_LanTalker_Client.Properties.Resources.STR_MSGTITLE_SERVER);
                    }
                    else if (t_MType == MESSAGE_TYPE_ADDFRIEND)
                    {

                        if (EstimateMessage(t_BReader.ReadString(), global::JPMLab_LanTalker_Client.Properties.Resources.STR_MSGTITLE_SERVER) == frmMessage.MR_YES)
                        {
                            t_BWriter.Write('t');
                        }
                        else
                        {
                            t_BWriter.Write('f');
                        }
                    }
                    else if (t_MType == MESSAGE_TYPE_FILE)
                    {
                    }
                }
                else
                {
                     
                    string t_Msg;
                    int t_n, t_c, t_SUID;
                    icUser t_icUser;

                    
                    t_Msg = t_BReader.ReadString();

                    t_SUID = t_SUID;
                    t_c = _FriendList.Count;
                    for (t_n = 0; t_n < t_c; t_n++)
                    {
                        t_icUser = (icUser)_FriendList[t_n];

                        if (t_icUser._uID == t_SUID)
                        {
                            frmTalk t_frmTalk;

                            t_icUser._lastIP = t_LTMsg.SAddress.ToString();
                            t_icUser._lastPort = t_LTMsg.SPort;

                            t_c = _TalkFormList.Count;
                            for (t_n = 0; t_n < t_c; t_n++)
                            {
                                t_frmTalk = (frmTalk)_TalkFormList[t_n];
                                if (t_frmTalk.HisUID == t_LTMsg.SUID)
                                {
                                    t_frmTalk.AddMessage(t_Msg);
                                }
                            }

                            if (t_n == t_c)
                            {
                                t_frmTalk = new frmTalk(t_SUID);

                                _TalkFormList.Add(t_frmTalk);

                                t_frmTalk.Show();
                                t_frmTalk.AddMessage(t_Msg);
                            }
                        }
                    }
                }

                // _MessageList.Add(t_LTMsg);

                t_BReader.Close();
                t_BWriter.Close();
                t_NetStream.Close();
                t_NSocket.Close();
            }

            _socMsgListener.Stop();
            _socMsgListener = null;
        }

        /// <summary>
        /// Boot function
        /// </summary>

        public static void StartNetwork()
        {
            if (_thdListener != null)
            {
                _keepListen = false;
            }
            while (_thdListener != null) ;
            _thdListener = new Thread(new ThreadStart(thdfunMsgListener));
            _keepListen = true;
            _thdListener.Start();
        }

        public static void StopNetwork()
        {
            _keepListen = false;
            while (_socMsgListener != null) ;
        }

        public static void ResetNetwork()
        {
            _keepListen = false;
            while (_socMsgListener != null) ;
            StartNetwork();
        }
*/
        /// <summary>
        /// Operation current function
        /// </summary>

        public static int SureMessage(string Msg, string MTitle)
        {
            frmMessage t_f = new frmMessage();

            t_f.MessageStr = Msg;
            t_f.MessageTitle = MTitle;
            t_f.ShowDialog();

            return t_f._messageResult;
        }

        public static int SureMessageEx(string Msg, string MTitle)
        {
            frmMessage t_f = new frmMessage();

            t_f.MessageStr = Msg;
            t_f.MessageTitle = MTitle;
            t_f.Show();

            return t_f._messageResult;
        }

        public static int EstimateMessage(string Msg, string MTitle)
        {
            frmMessage t_f = new frmMessage();

            t_f._messageType = frmMessage.MT_YNC;
            t_f.MessageStr = Msg;
            t_f.MessageTitle = MTitle;
            t_f.ShowDialog();

            return t_f._messageResult;
        }

        /// <summary>
        /// Thread functions
        /// </summary>

        static void thdfunMsgAlarmer()
        {
            int t_ii;

            _keepAlarm = true;
            t_ii = 0;
            while (_MsgBuffer.Count > 0)
            {
                int t_n, t_c;
                int t_SID = ((icMessage)_MsgBuffer[0])._UID;
                int t_HI = 0;
                icUser t_OU;

                t_c = _FriendList.Count;
                for (t_n = 0; t_n < t_c; t_n++)
                {
                    t_OU = (icUser)_FriendList[t_n];
                    if (t_OU._uID == t_SID)
                    {
                        t_HI = t_OU._headPic;
                        break;
                    }
                }

                if (t_ii == 0)
                {
                    _MainNot.Icon = _HIconList[t_HI];
                    t_ii = 1;
                }
                else
                {
                    _MainNot.Icon = _frmMain.Icon;
                    t_ii = 0;
                }

                Thread.Sleep(500);
            }

            _MainNot.Icon = _frmMain.Icon;
            _keepAlarm = false;
        }

        static void thdfunSession()
        {
            try
            {
                if (_tempSession != null)
                {

                    System.Net.Sockets.Socket t_socSession = _tempSession;
                    System.Net.Sockets.NetworkStream t_NStream = new NetworkStream(t_socSession);
                    System.IO.BinaryReader t_BReader = new BinaryReader(t_NStream);
                    System.IO.BinaryWriter t_BWriter = new BinaryWriter(t_NStream);
                    char t_MType;

                    _tempSession = null;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -