internal static byte[] GetBytesFromStruct(HeaderMsg header)
        {
            //byte[] arr = null;

            //try
            //{z
            //    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();
        }
Пример #2
0
        internal static byte[] GetBytesFromStruct(HeaderMsg header)
        {
            //byte[] arr = null;

            //try
            //{z
            //    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 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;
        }
Пример #4
0
        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);
        }
Пример #5
0
        void RunServer()
        {
            if (serverListener != null)
            {
                serverListener.Start();
                Console.WriteLine("Server started");

                while (true)
                {
                    try
                    {
                        if (!clientSocket.Connected)
                        {
                            Console.WriteLine("Waiting for a connection...");

                            clientSocket = serverListener.AcceptTcpClient();

                            Console.WriteLine("Connected");
                        }
                        NetworkStream networkStream = clientSocket.GetStream();

                        byte[] bytesFrom = new byte[10240];

                        networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                        string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                        dataFromClient = dataFromClient.TrimEnd('\0');

                        Console.WriteLine("Data From Client {0}:\n{1}\n", ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString(), dataFromClient);

                        // string serverResponse = "Data Received";

                        HeaderMsg header = new HeaderMsg();
                        header.messageID   = sequence++;
                        header.messageFrom = "server";
                        header.messageTO   = ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString();
                        header.messageSize = 0;

                        byte[] sendBytes = Utility.GetBytesFromStruct(header);

                        networkStream.Write(sendBytes, 0, sendBytes.Length);

                        networkStream.Flush();

                        Console.WriteLine("Response Sent");
                    }
                    catch (System.Exception ex)
                    {
                        Console.WriteLine("Exception: {0}", ex.Message);
                        clientSocket.Close();
                    }
                }
            }
            else
            {
                Console.WriteLine("Server error");
                stopServer.Set();
                return;
            }
        }// run server
Пример #6
0
        void RunServer()
        {
            if (serverListener!=null)
            {
                serverListener.Start();
                Console.WriteLine("Server started");

            while (true)
            {
                try
                {
                    if (!clientSocket.Connected)
                    {
                        Console.WriteLine("Waiting for a connection...");

                        clientSocket = serverListener.AcceptTcpClient();

                        Console.WriteLine("Connected");

                    }
                    NetworkStream networkStream = clientSocket.GetStream();

                    byte[] bytesFrom = new byte[10240];

                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);

                    string dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                    dataFromClient = dataFromClient.TrimEnd('\0');

                    Console.WriteLine("Data From Client {0}:\n{1}\n", ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString(), dataFromClient);

                   // string serverResponse = "Data Received";

                    HeaderMsg header = new HeaderMsg();
                    header.messageID = sequence++;
                    header.messageFrom = "server";
                    header.messageTO = ((IPEndPoint)clientSocket.Client.RemoteEndPoint).Address.ToString();
                    header.messageSize = 0;

                    byte[] sendBytes = Utility.GetBytesFromStruct(header);

                    networkStream.Write(sendBytes, 0, sendBytes.Length);

                    networkStream.Flush();

                    Console.WriteLine("Response Sent");

                }
                catch (System.Exception ex)
                {
                    Console.WriteLine("Exception: {0}", ex.Message);
                    clientSocket.Close();
                }
            }
            }
            else
            {
                Console.WriteLine("Server error");
                stopServer.Set();
                return;
            }
        }