Example #1
0
        public void stopServer(string str = null)
        {
            Controller.ConnectionHandle conn = new Controller.ConnectionHandle(this.txtCmd);

            conn.stopServer(str);

            startServer = false;

            if (sckServer != null)
            {
                sckServer.Close();
                StopServer(this, new EventArgs());
            }

            btnStop.Visible  = false;
            btnStart.Visible = true;

            if (Listening != null)
            {
                Listening.Abort();
            }
        }
Example #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));
            }
        }