예제 #1
0
        private void ScreenShotButton_Click(object sender, EventArgs e)//点击“截图”图标
        {
            InfoWindow Mes = new InfoWindow("功能尚未开放\n    敬请期待", false);

            Mes.StartPosition = FormStartPosition.CenterParent;
            Mes.ShowDialog();
        }
예제 #2
0
        private void button2_Click(object sender, EventArgs e)//点击"退出登录"按键,执行下线功能
        {
            InfoWindow Confirm = new InfoWindow("确认退出当前账号?", true);

            Confirm.StartPosition = FormStartPosition.CenterParent;
            Confirm.ShowDialog();
            if (Confirm.DialogResult == DialogResult.OK)//确认退出
            {
                Confirm.Close();
                TcpClient client = new TcpClient();
                try
                {
                    client.Connect(BasicInfo.ServerIp, BasicInfo.ServerPort);
                }
                catch
                {
                    InfoWindow Mes = new InfoWindow("服务器正忙\n 请稍等", false);
                    Mes.StartPosition = FormStartPosition.CenterParent;
                    Mes.Show();
                    return;
                }
                if (client.Connected)//连接成功发送下线请求
                {
                    NetworkStream ClientToServer = client.GetStream();
                    string        LoginInfo      = "logout" + BasicInfo.UserName;        //"logout"+学号
                    byte[]        send           = Encoding.Default.GetBytes(LoginInfo); //转化为字节流
                    ClientToServer.Write(send, 0, send.Length);                          //写入NetworkStream

                    byte[] msg      = new byte[1024];                                    //接收
                    int    length   = ClientToServer.Read(msg, 0, msg.Length);
                    string response = Encoding.Default.GetString(msg, 0, length);
                    Console.WriteLine(response);
                    if (response == "loo")//服务器返回成功下线信息
                    {
                        ClientToServer.Close();
                        client.Close();
                        InfoWindow Mes = new InfoWindow("下线成功!", false);
                        Mes.StartPosition = FormStartPosition.CenterParent;
                        Mes.ShowDialog();
                        if (Mes.DialogResult == DialogResult.Cancel)
                        {
                            System.Environment.Exit(0);//关闭所有进程
                        }
                    }
                    else
                    {
                        //InfoWindow Mes = new InfoWindow("服务器状态异常\n    请稍等", false);//提示信息好像会重复,这里不要了
                        //Mes.StartPosition = FormStartPosition.CenterParent;
                        //Mes.Show();
                        return;
                    }
                }
            }
        }
예제 #3
0
        static void Logout()//下线功能,现已移植到ChatWindow中
        {
            InfoWindow Confirm = new InfoWindow("确认退出当前账号?", true);

            Confirm.StartPosition = FormStartPosition.CenterParent;
            Confirm.ShowDialog();
            if (Confirm.DialogResult == DialogResult.OK)
            {
                Confirm.Close();
                TcpClient client = new TcpClient();
                try
                {
                    client.Connect(BasicInfo.ServerIp, BasicInfo.ServerPort);
                }
                catch
                {
                    InfoWindow Mes = new InfoWindow("服务器无响应\n您已强制退出程序", false);
                    Mes.StartPosition = FormStartPosition.CenterParent;
                    Mes.Show();
                }
                if (client.Connected)
                {
                    NetworkStream ClientToServer = client.GetStream();
                    string        LoginInfo      = "logout" + BasicInfo.UserName;        //下线格式:"logout"+学号
                    byte[]        send           = Encoding.Default.GetBytes(LoginInfo); //将输入信息转化为byte字节流
                    ClientToServer.Write(send, 0, send.Length);                          //从networkstream中写入字节流
                    //接收
                    byte[] msg      = new byte[1024];
                    int    length   = ClientToServer.Read(msg, 0, msg.Length);
                    string response = Encoding.Default.GetString(msg, 0, length);
                    Console.WriteLine(response);
                    if (response == "loo")
                    {
                        ClientToServer.Close();
                        client.Close();
                        InfoWindow Mes = new InfoWindow("您已强制退出程序\n  下线成功!", false);
                        Mes.StartPosition = FormStartPosition.CenterParent;
                        Mes.ShowDialog();
                        if (Mes.DialogResult == DialogResult.Cancel)
                        {
                            System.Environment.Exit(0);
                        }                              //关闭所有进程
                    }
                    else
                    {
                        InfoWindow Mes = new InfoWindow("未成功下线!\n您已强制退出程序", false);
                        Mes.StartPosition = FormStartPosition.CenterParent;
                        Mes.Show();
                    }
                }
            }
        }
