// Pop up a Connect user dialog and send a message requesting user to log in to chat. void AttemptLogin() { frmConnectUser frmConnectUser = new frmConnectUser(); frmConnectUser.StartPosition = FormStartPosition.CenterParent; frmConnectUser.ShowDialog(this); SendData("CONNECT|" + frmConnectUser.txtUserLogin.Text); frmConnectUser.Dispose(); }
// When the form starts, this subroutine will connect to the server and attempt to // log in. private void frmMain_Load(object sender, System.EventArgs e) { frmConnectUser frmConnectUser = new frmConnectUser(); 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("Server is not active. Please start server and try again.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); this.Dispose(); } }