private void Client_OnDataReceived(byte[] buffer) { PacketReader pr = new PacketReader(buffer); Byte ID = pr.ReadByte(); if(ID == 1) { int id = pr.ReadInt32(); string message = pr.ReadString(); string msg = id + " : " + message; listBox_messages.Items.Add(msg); } }
/// <summary> /// Connect the client /// </summary> /// <returns>Success</returns> public bool Connect() { try { Client.Connect(Hostname, Port); if (ClientRequest != null) ClientRequest.Connect(Hostname, PortRequest); byte[] idBuffer = new byte[4]; Client.GetStream().Read(idBuffer, 0, idBuffer.Length); using (PacketReader pr = new PacketReader(idBuffer)) { ID = pr.ReadInt32(); } if (OnConnected != null) OnConnected(); StartHandle(); DebugMessage("Connected successfully"); Connected = true; return true; } catch (Exception ex) { DebugMessage("Failed to connect : " + ex.Message); Disconnect(); return false; } }