Пример #1
0
        private Controller.ObjectForThread getObjForConnectionHandel()
        {
            string path = Path.GetDirectoryName(Application.ExecutablePath);

            path = Path.Combine(path, Controller.Constant.nameFolderSaveFile);
            path = Path.Combine(path, Controller.Constant.nameFileSetting);
            Controller.IO_INI ini        = new Controller.IO_INI(path);
            string            time       = ini.IniReadValue(Controller.Constant.sectionSetting, Controller.Constant.keyTime);
            string            numGetDB   = "";
            string            numGetFile = "";

            if (ini.IniReadValue(Controller.Constant.sectionDB, Controller.Constant.keyIdCourse) != string.Empty)
            {
                numGetDB = ini.IniReadValue(Controller.Constant.sectionDB, Controller.Constant.keyGetQuesDB);
            }
            if (ini.IniReadValue(Controller.Constant.sectionFile, Controller.Constant.keyPathFile) != string.Empty)
            {
                numGetFile = ini.IniReadValue(Controller.Constant.sectionFile, Controller.Constant.keyGetQuesFile);
            }
            if (numGetDB == string.Empty)
            {
                numGetDB = "0";
            }
            if (numGetFile == string.Empty)
            {
                numGetFile = "0";
            }
            var obj = new Controller.ObjectForThread(Convert.ToInt32(time), Convert.ToInt32(numGetDB), Convert.ToInt32(numGetFile));

            return(obj);
        }
Пример #2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!startServer)
            {
                string path = Path.GetDirectoryName(Application.ExecutablePath);
                path = Path.Combine(path, Controller.Constant.nameFolderSaveFile);
                path = Path.Combine(path, Controller.Constant.nameFileSetting);

                if (File.Exists(path))
                {
                    Controller.IO_INI ini  = new Controller.IO_INI(path);
                    string            ip   = ini.IniReadValue(Controller.Constant.sectionInfo, Controller.Constant.keyIP);
                    string            port = ini.IniReadValue(Controller.Constant.sectionInfo, Controller.Constant.keyPort);

                    eventStartServer(this, new Controller.EventSendData(ip, port));

                    sckServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    try
                    {
                        sckServer.Bind(new IPEndPoint(IPAddress.Any, Convert.ToInt32(port)));
                        sckServer.Listen(100);
                        AppendText(txtCmd, "Server start. Waiting for client ..........", new Tuple <int, int, int>(165, 42, 42));
                    }
                    catch (SocketException ex)
                    {
                        MessageBox.Show(ex.ToString(), "Có lỗi xảy ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    startServer = true;

                    Listening = new Thread(() =>
                    {
                        try
                        {
                            while (startServer)
                            {
                                Socket sckClient = sckServer.Accept();

                                Controller.ConnectionHandle server = new Controller.ConnectionHandle(sckClient, txtCmd);

                                server.Run();
                            }
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show(ex.ToString(), "Thread", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    });
                    Listening.IsBackground = true;
                    Listening.Start();


                    btnStart.Visible = false;
                    btnStop.Visible  = true;
                }
                else
                {
                    MessageBox.Show("Không tìm thấy file " + Controller.Constant.nameFileSetting + "! Vui lòng kiểm tra lại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                AppendText(txtCmd, "Server was started!", new Tuple <int, int, int>(165, 42, 42));
            }
        }