public bool connect(string srvIP, int srvPort, string n) { serverIP = srvIP; serverPort = srvPort; string nick = n; string xmlReadyNick = n.Trim(new char[]{'$'}); for(;;) try { clientSocket.Connect(srvIP, srvPort); serverStream = clientSocket.GetStream(); byte[] outStream = System.Text.Encoding.ASCII.GetBytes(nick); serverStream.Write(outStream, 0, outStream.Length); serverStream.Flush(); ctThread = new Thread(getMessage); ctThread.Start(); readData = "Conected to Chat Server!"; form.msg(readData); xms = new xmlMessageSender(xmlReadyNick); return true; } catch (System.Net.Sockets.SocketException err) { MessageBox.Show("Error connecting to server\n" + err); return false; } }
private void connectButton_Click(object sender, EventArgs e) { string nick = nickField.Text + "$"; //string nick = nickField.Text; serverIP = IPField.Text; try { serverPort = int.Parse(portField.Text); }//end try catch (System.FormatException err) { MessageBox.Show("Server Port: can only contain numbers"); return; }//end catch if (client.connect(serverIP, serverPort, nick)) { form.connectButton.Visible = false; form.button1.Visible = true; form.messageField.Enabled = true; form.sendButton.Enabled = true; form.label1.Text = "Connected: " + serverIP + " : " + serverPort; form.Text = "Connected to: " + serverIP + " Port : " + serverPort; form.messageField.Focus(); this.Close(); xmlMessageSender msg = new xmlMessageSender(nick); } }