internal static byte[] GetBytesFromStruct(HeaderMsg header) { //byte[] arr = null; //try //{ // int size = Marshal.SizeOf(header); // arr = new byte[size]; // IntPtr ptr = Marshal.AllocHGlobal(size); // Marshal.StructureToPtr(header, ptr, true); // Marshal.Copy(ptr, arr, 0, size); // Marshal.FreeHGlobal(ptr); //} //catch(Exception e) //{ // Console.WriteLine(e.Message); //} //return arr; var stream = new MemoryStream(); var writer = new BinaryWriter(stream); writer.Write(header.messageID); writer.Write(header.messageFrom); writer.Write(header.messageTO); writer.Write(header.messageSize); return stream.ToArray(); }
internal static byte[] GetBytesFromStruct(HeaderMsg header) { //byte[] arr = null; //try //{ // int size = Marshal.SizeOf(header); // arr = new byte[size]; // IntPtr ptr = Marshal.AllocHGlobal(size); // Marshal.StructureToPtr(header, ptr, true); // Marshal.Copy(ptr, arr, 0, size); // Marshal.FreeHGlobal(ptr); //} //catch(Exception e) //{ // Console.WriteLine(e.Message); //} //return arr; var stream = new MemoryStream(); var writer = new BinaryWriter(stream); writer.Write(header.messageID); writer.Write(header.messageFrom); writer.Write(header.messageTO); writer.Write(header.messageSize); return(stream.ToArray()); }
void ListenResponse() { Console.WriteLine("Start Client Listening"); try { while (true) { byte[] responseByte = new byte[10240]; networkStream.Read(responseByte, 0, (int)clientSocket.ReceiveBufferSize); //string responseData = Encoding.ASCII.GetString(responseByte); HeaderMsg header = Utility.GetStructFromBytes(responseByte); if (ServerEvent != null) { ServerEvent(this, header); } } } catch (System.Exception ex) { Console.WriteLine("Server error:{0}", ex.Message); serverEvent.Set(); } }
static void client_ServerEvent(object sender, object response) { HeaderMsg header = (HeaderMsg)response; StringBuilder builder = new StringBuilder(); builder.Append(String.Format("ID:{0}\n", header.messageID)); builder.Append(String.Format("From:{0}\n", header.messageFrom)); builder.Append(String.Format("To:{0}\n", header.messageTO)); builder.Append(String.Format("Rest Size:{0}", header.messageSize)); Console.WriteLine("Message From Server:\n{0}\n", builder.ToString()); }
internal static HeaderMsg GetStructFromBytes(byte[] data) { //HeaderMsg header = new HeaderMsg(); //int size = Marshal.SizeOf(header); //IntPtr ptr = Marshal.AllocHGlobal(size); //Marshal.Copy(data, 0, ptr, size); //header = (HeaderMsg)Marshal.PtrToStructure(ptr, data.GetType()); //Marshal.FreeHGlobal(ptr); //return header; var reader = new BinaryReader(new MemoryStream(data)); HeaderMsg header = new HeaderMsg(); header.messageID = reader.ReadInt32(); header.messageFrom = reader.ReadString(); header.messageTO = reader.ReadString(); header.messageSize = reader.ReadInt32(); return header; }
internal static HeaderMsg GetStructFromBytes(byte[] data) { //HeaderMsg header = new HeaderMsg(); //int size = Marshal.SizeOf(header); //IntPtr ptr = Marshal.AllocHGlobal(size); //Marshal.Copy(data, 0, ptr, size); //header = (HeaderMsg)Marshal.PtrToStructure(ptr, data.GetType()); //Marshal.FreeHGlobal(ptr); //return header; var reader = new BinaryReader(new MemoryStream(data)); HeaderMsg header = new HeaderMsg(); header.messageID = reader.ReadInt32(); header.messageFrom = reader.ReadString(); header.messageTO = reader.ReadString(); header.messageSize = reader.ReadInt32(); return(header); }