private void btnConnect_Click(object sender, EventArgs e) { NetworkDialog dlg = new NetworkDialog(); if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) { game.networkClient = new NetworkClient(dlg.Host, dlg.Port); if (game.networkClient.ErrorText != "") { MessageBox.Show(game.networkClient.ErrorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); game.networkClient = null; return; } TopMost = false; GameSelectDialog selectDlg = new GameSelectDialog(game); if (selectDlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) { game.networkClient.Disconnect(); TopMost = true; return; } game.Connect(); btnConnect.Enabled = false; btnStart.Enabled = true; btnConnect.Text = "Connected"; TopMost = true; } }
private void btnHost_Click(object sender, EventArgs e) { NetworkDialog dlg = new NetworkDialog(); dlg.txtServer.Enabled = false; if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) { return; } try { server = new NetworkServer(dlg.port); server.gameCreated += GameCreated; server.SetServerCallback(4, (_, message) => { this.server.hostedGames[_.gameID].open = false; this.server.UpdateGamesList(); }); System.Net.IPAddress publicIP = NetworkServer.GetPublicIP(); lblPublicIP.Text = (publicIP == null ? "Public IP could not be evaluated." : "Public IP: " + publicIP.ToString()) + "\nLocal IP:" + NetworkServer.LocalIPAddresses()[0].ToString(); btnHost.Enabled = false; while (Created) { try { server.Update(); Application.DoEvents(); System.Threading.Thread.Sleep(1); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } finally { server = null; } }