private void conn_result(object sender, connectionEventArgs e) { int position = 0; if (e.Agree == false) MessageBox.Show("Invalid Username/Password"); else if (e.Agree == true) { this.label4.Text = this.username; //filling the list box if (e.IDs != null) { string temp; checkedListBox1.Items.Clear(); foreach (users item in e.IDs) { if (item.id != "\0\0\0\0\0\0\0\0\0\0") { position = item.id.IndexOf('\0'); if (position != -1) temp = item.id.Substring(0, position); else temp = item.id; if (temp != this.username) if (item.online) checkedListBox1.Items.Add(temp, true); else checkedListBox1.Items.Add(temp, false); } else break; } } //starting the thread connThread1 = new Thread(new ThreadStart(conn.Reception)); connThread1.Start(); label1.Visible = true; } }
public void Login() { int a = 0; byte[] answer = new byte[1100]; byte[] arr = new byte[20]; ipep1 = new IPEndPoint(IPAddress.Parse(ip), 9050); Form1.clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { Form1.clientSock.Connect(ipep1); } catch (SocketException d) { MessageBox.Show(d.Message); } //change to byte array try { Encoding.ASCII.GetBytes(username, 0, username.Length, arr, 0); int s = 10 - username.Length; int b = username.Length; for (int i = 0; i < s; ++i, ++b) Encoding.ASCII.GetBytes("\0", 0, 1, arr, b); // Encoding.ASCII.GetBytes(password, 0, password.Length, arr, 10); int e = 10 - password.Length; int t = password.Length + 10; for (int j = 0; j < e; ++j, ++t) Encoding.ASCII.GetBytes("\0", 0, 1, arr, t); } catch (Exception h) { MessageBox.Show(h.Message); } try { a = Form1.clientSock.Send(arr); //sending username & password if (a == 0) { MessageBox.Show("Could not send username and password"); } Form1.clientSock.Receive(answer, 0, 1100, SocketFlags.None); if (Encoding.ASCII.GetString(answer, 0, 14) == "wrong password") { connectionEventArgs j = new connectionEventArgs(false, answer); result(this, j); } else //Connected , the array "answer" contains all the on line { //users connectionEventArgs g = new connectionEventArgs(true, answer); result(this, g); } } catch (SocketException o) { MessageBox.Show("Could not send the Username and Password"); } }