예제 #4
0
 private void TcpSend(NetworkStream ClientToServer, string Message) //发送TCP消息
 {
     byte[] msg = Encoding.Default.GetBytes(Message);               //将字符串转化为字节流
     try
     {
         ClientToServer.Write(msg, 0, msg.Length);//写入NetworkStream
         Console.WriteLine(Message);
     }
     catch
     {
         InfoWindow Mes = new InfoWindow("发送消息失败!", false);
         Mes.StartPosition = FormStartPosition.CenterParent;
         Mes.ShowDialog();
     }
 }
예제 #5
0
        private void SearchDialogueButton_Click(object sender, EventArgs e)//点击“保存/查询聊天记录”图标
        {
            string path = ".\\" + BasicInfo.UserName;

            if (!Directory.Exists(path))//用户目录不存在时新建文件夹
            {
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                directoryInfo.Create();
            }
            if (!File.Exists(path + "\\With" + BasicInfo.CurrentFriend + ".txt"))//打开保存聊天记录的文件
            {
                FileStream fs = new FileStream(path + "\\With" + BasicInfo.CurrentFriend + ".txt", FileMode.Create, FileAccess.ReadWrite);
                fs.Close();
            }
            int pos = -1;

            for (int i = 0; i < MyFriendList.Count; i++)
            {
                if (MyFriendList[i].Name == BasicInfo.CurrentFriend)
                {
                    pos = i;
                    break;
                }
            }
            if (pos > -1)
            {
                Console.WriteLine(pos);
                StreamWriter sw = new StreamWriter(path + "\\With" + BasicInfo.CurrentFriend + ".txt", true);
                for (int i = MyFriendList[pos].RecentSave; i < MyFriendList[pos].WordList.Count; i++)
                {
                    sw.WriteLine(MyFriendList[pos].WordList[i]);
                }
                MyFriendList[pos].RecentSave = MyFriendList[pos].WordList.Count;
                sw.Close();

                InfoWindow Confirm = new InfoWindow("聊天记录保存成功\n    是否打开?", true);
                Confirm.StartPosition = FormStartPosition.CenterParent;
                Confirm.ShowDialog();
                if (Confirm.DialogResult == DialogResult.OK)//确认退出
                {
                    System.Diagnostics.Process.Start("Explorer", path);
                }
                else
                {
                    return;
                }
            }
        }
예제 #6
0
 private void LoginButton_Click(object sender, EventArgs e)//"Send"按键,按下发送消息键
 {
     if (RichText.Text.Length == 0)
     {
         InfoWindow Mes1 = new InfoWindow("您还未输入信息哦", false);
         Mes1.StartPosition = FormStartPosition.CenterParent;
         Mes1.ShowDialog();
         return;
     }
     else
     {
         if (BasicInfo.CurrentFriend == null)
         {
             InfoWindow Mes1 = new InfoWindow("您还未添加好友哦", false);
             Mes1.StartPosition = FormStartPosition.CenterParent;
             Mes1.ShowDialog();
             return;
         }
         string filePath = "";
         //string
         if (filePath == string.Empty)
         {
             if (SendText(BasicInfo.CurrentFriend, RichText.Text))
             {
                 Friend TempFriend = UpdateChatPanel(BasicInfo.CurrentFriend, RichText.Text, DialogueType.MyText);
                 Refresh(TempFriend, true);
                 RichText.Clear();
             }
             else
             {
                 return;
             }
         }
         else
         {
             string file = BasicInfo.CurrentFriend + filePath;
             Thread SendFile;              //发送文件线程
             SendFile = new Thread(new ParameterizedThreadStart(sendfile));
             SendFile.Start((object)file); //开线程传文件
             RichText.Clear();
         }
     }
 }
