Beispiel #1
0
        private void SendFurtherMsg(object msg)
        {
            //编码方法 Id+发送方号码+接收方号码+日期+时间+内容+发送方的Id  (多了一个)
            String[] Msgs     = ((String)msg).Split(' ');
            DateTime SendTime = new DateTime();

            SendTime = DateTime.Parse(Msgs[3] + " " + Msgs[4]);
            while (SendTime.CompareTo(DateTime.Now) > 0)
            {
                Thread.Sleep(1000);//毫秒
            }
            //开始定位接收方
            bool flag = false;

            for (int i = 0; i < Singlephone.PhoneNum; i++)
            {
                Singlephone ReceivePhone = Singlephone.GetSinglePhone(i);
                //找到接收方
                if (ReceivePhone.UserPhone == Msgs[2])
                {
                    flag = true;
                    if (ReceivePhone.GetStatus() == true) //表示还在登陆
                    {
                        //发给收件方
                        String SendMsg = Msgs[0] + " " + Msgs[1] + " " + Msgs[2] + " " + Msgs[3] + " " + Msgs[4] + " " + Msgs[5];
                        ReceivePhone.Send(SendMsg);
                        //回执给发件方
                        int Sid = int.Parse(Msgs[Msgs.Length - 1]);
                        Singlephone.GetSinglePhone(Sid).Send("Info " + Msgs[2] + " SUCCESS");
                    }
                    else //对方没登陆 要缓存和回执 这里的回执一定要发件人登陆~
                    {
                        //缓存
                        int Id = ReceivePhone.Id;
                        ((UnMsg)UnMessages[Id]).Add((String)msg);
                        //回执
                        int id = int.Parse(Msgs[Msgs.Length - 1]);
                        Singlephone.GetSinglePhone(id).Send("Info " + Msgs[2] + " NOTONLINE");
                    }
                    break;
                }
            }
            if (!flag)
            {
                int Sid = int.Parse(Msgs[Msgs.Length - 1]);
                Singlephone.GetSinglePhone(Sid).Send("Info " + Msgs[2] + " NOREGISTER");
            }
        }
Beispiel #2
0
        public void SendUnMsg()
        {
            Singlephone RPhone = Singlephone.GetSinglePhone(Id);//在队列中获得收件人信息

            //对于每一个短信
            for (int i = 0; i < MsgBox.Count; i++)
            {
                //先发出去
                RPhone.Send(MsgBox[i]);
                //再发回执
                String[]    Msgs   = MsgBox[i].Split(' ');
                int         SendId = int.Parse(Msgs[Msgs.Length - 1]);//最后是发送人的Id
                Singlephone SPhone = Singlephone.GetSinglePhone(SendId);
                if (SPhone != null)
                {
                    SPhone.Send("Info " + Msgs[2] + " DELAY"); //短信编号
                }
                Thread.Sleep(100);
            }
            MsgBox.Clear();
            FileStream file = new FileStream(RPhone.Id.ToString(), FileMode.Create);  //这个相当于清空吧

            file.Close();
        }
