Exemplo n.º 1
0
        void Receive()
        {
            try
            {
                while (true)
                {
                    byte[] buffer = new byte[1024 * 1024 * 20];
                    client.Receive(buffer);

                    object         data      = Deserialize(buffer);
                    ServerResponse container = data as ServerResponse;

                    switch (container.Type)
                    {
                    case ServerResponseType.SendFile:

                        FileResponse fileResponse = container.Data as FileResponse;

                        string fileName = fileResponse.FileInfo.Name;
                        lblDeThi.Text = fileName;
                        file          = fileName;
                        using (var fileStream = File.Create(fileName))
                        {
                            fileStream.Write(fileResponse.FileContent, 0, fileResponse.FileContent.Length);
                        }

                        break;

                    case ServerResponseType.SendList:

                        List <Student> listStudent = container.Data as List <Student>;

                        cbDSThi.DataSource    = listStudent;
                        cbDSThi.DisplayMember = "FullNameAndId";

                        break;

                    case ServerResponseType.SendString:
                        List <string> monthi = container.Data as List <string>;

                        lblMonThi.Text = monthi[0].ToString();
                        break;

                    case ServerResponseType.BeginExam:

                        int minute = (int)container.Data;
                        counter = minute * 60;
                        int hour = minute / 60;

                        lblThoiGian.Text = $"{hour} giờ, {minute % 60} phút";

                        countdown.Enabled = true;

                        break;

                    case ServerResponseType.FinishExam:
                        break;

                    case ServerResponseType.LockClient:
                        break;

                    case ServerResponseType.SendMessage:
                        string mess = container.Data as string;
                        rtxtMesseage.Text += "Server: " + mess + "\n";



                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Có lỗi xảy ra trong quá trình nhận phản hồi từ server. Đóng kết nối. " + ex.Message);
                CloseConnection();
            }
        }