Пример #1
0
        /*
         * When the form starts, this subroutine will connect to the server and attempt to
         * log in.
         */
        private void Form_Main_Load(object sender, EventArgs e)
        {
            Form_Login frmLogin = new Form_Login();

            try
            {
                // The TcpClient is a subclass of Socket, providing higher level
                // functionality like streaming.
                client = new TcpClient("localhost", PORT_NUM);
                // Start an asynchronous read invoking DoRead to avoid lagging the user
                // interface.
                client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(DoRead), null);
                // Make sure the window is showing before popping up connection dialog.
                this.Show();
                AttemptLogin();
            }
            catch (Exception Ex)
            {
                MessageBox.Show("Không thể kết nối với máy chủ. Vui lòng thực hiện lại việc đăng nhập.",
                                this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.Dispose();
            }
        }