Esempio n. 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            Socket_Client sc = new Socket_Client();

            sc.StartPosition = FormStartPosition.CenterScreen;
            sc.Show();
            // LoginForm dlg = new LoginForm();
            //ctrl +k,ctrl+C  实现多行注释
            // dlg.ShowDialog();
            // 这里ShowDialog方法表示你必须先操作完dlg窗口,才能操作后面的主窗体。
            // 如果要登录窗口显示在主窗口的中心,则在显示之前设置如下
            // dlg.StartPosition = FormStartPosition.CenterParent;
            // dlg.ShowDialog();
            // 能够这样做的前提是主窗体必须先定义和显示。否则登录窗体可能无法找到父窗体。
            //除此之外,也可以手动设置窗口显示的位置,即窗口坐标。
            //首先必须把窗体的显示位置设置为手动。
            //dlg.StartPosition = FormStartPosition.Manual;
            //随后获取屏幕的分辨率,也就是显示器屏幕的大小。
            //int xWidth = SystemInformation.PrimaryMonitorSize.Width;//获取显示器屏幕宽度
            //int yHeight = SystemInformation.PrimaryMonitorSize.Height;//高度
            //然后定义窗口位置,以主窗体为例
            //mainForm.Location = new Point(xWidth / 2, yHeight / 2);//这里需要再减去窗体本身的宽度和高度的一半
            //mainForm.Show();
        }
Esempio n. 2
0
        private void ReceiveMessage()
        {
            long TotalLength = 0;

            //如果位while(true),当窗体关闭之后还会有线程一直在跑,这样服务端就会认为该客户端没断线
            while (this.Visible)
            {
                byte[] result        = new byte[1024 * 1024 * 10];
                int    ReceiveLength = 0;
                try
                {
                    ReceiveLength = ClientSocket.Receive(result);
                    if (result[0] == 0)//表示接收到的是消息
                    {
                        try
                        {
                            richTextBox1.Text += "接收服务器消息:\r\n";
                            string str = Encoding.UTF8.GetString(result, 1, ReceiveLength - 1);
                            richTextBox1.Text += str + "\r\n";
                        }
                        catch (Exception ex)
                        {
                            richTextBox1.Text += "接收服务器消息失败!\r\n";
                            ClientSocket.Shutdown(SocketShutdown.Both);
                            ClientSocket.Close();
                            break;
                        }
                    }
                    if (result[0] == 6)//接收到监控端发送来的参数信息
                    {
                        string        Information = Encoding.UTF8.GetString(result, 1, ReceiveLength - 1);
                        Socket_Client sc          = new Socket_Client();
                        sc.Parameter       = Information;
                        richTextBox1.Text += "接收修改参数:" + Information + "\r\n";
                        Status sta = new Status();
                        if (Information != null)
                        {
                            if (Information[0] == '1')
                            {
                                sta.flag11            = true;
                                sta.pictureBox1.Image = Properties.Resources.led_O;
                            }

                            if (Information[1] == '1')
                            {
                                sta.flag22            = true;
                                sta.pictureBox2.Image = Properties.Resources.led_O;
                            }
                            if (Information[2] == '1')
                            {
                                sta.flag33            = true;
                                sta.pictureBox3.Image = Properties.Resources.led_O;
                            }

                            if (Information[3] == '1')
                            {
                                sta.flag44            = true;
                                sta.pictureBox4.Image = Properties.Resources.led_O;
                            }

                            if (Information[4] == '1')
                            {
                                sta.flag55            = true;
                                sta.pictureBox5.Image = Properties.Resources.led_O;
                            }

                            if (Information[5] == '1')
                            {
                                sta.flag66            = true;
                                sta.pictureBox6.Image = Properties.Resources.led_O;
                            }

                            if (Information[6] == '1')
                            {
                                sta.flag77            = true;
                                sta.pictureBox7.Image = Properties.Resources.led_O;
                            }

                            if (Information[7] == '1')
                            {
                                sta.flag88            = true;
                                sta.pictureBox8.Image = Properties.Resources.led_O;
                            }
                        }
                        //sta.StartPosition = FormStartPosition.CenterScreen;
                        //sta.Show();
                    }
                    if (result[0] == 3)//接收到服务端返回的认证包
                    {
                        richTextBox2.Text += DateTime.Now + "接受服务器认证包:";
                        string str = Encoding.UTF8.GetString(result, 1, ReceiveLength - 1);
                        richTextBox2.Text += str + "\r\n";
                        heartNum           = 0;//收到认证包
                    }
                    if (heartNum > 6)
                    {
                        connectCheck = false;
                        MessageBox.Show("服务器可能已经挂掉!");
                        ClientSocket.Shutdown(SocketShutdown.Both);
                        ClientSocket.Close();
                    }
                    if (result[0] == 2)
                    {
                        string fileNameWithLength = Encoding.UTF8.GetString(result, 1, ReceiveLength - 1);
                        string str1 = fileNameWithLength.Split('-').First();
                        TotalLength        = Convert.ToInt64(fileNameWithLength.Split('-').Last());
                        richTextBox1.Text += "接收服务器后缀名为:" + str1 + "的文件" + "\r\n";
                    }
                    if (result[0] == 1)//表示接收到的是文件
                    {
                        try
                        {
                            richTextBox1.Text += "文件总长度" + TotalLength.ToString() + "\r\n";
                            SaveFileDialog sfd = new SaveFileDialog();
                            if (sfd.ShowDialog(this) == DialogResult.OK)
                            {
                                string fileSavePath  = sfd.FileName;//获取文件保存路径
                                long   receiveLength = 0;
                                bool   flag          = true;
                                int    rec           = 0;
                                using (FileStream fs = new FileStream(fileSavePath, FileMode.Create, FileAccess.Write))
                                {
                                    while (TotalLength > receiveLength)
                                    {
                                        if (flag)
                                        {
                                            fs.Write(result, 1, ReceiveLength - 1);
                                            fs.Flush();
                                            receiveLength += ReceiveLength - 1;
                                            flag           = false;
                                        }
                                        else
                                        {
                                            rec = ClientSocket.Receive(result);
                                            fs.Write(result, 0, rec);
                                            fs.Flush();
                                            receiveLength += rec;
                                        }
                                    }
                                    richTextBox1.Text += "文件保存成功:" + " " + fileSavePath + "\r\n";
                                    fs.Close();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            richTextBox1.Text += "文件保存失败!\r\n";
                            MessageBox.Show(ex.Message);
                            connectCheck = false;
                            ClientSocket.Shutdown(SocketShutdown.Both);
                            ClientSocket.Close();
                            break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("接收出错,服务器可能已经挂了!");
                    break;
                }
            }
        }