예제 #7
0
        private QueryResult SearchIP(string Name) //查询对话方的IP地址信息
        {
            TcpClient   client = new TcpClient();
            QueryResult query  = new QueryResult();

            try
            {
                client.Connect(BasicInfo.ServerIp, BasicInfo.ServerPort);
            }
            catch
            {
                InfoWindow Mes2 = new InfoWindow("连接失败\n  请稍后重试", false);
                Mes2.StartPosition = FormStartPosition.CenterParent;
                Mes2.ShowDialog();
                client.Close();
            }
            if (client.Connected)
            {
                NetworkStream ClientToServer = client.GetStream();
                string        QueryInfo      = "q" + Name;//“q”+学号查询在线状态
                TcpSend(ClientToServer, QueryInfo);
                string response = TcpReceive(ClientToServer);
                query.Message = response;
                if (response.Length > 0)
                {
                    if (response == "n")
                    {
                        InfoWindow Mes1 = new InfoWindow("该用户不在线", false);
                        Mes1.StartPosition = FormStartPosition.CenterParent;
                        Mes1.ShowDialog();
                    }
                    else
                    {
                        query.IsOnline = true;
                        query.IP       = response;
                    }
                }
                ClientToServer.Close();
            }
            client.Close();
            return(query);
        }
예제 #8
0
        private void GroupButton_Click(object sender, EventArgs e)//点击“发起群聊”图标
        {
            if (MyFriendList.Count < 3)
            {
                InfoWindow Mes1 = new InfoWindow("您的好友数小于3\n  不能发起群聊", false);
                Mes1.StartPosition = FormStartPosition.CenterParent;
                Mes1.ShowDialog();
                return;
            }
            InfoWindow Confirm = new InfoWindow("与当前所有好友\n  发起群聊?", true);

            Confirm.StartPosition = FormStartPosition.CenterParent;
            Confirm.ShowDialog();
            if (Confirm.DialogResult == DialogResult.Cancel)//
            {
                return;
            }
            InfoWindow Mes = new InfoWindow("程序员Debug中...\n  请尝试其他功能", false);

            Mes.StartPosition = FormStartPosition.CenterParent;
            Mes.ShowDialog();
            return;
        }
예제 #9
0
        private string TcpReceive(NetworkStream ClientToServer)//接收TCP消息
        {
            string response = "";

            byte[] Buffer = new byte[1024];
            int    length = 0;

            try
            {
                length = ClientToServer.Read(Buffer, 0, 1024);
                if (length > 0)
                {
                    response = Encoding.Default.GetString(Buffer, 0, length);//将字节流转化为字符串
                    Console.WriteLine("TCP_Receive" + response);
                }
            }
            catch
            {
                InfoWindow Mes = new InfoWindow("接收消息失败!", false);
                Mes.StartPosition = FormStartPosition.CenterParent;
                Mes.ShowDialog();
            }
            return(response);
        }
예제 #10
0
        private void button1_Click(object sender, EventArgs e)//“查询好友”按键,查询好友在线状态并添加好友
        {
            string QueryName = SearchText.Text;

            if (QueryName == BasicInfo.UserName)//查询自己学号禁止添加自己操作
            {
                InfoWindow Mes2 = new InfoWindow("您已在线", false);
                Mes2.StartPosition = FormStartPosition.CenterParent;
                Mes2.ShowDialog();
                return;
            }
            TcpClient   client = new TcpClient();
            QueryResult query  = new QueryResult();

            try
            {
                client.Connect(BasicInfo.ServerIp, BasicInfo.ServerPort);
            }
            catch
            {
                InfoWindow Mes2 = new InfoWindow("连接失败\n  请稍后重试", false);
                Mes2.StartPosition = FormStartPosition.CenterParent;
                Mes2.ShowDialog();
                client.Close();
            }
            if (client.Connected)
            {
                NetworkStream ClientToServer = client.GetStream();
                string        QueryInfo      = "q" + QueryName;//“q”+学号查询在线状态
                TcpSend(ClientToServer, QueryInfo);
                string response = TcpReceive(ClientToServer);
                query.Message = response;
                Console.WriteLine("查询好友状态IP" + response);
                if (response.Length > 0)
                {
                    if (response.StartsWith("I") || response.StartsWith("P"))//服务器返回InVailid No.或Please send correct message
                    {
                        InfoWindow Mes1 = new InfoWindow("该用户不存在", false);
                        Mes1.StartPosition = FormStartPosition.CenterParent;
                        Mes1.ShowDialog();
                        return;
                    }
                    else if (response == "n")
                    {
                        InfoWindow Mes1 = new InfoWindow("该用户不在线", false);
                        Mes1.StartPosition = FormStartPosition.CenterParent;
                        Mes1.ShowDialog();
                        return;
                    }
                    else
                    {
                        query.IsOnline = true;
                        query.IP       = response;
                    }
                }
                ClientToServer.Close();
            }
            client.Close();

            InfoWindow Mes = new InfoWindow(QueryName + "在线\n 是否发起会话?", true);

            Mes.StartPosition = FormStartPosition.CenterParent;
            Mes.ShowDialog();
            if (Mes.DialogResult == DialogResult.Cancel)
            {
                return;
            }
            Friend CurrentFriend = UpdateChatPanel(QueryName, string.Empty);

            Refresh(CurrentFriend, true);
            return;
        }