Beispiel #3
0
        public void GetChat(Singlephone phone)
        {
            //登陆进来过后
            //(1)发送未发送的消息 查找收件人的未发送队列
            UnMessages.Insert(phone.Id, new UnMsg(phone.Id, phone.UserPhone));
            ((UnMsg)UnMessages[phone.Id]).InitUnMsg();// 从文件中读取出来
            if (UnMessages[phone.Id] != null)
            {
                ((UnMsg)UnMessages[phone.Id]).SendUnMsg();
            }

            //(2)处理接收到的短消息
            bool logoutflag = false;

            while (true)
            {
                if (logoutflag)
                {
                    break;
                }
                String Msg = phone.Receive();
                if (Msg == "")
                {
                    continue;
                }                //空消息不处理
                if (Msg == null) //表示退出了
                {
                    phone.Logout();
                    break;
                }
                String[] Msgs = Msg.Split('\0');
                foreach (String msg in Msgs)  //对每一条客户端的消息
                {
                    //分割是用来定位发给谁的!!!!
                    String[] SegMent = msg.Split(' ');
                    //编码方法 Id+发送方号码+接收方号码+日期+时间+内容
                    if (SegMent.Length < 6)         //表示发送的编码有问题
                    {
                        if (SegMent[0] == "logout") //如果是注销 进行判断
                        {
                            phone.Logout();
                            logoutflag = true;
                            break;
                        }
                        continue;
                    }

                    //在这里实现定时短信发送,开一个线程实现
                    if (Convert.ToDateTime(SegMent[3] + " " + SegMent[4]).CompareTo(DateTime.Now) > 0)
                    {
                        Thread FurtherMsg = new Thread(SendFurtherMsg);
                        String Newmsg     = msg + " " + phone.Id.ToString();
                        FurtherMsg.Start(Newmsg);
                        continue;
                    }
                    bool flag = false; //用来标志有没有 曾经登陆
                    for (int i = 0; i < Singlephone.PhoneNum; i++)
                    {
                        Singlephone ReceivePhone = Singlephone.GetSinglePhone(i);
                        //找到接收方
                        if (ReceivePhone.UserPhone == SegMent[2])
                        {
                            flag = true;
                            if (ReceivePhone.GetStatus() == true)
                            {
                                String SendMsg = msg;
                                if (ReceivePhone.Send(SendMsg))
                                {
                                    //向发送方返回短信状态 Info+Id+状态
                                    phone.Send("Info " + SegMent[2] + " SUCCESS");
                                }
                                else//表示发送有问题
                                {
                                    //向发送方返回短信状态
                                    phone.Send("Info " + SegMent[2] + " FAIL");
                                }
                            }
                            else //对方没有在线 加入发信箱
                            {
                                int    Sid     = ReceivePhone.Id;
                                String SendMsg = msg + " " + phone.Id.ToString();
                                ((UnMsg)UnMessages[Sid]).Add(SendMsg);
                            }
                            break;
                        }
                    }
                    if (!flag)  //没有注册
                    {
                        Singlephone.GetSinglePhone(phone.Id).Send("Info " + SegMent[2] + " NOREGISTER");
                    }
                }
            }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            //端口没有错误设置,必须输入正确
            #region 端口设置
            IPHostEntry ipe      = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress   ServerIp = ipe.AddressList[1];
            Int32       Port;
            String      temp = "13000";
            Port = int.Parse(temp);
            #endregion



            #region 初始化用户列表 和未读短信
            Singlephone.InitPhone();
            #endregion

            #region 开始服务
            NetworkStream stream = null;
            TcpServer = new TcpListener(ServerIp, Port);
            TcpServer.Start();
            Console.WriteLine("服务器开启成功~");
            Console.WriteLine("服务器IP:" + ServerIp + "   端口号" + Port);
            while (true)
            {
                //用来监听新用户的连接  并且用来产生阻塞
                TcpClient NewClient = TcpServer.AcceptTcpClient();
                //获得登陆信息
                stream = NewClient.GetStream();
                Byte[] bytes = new Byte[1024];
                String TempPhone;
                try
                {
                    int length = stream.Read(bytes, 0, bytes.Length);
                    TempPhone = System.Text.Encoding.UTF8.GetString(bytes, 0, length);
                }
                catch (Exception e)
                {
                    Console.WriteLine("登录出现问题");
                    continue;
                }
                bool flag = false;
                for (int i = 0; i < Singlephone.PhoneNum; i++)
                {
                    if (Singlephone.GetSinglePhone(i).UserPhone == TempPhone) //找到相同的
                    {
                        flag = true;
                        if (Singlephone.GetSinglePhone(i).GetStatus() == false)//表示曾经登陆过
                        {
                            Byte[] Msg = System.Text.Encoding.UTF8.GetBytes("Info -1 " + "LEGAL");
                            try
                            {
                                stream.Write(Msg, 0, Msg.Length);
                                Console.WriteLine("重新登录");
                                //先重新登录 再传入Client
                                Singlephone.GetSinglePhone(i).Login(NewClient);  //重新登陆
                                //再开一个线程来处理和其交互消息
                                Thread NewThread = new Thread(Process);
                                NewThread.Start(Singlephone.GetSinglePhone(i));
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("连接出现问题");
                            }
                        }
                        else  //表示发生两个号码重复登陆
                        {
                            Byte[] Msg = System.Text.Encoding.UTF8.GetBytes("Info -1 " + "LEGAL");
                            try
                            {
                                Console.WriteLine("号码重复非法");
                                stream.Write(Msg, 0, Msg.Length);
                                //可以允许
                                Singlephone.GetSinglePhone(i).Login(NewClient);
                                //再开一个线程来处理和其交互消息
                                Thread NewThread = new Thread(Process);
                                NewThread.Start(Singlephone.GetSinglePhone(i));
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("连接出现问题");
                            }
                        }
                        break; //跳出循环
                    }
                }
                if (!flag) //如果没有以前记录
                {
                    Byte[] Msg = System.Text.Encoding.UTF8.GetBytes("Info -1 " + "LEGAL");
                    try
                    {
                        stream.Write(Msg, 0, Msg.Length);
                        //有了的话就新建立对象并进行登陆
                        Singlephone NewPhone = new Singlephone(NewClient, TempPhone);
                        //开线程处理
                        Thread NewThread = new Thread(Process);
                        NewThread.Start(NewPhone);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("连接出现问题");
                    }
                }
            }
            #endregion
        }