Esempio n. 1
0
        public void Tam()
        {
            while (true)
            {
                while (!listener.Pending())
                {
                    Thread.Sleep(1000);
                }
                Socket clientToHandle = listener.AcceptSocket();
                ListClient.Add(clientToHandle);
                string      IP         = clientToHandle.RemoteEndPoint.ToString().Split(':')[0];
                Maytinhinfo newmaytinh = new Maytinhinfo()
                {
                    endPoint = clientToHandle.RemoteEndPoint as IPEndPoint,
                    ClientIP = IP,
                    status   = ClientinfoStatus.ClientConnected,
                    student  = new Student()
                };

                maytinhInfoManager.Add(newmaytinh);
                if (_onClientListChanged != null)
                {
                    _onClientListChanged(maytinhInfoManager.maytinh);
                }
                receiveCommand = new Thread(ReceiveCommand);
                receiveCommand.IsBackground = true;
                receiveCommand.Start(clientToHandle);
            }
        }
Esempio n. 2
0
 public Maytinhinfo(Maytinhinfo client)
 {
     PCName   = client.PCName;
     student  = client.student;
     endPoint = client.endPoint;
     ClientIP = client.ClientIP;
     status   = client.status;
 }
Esempio n. 3
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            Maytinhinfo client = obj as Maytinhinfo;

            return(client.ClientIP == ClientIP && client.PCName == PCName && client.status == status && client.student.MSSV == student.MSSV);
        }
Esempio n. 4
0
        public void Add(Maytinhinfo client)
        {
            int?index = IndexOf(client);

            if (index != null)
            {
                _maytinh[(int)index] = client;
                return;
            }
            AddTail(client);
        }
Esempio n. 5
0
        public int?IndexOf(Maytinhinfo client)
        {
            for (int i = 0; i < _maytinh.Count; i++)
            {
                if (_maytinh[i].ClientIP.Equals(client.ClientIP))
                {
                    return(i);
                }
            }

            return(null);
        }
Esempio n. 6
0
 public MaytinhInfoManager(string FirstIP, string LastIP, string SubnetMask)
 {
     _maytinh = new List <Maytinhinfo>();
     try
     {
         string s1 = "", s2 = "";
         int    y = 0, x = 0, z = 0, t = 0;
         if (FirstIP != "")
         {
             s1 = FirstIP.Substring(0, FirstIP.LastIndexOf("."));
             x  = int.Parse(FirstIP.Substring(FirstIP.LastIndexOf(".") + 1));
         }
         if (LastIP != "")
         {
             s2 = LastIP.Substring(0, LastIP.LastIndexOf("."));
             y  = int.Parse(LastIP.Substring(LastIP.LastIndexOf(".") + 1));
         }
         t = y - x;
         if (SubnetMask != "")
         {
             z = 256 - int.Parse(SubnetMask.Substring(SubnetMask.LastIndexOf(".") + 1));
         }
         if (x < 255 && y < 255 && s1.CompareTo(s2) == 0)
         {
             for (int i = x; i < z && i <= y; i++)
             {
                 string      ip          = s1 + "." + i.ToString();
                 Maytinhinfo maytinhinfo = new Maytinhinfo();
                 maytinhinfo.ClientIP = ip;
                 _maytinh.Add(maytinhinfo);
             }
         }
         else
         {
             MessageBox.Show("Nhập sai IP");
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 7
0
        void RenderClientList(List <Maytinhinfo> clientList)
        {
            if (flpPc.Controls.Count == 0)
            {
                foreach (Maytinhinfo clientInfo in clientList)
                {
                    frmmaytinh frm = new frmmaytinh(clientInfo);
                    flpPc.Controls.Add(frm);
                }

                return;
            }

            int clientControlLength = flpPc.Controls.Count;
            int i = 0;

            for (i = 0; i < clientList.Count; i++)
            {
                Maytinhinfo clientInfoInList = clientList[i];

                if (i < clientControlLength)
                {
                    frmmaytinh  frm = flpPc.Controls[i] as frmmaytinh;
                    Maytinhinfo clientInfoInControl = frm.maytinhinfo;

                    frm.SetClient(clientInfoInList);
                }
                else
                {
                    frmmaytinh frm = new frmmaytinh(clientInfoInList);
                    flpPc.Controls.Add(frm);
                }
            }

            if (i < flpPc.Controls.Count)
            {
                for (int j = flpPc.Controls.Count - 1; j >= i; j--)
                {
                    flpPc.Controls.RemoveAt(j);
                }
            }
        }
Esempio n. 8
0
 void AddTail(Maytinhinfo client)
 {
     _maytinh.Add(client);
 }
Esempio n. 9
0
        public void ReceiveCommand(object obj)
        {
            Socket      client      = obj as Socket;
            string      ClientIP    = client.RemoteEndPoint.ToString().Split(':')[0];
            Maytinhinfo maytinhinfo = maytinhInfoManager.Find(ClientIP);

            try
            {
                while (true)
                {
                    byte[] buffer = new byte[Buffersize];
                    client.Receive(buffer);
                    ServerResponse response = ServerResponse.Deserialize(buffer);
                    switch (response.Type)
                    {
                    case ServerResponseType.SendDSCSDL:
                        break;

                    case ServerResponseType.SendListStudentExcel:
                        break;

                    case ServerResponseType.SendStudent:
                        Student student = response.Data as Student;
                        maytinhinfo.student = student;
                        maytinhinfo.status  = ClientinfoStatus.StudentConnected;
                        if (_onClientListChanged != null)
                        {
                            _onClientListChanged(maytinhInfoManager.maytinh);
                        }
                        break;

                    case ServerResponseType.SendMessage:
                        break;

                    case ServerResponseType.SendPcName:
                        string pcname = response.Data as string;
                        maytinhinfo.PCName = pcname;
                        maytinhinfo.status = ClientinfoStatus.ClientConnected;
                        if (_onClientListChanged != null)
                        {
                            _onClientListChanged(maytinhInfoManager.maytinh);
                        }
                        break;

                    case ServerResponseType.DisconnectAll:

                        break;

                    case ServerResponseType.BeginExam:
                        break;

                    case ServerResponseType.FinishExam:
                        FileContainer savefilebaithi = response.Data as FileContainer;
                        string        savepath       = this.serverpath;
                        if (!Directory.Exists(savepath))
                        {
                            Directory.CreateDirectory(savepath);
                        }

                        string fileName = savefilebaithi.FileInfo.Name;
                        string fullPath = Path.Combine(savepath, fileName);

                        byte[] fileContent = savefilebaithi.FileContent;

                        using (var fileStream = File.Create(fullPath))
                            fileStream.Write(fileContent, 0, fileContent.Length);

                        break;

                    case ServerResponseType.LockClient:
                        break;

                    case ServerResponseType.Undefined:
                        break;

                    default:
                        break;
                    }
                }
            }
            catch (Exception)
            {
                maytinhinfo.status = ClientinfoStatus.Disconnected;

                if (_onClientListChanged != null)
                {
                    _onClientListChanged(maytinhInfoManager.maytinh);
                }

                ListClient.Remove(client);
                client.Close();
            }
        }