예제 #11
0
        private void sendfile(object file)//发起P2P连接发送文件
        {
            string FileName   = (string)file;
            string FriendName = FileName.Substring(0, 10);

            FileName = FileName.Substring(10);
            string     truefilename = FileName.Substring(FileName.LastIndexOf('\\') + 1);
            FileStream ReadStream   = new FileStream(FileName, FileMode.Open, FileAccess.Read);

            byte[] buffer       = new byte[1024];
            int    Packet       = 1024;
            int    TotalFileNum = (int)(ReadStream.Length + 1023) / 1024;

            if (TotalFileNum > 0)// 文件不为空
            {
                int pos = -1;
                for (int i = 0; i < MyFriendList.Count; i++)
                {
                    if (MyFriendList[i].Name == FriendName)
                    {
                        pos = i;
                        break;
                    }
                }
                QueryResult query = SearchIP(FriendName); //与发送消息基本相同
                while (true)                              //等待端口空闲
                {
                    pos = -1;
                    for (int i = 0; i < MyFriendList.Count; i++)
                    {
                        if (MyFriendList[i].Name == FriendName)
                        {
                            pos = i;
                            break;
                        }
                    }
                    if (pos > -1 || MyFriendList[pos].IsBusy == false)
                    {
                        break;
                    }
                    else
                    {
                        InfoLabel.Text = "端口正在占用,请稍等";
                    }
                }
                TcpClient NewClient = new TcpClient();
                MyFriendList[pos].IsBusy = true; //正在占用端口

                try                              //先尝试连接服务器
                {
                    int Port = int.Parse(FriendName.Substring(6)) + 10000;
                    NewClient.Connect(query.IP, Port);
                }
                catch//未连接成功
                {
                    Console.WriteLine("发起P2P连接失败");
                    return;
                }
                if (NewClient.Connected)
                {
                    NetworkStream PeerToPeer     = NewClient.GetStream();
                    byte[]        ConfirmMessage = Encoding.Default.GetBytes(BasicInfo.UserName);
                    string        response;
                    try
                    {
                        PeerToPeer.Write(ConfirmMessage, 0, ConfirmMessage.Length);//发送自己的名字,收到ACK时进行对话
                        byte[] ReturnMessage = new byte[20];
                        int    length        = PeerToPeer.Read(ReturnMessage, 0, ReturnMessage.Length);
                        response = Encoding.Default.GetString(ReturnMessage, 0, length);
                        Console.WriteLine(response);
                    }
                    catch
                    {
                        Console.WriteLine("发送P2P认证信息失败");
                        PeerToPeer.Close();
                        NewClient.Close();
                        return;
                    }
                    if (response == "ACK")//对方已经收到发出去的消息并加自己为好友
                    {
                        string temp = "[FileName]" + truefilename + "[Length]" + ReadStream.Length.ToString();
                        byte[] msg  = Encoding.Default.GetBytes(temp);
                        //Console.WriteLine(msg.Length);
                        PeerToPeer.Write(msg, 0, msg.Length);
                        PeerToPeer.WriteTimeout = 20000;
                        int fileLength = 0;
                        try
                        {
                            while (fileLength < ReadStream.Length)
                            {
                                this.Invoke(new EventHandler(delegate
                                {
                                    double percent = Convert.ToInt32(100.0 * fileLength / ReadStream.Length);
                                    InfoLabel.Text = "文件传输中..." + percent + "%";//显示文件传输进度
                                }));
                                Packet = ReadStream.Read(buffer, 0, buffer.Length);
                                PeerToPeer.Write(buffer, 0, Packet);
                                fileLength += Packet;
                            }
                            Console.WriteLine(fileLength.ToString() + "  " + ReadStream.Length.ToString());
                            this.Invoke(new EventHandler(delegate
                            {
                                InfoLabel.Text = "文件传输完成";
                            }));
                            ReadStream.Flush();//ReadStream.Flush();
                            PeerToPeer.Flush();
                            ReadStream.Close();
                        }
                        catch
                        {
                            InfoWindow Mes2 = new InfoWindow("文件传输失败", false);
                            Console.WriteLine("文件传输失败");
                            Mes2.StartPosition = FormStartPosition.CenterParent;
                            Mes2.Show();
                            return;
                        }
                        this.Invoke(new EventHandler(delegate
                        {
                            temp = "[文件][" + truefilename + "]";
                            UpdateChatPanel(BasicInfo.CurrentFriend, temp, DialogueType.MyText);//更新对话框显示
                        }));
                    }
                    PeerToPeer.Close();
                    NewClient.Close();
                }
                MyFriendList[pos].IsBusy = false;//释放端口
            }
            else
            {
                InfoWindow Mes2 = new InfoWindow("传输文件为空或过大", false);
                Mes2.StartPosition = FormStartPosition.CenterParent;
                Mes2.ShowDialog();
                return;
            }
        }
