public ServerConnection(string host, int port, string username, string password) { Host = host; Port = port; Username = username; _currentConnection = this; try { Client = new TcpClient(host, port); Reader = new StreamReader(Client.GetStream()); Writer = new StreamWriter(Client.GetStream()); if (password.Equals("")) { Writer.WriteLine("M:/name " + username); } else { Writer.WriteLine("M:/auth " + username + " " + password); } Writer.Flush(); Writer.WriteLine("S:Client"); Writer.Flush(); Writer.WriteLine("S:Account"); Writer.Flush(); Writer.WriteLine("S:ChannelClientList"); Writer.Flush(); } catch { Client = null; } }
public static void SetCurrentConnection(ServerConnection connection) { lock (_lock) { _currentConnection = connection; } }
private void _connectButton_Click(object sender, EventArgs e) { string host = _hostInput.Text; int port = 0; bool validPort = int.TryParse(_portInput.Text, out port); string username = _usernameInput.Text; if (string.IsNullOrEmpty(host) || string.IsNullOrWhiteSpace(host)) { MessageBox.Show("Please correct the errors with the host field.", "Host"); return; } if (string.IsNullOrEmpty(_portInput.Text) || !validPort) { MessageBox.Show("Please correct the errors with the port field.", "Port"); return; } if (!ValidUsername(username)) { MessageBox.Show("Please correct the errors with the username field.", "Username"); return; } string password = ""; if (!string.IsNullOrEmpty(_passwordInput.Text) && !string.IsNullOrWhiteSpace(_passwordInput.Text)) { password = _passwordInput.Text; } ServerConnection connection = new ServerConnection(host, port, username, password); new ChatWindow().Show(); Hide(); }