private void button_connect_Click(object sender, EventArgs e) { if (textBox_username.Text.Trim() != "" && textBox_password.Text.Trim() != "") { PacketWriter pw = new PacketWriter(); pw.WriteByte(1); pw.WriteString(textBox_username.Text.Trim()); pw.WriteString(textBox_password.Text.Trim()); byte[] response = client.SendRequest(pw.ToArray()); PacketReader pr = new PacketReader(response); byte r = pr.ReadByte(); if(r == 0) { MessageBox.Show("Connection successful !"); } else if(r == 2) { MessageBox.Show("Banned : " + pr.ReadString()); } else { MessageBox.Show("Wrong password or username !"); } } }
private void textBox_message_KeyDown(object sender, KeyEventArgs e) { if(e.KeyCode == Keys.Enter) { PacketWriter pw = new PacketWriter(); pw.WriteByte(1); pw.WriteString(textBox_message.Text); textBox_message.Clear(); client.Send(pw.ToArray()); } }
public byte[] LoginRequest(string username, string password) { connection.Open(); byte[] response = null; SQLiteCommand command = new SQLiteCommand("select * from accounts where username='******'", connection); SQLiteDataReader sqlreader = command.ExecuteReader(); if (sqlreader.Read()) { if (SHA256Encrypt(password) == sqlreader["password"].ToString()) { if (!(bool)sqlreader["banned"]) { Program.Log("Connection request accepted !"); response = new byte[1]; using (PacketWriter pw = new PacketWriter()) { pw.WriteByte(0); response = pw.ToArray(); } } else { Program.Log("Connection request failed : banned user !"); using (PacketWriter pw = new PacketWriter()) { pw.WriteByte(2); pw.WriteString(sqlreader["bannedr"].ToString()); response = pw.ToArray(); } } } } if (response == null) { Program.Log("Connection request failed : bad username or password !"); using (PacketWriter pw = new PacketWriter()) { pw.WriteByte(1); response = pw.ToArray(); } } connection.Close(); return response; }
static void server_OnDataReceived(eSClient client, byte[] buffer) { PacketReader pr = new PacketReader(buffer); Byte ID = pr.ReadByte(); if(ID == 1) { string message = pr.ReadString(); PacketWriter pw = new PacketWriter(); pw.WriteByte(ID); pw.WriteInt32(server.GetIDFromTcpClient(client.GetTcpClient())); pw.WriteString(message); server.SendToAll(pw.ToArray()); Console.WriteLine("Message received from " + client.GetID() + " : " + message); } }
/// <summary> /// Listen for client /// </summary> /// <returns></returns> private async Task ListenAsync() { try { while (true) { TcpClient client = await Listener.AcceptTcpClientAsync(); Int32 id = (Int32)randomID.Next(10000, 99999); eSClient sClient = new eSClient(id, client); byte[] idBuffer; using (PacketWriter pw = new PacketWriter()) { pw.WriteInt32(id); idBuffer = pw.ToArray(); } client.Send(idBuffer); ClientList.Add(sClient); if (OnClientConnected != null) OnClientConnected(sClient); StartHandleClient(sClient); } } catch (Exception ex) { DebugMessage("Failed to listen for new client : " + ex.Message); } finally { Stop(); } }