예제 #12
0
        private void LoginButton_Click(object sender, EventArgs e)
        {
            string LoginInfo;
            int    Port = 10000;

            try//记录端口
            {
                Port = int.Parse(NameText.Text.Substring(6)) + 10000;
            }
            catch
            {
                InfoWindow Mes = new InfoWindow("请输入有效账号", false);
                Mes.StartPosition = FormStartPosition.CenterParent;
                Mes.ShowDialog();
            }
            IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

            IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();

            foreach (IPEndPoint endPoint in ipEndPoints)//判断该端口是否被占用
            {
                if (endPoint.Port == Port)
                {
                    InfoWindow Mes = new InfoWindow("该账号已登录", false);
                    Mes.StartPosition = FormStartPosition.CenterParent;
                    Mes.ShowDialog();
                    return;
                }
            }
            TcpClient client = new TcpClient();

            try//尝试连接服务器
            {
                client.Connect(BasicInfo.ServerIp, BasicInfo.ServerPort);
            }
            catch//未连接成功
            {
                InfoWindow Mes = new InfoWindow("连接失败\n请稍后重试", false);
                Mes.StartPosition = FormStartPosition.CenterParent;
                Mes.ShowDialog();
            }
            if (client.Connected)
            {
                NetworkStream ClientToServer = client.GetStream();
                LoginInfo = NameText.Text + "_" + PasswordText.Text;
                byte[] send = Encoding.Default.GetBytes(LoginInfo); //将输入信息转化为字节流
                ClientToServer.Write(send, 0, send.Length);         //写入NetworkStream

                //接收
                byte[] msg      = new byte[1024];
                int    length   = ClientToServer.Read(msg, 0, msg.Length);
                string response = Encoding.Default.GetString(msg, 0, length);
                Console.WriteLine(response);
                if (response == "lol")//服务器返回登录成功
                {
                    BasicInfo.UserName = NameText.Text;
                    ClientToServer.Close();
                    client.Close();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    InfoWindow Mes = new InfoWindow("用户名或密码错误!", false);
                    Mes.StartPosition = FormStartPosition.CenterParent;
                    Mes.ShowDialog();
                }
            }
            else
            {
                return;
            }
        }